Created
February 5, 2013 18:08
-
-
Save rcmachado/4716386 to your computer and use it in GitHub Desktop.
Revisions
-
rcmachado created this gist
Feb 5, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,54 @@ /** * Logger class to print messages according to specified level. * * Usage: * var log = new Logger(Logger.INFO); * log.info("Show message"); */ function Logger(level) { this.level = level || Logger.DEBUG; } (function () { fn = Logger.prototype; /* Log levels */ fn.DEBUG = 10; fn.INFO = 20; fn.WARNING = 30; fn.ERROR = 40; fn.LEVEL_NAMES = {}; fn.LEVEL_NAMES[fn.DEBUG] = 'DEBUG'; fn.LEVEL_NAMES[fn.INFO] = 'INFO'; fn.LEVEL_NAMES[fn.WARNING] = 'WARNING'; fn.LEVEL_NAMES[fn.ERROR] = 'ERROR'; fn.log = function(level, msg) { if (level > this.level) { return; } var levelName = fn.LEVEL_NAMES[level], dateFull = ISODate().tojson(), dateString = dateFull.replace('ISODate("', '').replace('")', ''); print(levelName + " " + dateString + " " + msg); }; fn.debug = function(msg) { this.log(fn.DEBUG, msg); }; fn.info = function(msg) { this.log(fn.INFO, msg); }; fn.warning = fn.warn = function(msg) { this.log(fn.WARNING, msg); }; fn.error = function(msg) { this.log(fn.ERROR, msg); }; }()); This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,7 @@ // Usage example for Logger javascript class // Don't forget to include the logger.js file var log = new Logger(Logger.INFO); log.info("This message will be printed on stdout"); log.error("And so this one"); log.debug("But this don't! (As Logger.INFO was specified on object creation.)");