Last active
December 13, 2015 19:38
-
-
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.
This file contains 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
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