Skip to content

Instantly share code, notes, and snippets.

@c4urself
Last active December 13, 2015 19:38
Show Gist options
  • Save c4urself/4964132 to your computer and use it in GitHub Desktop.
Save c4urself/4964132 to your computer and use it in GitHub Desktop.
Simple logging utility for JS, takes IE's lack of console into account as well as difference in browser abilities with regard to console.info, console.warn and console.error.
app = {
DEBUG: true,
VERSION: '1.0.0'
};
app.logs = app.logs || { info: [], error: [], warn: [] };
app.logger = app.logger || {
log: function () {
var type = arguments[0],
msg = arguments[1],
nop = typeof console === 'undefined';
msg = msg ? msg : type;
type = ['info', 'warn', 'error'].indexOf(type) < 0 ? 'info' : type;
app.logs[type].push(msg);
if (!nop && ((!app.DEBUG && type === 'error') || app.DEBUG)) {
var func = console[type] || console.log;
return func.apply(console, [msg]);
}
}
};
// USE:
// >>> app.logger.log('info', 'My info message');
// >>> app.logs.info
// ['My info message']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment