Skip to content

Instantly share code, notes, and snippets.

@JokingChicken
Last active November 20, 2018 10:22
Show Gist options
  • Save JokingChicken/e5b3f99fd315b9537ec305c058e896ec to your computer and use it in GitHub Desktop.
Save JokingChicken/e5b3f99fd315b9537ec305c058e896ec to your computer and use it in GitHub Desktop.
a custom logger for js
/*
* creator: Danj (https://github.com/DanBrothers)
* description: logger, made for easly configuration
* license: MIT, please link to this if you copy (thanks)
*
* usage:
var console = new Logger({
enabled: true,
enabledebug:true,
customfunction: function(e){
logelement.innerHTML = e.detail.message;
},
prefixes:{
log:"",
debug:"debugger"
}
});
//var chatcolor = console.getColors(); **not suported, but planned for in the future**
console.log(**chatcolor.GREEN +** chatcolor.background.BLACK + "all is good...", true);
console.info(**chatcolor.rgb(211,211,211, false) +** "this is lightgray").debug("this is debugger text");
if(console.resettings({ customfunction:(function(e) {console.log(e.detail.rawmessage, false)}) })) {
console.debug("changed settings for logger...");
}
//if you want to use the old console
console.console.log("log using the old console");
console.console.log(chatcolor.BLUE + "can still use the colors tho");
*/
function Logger(object) {
object = this.merge({
enabled: true,
enabledebug: false,
usecolors: true,
customfunction: null,
prefixes: {
log: this.colors.WHITE + "[log]",
debug: this.colors.BLUE + "[debug]",
info: this.colors.GRAY + "[info]",
warn: this.colors.ORANGE + "[warn]",
error: this.colors.RED + "[error]"
}
}, object);
this.console = console;
this.customfunction = object.customfunction;
this.enabled = object.enabled;
this.enabledebug = object.enabledebug;
this.usecolors = object.usecolors;
this.prefixes = {
log: object.prefixes.log,
debug: object.prefixes.debug,
info: object.prefixes.info,
warn: object.prefixes.warn,
error: object.prefixes.error
};
if(object.customfunction) {
if(document) {
document.addEventListener('nlm_logger', object.customfunction(e));
}
}
/* create a new function with specified name (for creating a extra log, with own specified function. like logging to file)
var myName = "myName";
var f = function () { return true; };
Object.defineProperty(f, 'name', {value: myName, writable: false});
*/
/*
// these colors only work for node (cmd, )
this.colors = {
RESET: "\x1b[0m",
BRIGHT: "\x1b[1m",
DIM: "\x1b[2m",
UNDERSCORE: "\x1b[4m",
BLINK: "\x1b[5m",
REVERSE: "\x1b[7m",
HIDDEN: "\x1b[8m",
BLACK: "\x1b[30m",
RED: '\x1b[31m',
GREEN: "\x1b[32m",
YELLOW: "\x1b[33m",
BLUE: "\x1b[34m",
MAGENTA: "\x1b[35m",
CYAN: "\x1b[36m",
WHITE: "\x1b[37m",
GRAY: "\x1b[38;2;128;128;128m",
background: {
BLACK: "\x1b[40m",
RED: "\x1b[41m",
GREEN: "\x1b[42m",
YELLOW: "\x1b[43m",
BLUE: "\x1b[44m",
MAGENTA: "\x1b[45m",
CYAN: "\x1b[46m",
WHITE: "\x1b[47m"
},
rgb: function(r,g,b, isbackground){
if(isbackground) {
return '\x1b[48;2;' + r +';'+ g +';'+ b + 'm';
}
return '\x1b[38;2;' + r +';'+ g +';'+ b + 'm';
}
};*/
this.intlog = function(text, type, ep){
if(this.enabled) {
const raw = text;
if(!text) {
text = "\n";
}else {
if(enablepre || ep) {
text = (this.prefixes[type] + this.colors.RESET + " " + text);
}
}
document.dispatchEvent(new CustomEvent('nlm_logger', { detail:{type, text, rawmessage} }));
this.console.log.apply(this.console, [text]);
}
};
this.merge = function(obj, src) {
Object.keys(src).forEach(function(key) { obj[key] = src[key]; });
return obj;
}
};
Logger.prototype.getColors = function() {
return this.colors;
};
Logger.prototype.log = function(text, enablepre) {
this.intlog(text, "log", true);
return this;
};
Logger.prototype.info = function(text) {
this.intlog(text, "info");
return this;
};
Logger.prototype.warn = function(text) {
this.intlog(text, "warn");
return this;
};
Logger.prototype.error = function(text) {
this.intlog(text, "error");
return this;
};
Logger.prototype.debug = function(text, type) {
this.intlog(text, "debug");
return this;
};
Logger.prototype.resettings = function(newset) {
if (newset) {
var object = this.merge({
enabled: this.enabled,
enabledebug: this.enabledebug,
customfunction: this.customfunction,
prefixes: {
log: this.prefixes.log,
debug: this.prefixes.debug,
info: this.prefixes.info,
warn: this.prefixes.warn,
error: this.prefixes.error
}
}, newset);
if(newset.customfunction) {
if(document) {
document.addEventListener('nlm_logger', newset.customfunction(e));
}
}
this.enabled = object.enabled;
this.enabledebug = object.enabledebug;
this.customfunction = object.customfunction;
this.prefixes = {
log: object.prefixes.log,
debug: object.prefixes.debug,
info: object.prefixes.info,
warn: object.prefixes.warn,
error: object.prefixes.error
};
return true;
}
return false;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment