/** * File: Adawim/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 details of an object. */ // load the things we need var DateFormat = require('dateformat') , Helper = require('./helper.js') , util = require('util') , Inspect = require('eyespect').inspector(); var eyeFriendly = 'friendly'; var isDebug = true; var isTrace = (isDebug && false); 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) { consoleOut(msg); } } exports.debug = function(msg) { msg = DateFormat(new Date(), "yyyy-mm-dd h:MM:ss TT") + " DEBUG " + msg; if(isDebug) { consoleOut(msg); } } exports.log = function(msg) { msg = DateFormat(new Date(), "yyyy-mm-dd h:MM:ss TT") + " INFO " + msg; consoleOut(msg); } exports.error = function(msg) { msg = DateFormat(new Date(), "yyyy-mm-dd h:MM:ss TT") + " ERROR " + msg; consoleErr(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); } } /* private */ function consoleErr(msg, content) { var hasContent = Helper.isDefinedAndNotNull(content); if(hasContent) { var msgAddon = (hasContent ? " | " + content : ""); if(Helper.isDefinedAndNotNull(msg)) { console.error(msg + msgAddon); } } else if(Helper.isDefinedAndNotNull(msg)) { console.error(msg); } }