Skip to content

Instantly share code, notes, and snippets.

@loretoparisi
Created March 10, 2016 16:00
Show Gist options
  • Save loretoparisi/55960e6dd0e6bf669ce5 to your computer and use it in GitHub Desktop.
Save loretoparisi/55960e6dd0e6bf669ce5 to your computer and use it in GitHub Desktop.
Colors Logger to Browser Console or Node Shell
/**
* Node or Browser Console Logger
* @author: Loreto Parisi (loretoparisi gmail dot com)
*/
var LP Logger = {
/** True if browser console, False if is node */
isConsole : ((typeof process === 'object' && process + '' === '[object process]')?false:true),
level:0,
settings: {
debug : true
},
/** Console Color Set */
CONSOLECOLORS : {
BG : '#FFF',
DEBUG : '#3399FF',
ERROR : '#CC0000',
WARN : '#FFCC00',
INFO : '#00CC33'
},
/** Shell Color Set */
SHELLCOLORS : {
Bg : '\033[99m',
Red : '\033[91m',
Green : '\033[92m',
Blue : '\033[94m',
Cyan : '\033[96m',
White : '\033[97m',
Yellow : '\033[93m',
Magenta : '\033[95m',
Grey : '\033[90m',
Black : '\033[90m'
},
setLevel:function(level) { this.level=level; },
getLevel:function() { return this.level;},
isDebug : function() {
return this.level==3;
},
isWarn : function() {
return this.level==1;
},
isInfo : function() {
return this.level==2;
},
isError : function() {
return this.level==0;
},
_log : function(message) {
if(typeof(console)!='undefined') console.log(message)
},
_replace : function(args) {
var rep= args.slice(1, args.length);
var i=0;
var output = args[0].replace(/%s/g, function(match,idx) {
var subst=rep.slice(i, ++i);
return( subst );
});
return(output);
},//replace,
error : function() {
var message=this._replace( Array.prototype.slice.call(arguments) );
if(this.level>=0) {
var bg=this.isConsole?this.CONSOLECOLORS.BG:this.SHELLCOLORS.Bg;
var color=this.isConsole?this.CONSOLECOLORS.ERROR:this.SHELLCOLORS.Red;
this._print(message,this.LEVELCOLORS.BG,this.LEVELCOLORS.ERROR);
}
},
info:function() {
var message=this._replace( Array.prototype.slice.call(arguments) );
if(this.level>=2) {
var bg=this.isConsole?this.CONSOLECOLORS.BG:this.SHELLCOLORS.Bg;
var color=this.isConsole?this.CONSOLECOLORS.INFO:this.SHELLCOLORS.Grey;
this._print(message,bg,color);
}
},
warn:function() {
var message=this._replace( Array.prototype.slice.call(arguments) );
if(this.level>=1) {
var bg=this.isConsole?this.CONSOLECOLORS.BG:this.SHELLCOLORS.Bg;
var color=this.isConsole?this.CONSOLECOLORS.WARN:this.SHELLCOLORS.Yellow;
this._print(message,bg,color);
}
},
debug:function() {
var message=this._replace( Array.prototype.slice.call(arguments) );
if(this.level>=3) {
var bg=this.isConsole?this.CONSOLECOLORS.BG:this.SHELLCOLORS.Bg;
var color=this.isConsole?this.CONSOLECOLORS.DEBUG:this.SHELLCOLORS.Blue;
this._print(message,bg,color);
}
},
_print : function(msg,bgc,txc) {
if(msg instanceof Object) {
try {
var time = new Date();
var msg = "[" + time + "] " + JSON.stringify(msg);
console.log( msg );
} catch(e) {
var time = new Date();
var msg = time + " " + msg;
this.log( msg );
}
} else {
var time = new Date();
var msg = "[" + time + "] " + msg;
if(this.isConsole) { //browser console
console.log('%c ' + msg, 'background:'+bgc+'; color:'+txc+';');
}
else { //node
console.log(txc+' ' + msg);
}
}
}
}//LPLogger
LPLogger.LEVEL_ERROR = 0;
LPLogger.LEVEL_WARN = 1;
LPLogger.LEVEL_INFO = 2;
LPLogger.LEVEL_DEBUG = 3;
//Example
LPLogger.setLevel(LPLogger.LEVEL_DEBUG);
LPLogger.debug("this is %s nifty %s message","blue","debug");
LPLogger.warn("this is %s nifty %s message","yellow", "warn");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment