/** * File: app/logging.js * Author: Gerrit Linnemann * * Logging with several log-level. * - debug: Printed only in development-state. * - log: Needed log-output. * - error: Prints log in error-level. * - inspect: Prints a message and a detail vier on an object. */ // load the things we need var Conf = require('../config.json') , Inspect = require('eyespect').inspector() , Helper = require('./helper.js') , util = require('util') , DateFormat = require('dateformat'); var app = null; var eyeFriendly = null; var isTrace = null; var isDebug = null; exports.init = function(express) { app = express; eyeFriendly = (Conf.logging.mode == 'friendly'); isDebug = ('development' == app.get('env')); isTrace = (isDebug && Conf.logging.trace == 'yes'); Helper.init(app); return this; } exports.isEyeFriendlyMode = function() { return eyeFriendly; } exports.isTrace = function() { return isTrace; } exports.isDebug = function() { return isDebug; } exports.trace = function(msg) { msg = DateFormat(new Date(), "yyyy-mm-dd h:MM:ss TT") + " TRACE " + msg; if(isTrace) { if(eyeFriendly) { util.puts(msg); } else { consoleOut(msg); } } } exports.debug = function(msg) { msg = DateFormat(new Date(), "yyyy-mm-dd h:MM:ss TT") + " DEBUG " + msg; if(isDebug) { if(eyeFriendly) { util.puts(msg); } else { consoleOut(msg); } } } exports.log = function(msg) { msg = DateFormat(new Date(), "yyyy-mm-dd h:MM:ss TT") + " INFO " + msg; if(eyeFriendly) { util.puts(msg); } else { consoleOut(msg); } } exports.error = function(msg) { msg = DateFormat(new Date(), "yyyy-mm-dd h:MM:ss TT") + " ERROR " + msg; if(eyeFriendly) { util.error(msg); } else { consoleOut(msg); } } exports.inspect = function(msg, content) { msg = DateFormat(new Date(), "yyyy-mm-dd h:MM:ss TT") + " INSPECT " + msg; if(eyeFriendly) { Inspect(content, msg); } else { var msgOut = msg + (Helper.isDefinedAndNotNull(content) ? ": "+JSON.stringify(content) : ""); consoleOut(msgOut); } } /* private */ function consoleOut(msg, content) { var hasContent = Helper.isDefinedAndNotNull(content); if(hasContent) { var msgAddon = (hasContent ? " | " + content : ""); if(Helper.isDefinedAndNotNull(msg)) { console.log(msg + msgAddon); } } else if(Helper.isDefinedAndNotNull(msg)) { console.log(msg); } }