Skip to content

Instantly share code, notes, and snippets.

@akayj
Last active September 28, 2015 11:08
Show Gist options
  • Save akayj/1429555 to your computer and use it in GitHub Desktop.
Save akayj/1429555 to your computer and use it in GitHub Desktop.
Logging for JavaScript
var Lgz = function(lgStr) {
var fn, msg, console = window.console || null;
var splitPos = lgStr.indexOf(':'), len = lgStr.length;
var fnMap = {
'i' : 'info',
'e' : 'error',
'w' : 'warn',
'l' : 'log'
};
var rLsplit = /^([^:]*)(?=:)/;
var rRsplit = /(?=:).*$/;
if ( len == 0 ) return;
// 'message'
if ( splitPos < 0 ) console.log(lgStr);
// ':message'
else if ( splitPos == 0 && len != (splitPos+1)) console.log(lgStr.slice(1));
// 'info:'
else if ( len == (splitPos+1) ) return;
// 'log:message'
else {
fn = lgStr.match(rLsplit)[0];
if ( fn.length == 1) {
fn = fnMap[fn];
if ( fn == undefined ) fn = 'log';
}
msg = lgStr.match(rRsplit)[0].split(':')[1];
console[fn](msg);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment