Created
May 4, 2012 15:59
-
-
Save jonnyreeves/2595747 to your computer and use it in GitHub Desktop.
Quick and dirty JavaScript Logging Shim
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 characters
/*global require, define */ | |
define(function (require) { | |
"use strict"; | |
var slice = Array.prototype.slice; | |
// Simple logging shim which allows log messages to be filtered and redirected to a logging solution | |
// of your choice when debugging. | |
var Logger = { | |
DEBUG: 1, | |
INFO: 2, | |
WARN: 4, | |
ERROR: 8, | |
OFF: 99, | |
// Handles incoming log messages. | |
logFunc: null, | |
// Current log level. | |
level: null, | |
debug: function () { | |
if (this.logFunc && Logger.DEBUG >= this.level) { | |
this.logFunc.apply(null, slice.call(arguments).splice(0, 0, Logger.INFO)); | |
} | |
}, | |
info: function () { | |
if (this.logFunc && Logger.INFO >= this.level) { | |
this.logFunc.apply(null, [Logger.INFO].concat(slice.call(arguments))); | |
} | |
}, | |
warn: function () { | |
if (this.logFunc && Logger.WARN >= this.level) { | |
this.logFunc.apply(null, [Logger.WARN].concat(slice.call(arguments))); | |
} | |
}, | |
error: function () { | |
if (this.logFunc && Logger.ERROR >= this.level) { | |
this.logFunc.apply(null, [Logger.ERROR].concat(slice.call(arguments))); | |
} | |
} | |
}; | |
// Returns a String which represents the supplied log level value. | |
Logger.getLevelName = function(level) { | |
switch (level) { | |
case Logger.INFO: | |
return "INFO"; | |
case Logger.DEBUG: | |
return "DEBUG"; | |
case Logger.WARN: | |
return "WARN"; | |
case Logger.ERROR: | |
return "ERROR"; | |
default: | |
return ""; | |
} | |
}; | |
// Aliases. | |
Logger.log = Logger.info; | |
// Default configuration disables logging | |
Logger.level = Logger.OFF; | |
// Enables deubugging at the supplied level. | |
Logger.activate = function(level) { | |
// Check for the presence of a logger. | |
if (!"console" in window) { | |
return; | |
} | |
Logger.level = level || Logger.DEBUG; | |
Logger.logFunc = function(msgLvl) { | |
var hdlr = window.console.log; | |
// Delegate through to custom error loggers if present. | |
if (msgLvl === Logger.WARN && window.console.warn) { | |
hdlr = window.console.warn; | |
} else if (msgLvl == Logger.ERROR && window.console.error) { | |
hdlr = window.console.error; | |
} | |
hdlr.apply(window.console, slice.call(arguments, [1])); | |
}; | |
}; | |
return Logger; | |
}); |
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 characters
var Logger = require('logger'); | |
// Activate, defaults to forwarding all log messages to `window.console`. | |
Logger.activate(); | |
// Log a message, use in place of `console.log()` | |
Logger.log("Example message", { key: "value" }); | |
// >> Example message > Object | |
// Restrict logging to a level. | |
Logger.level = Logger.WARN; | |
Logger.debug("A debug message"); | |
Logger.warn("zOMG! o_O"); | |
Logger.error("ARGH!! 0.0"); | |
// >> zOMG! o_O | |
// >> ARGH!! 0.0 | |
// Redirect the logger function, function should expect the message level (Number) | |
// followed by the remaining log arguments. | |
Logger.logFunc = function(msgLevel) { | |
alert("[" + Logger.getLevelName(msgLevel) + "] " + arguments[1]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment