Skip to content

Instantly share code, notes, and snippets.

@malulleybovo
Created November 28, 2017 23:16
Show Gist options
  • Save malulleybovo/94444a0fd2789ac41ea8740e56311e03 to your computer and use it in GitHub Desktop.
Save malulleybovo/94444a0fd2789ac41ea8740e56311e03 to your computer and use it in GitHub Desktop.
iLog - Intelligent Logger that personalizes console.log, console.warn, console.error, and console.info
/**
* iLog : intelligent logger.
* Supports : .log(), .warn(), .error(), and .info()
* Configuring :
* Change iLogCfg to your fit.
* Supports color changes and enable/disable stack trace for each of the log functions
* Use :
* console.ilog('my comment');
* -> my comment
* console.ilog(myVar1, myVar2, [more...]);
* -> [browser displays the objects]
* console.ilog('my header', 'my comment');
* -> my header: my comment
* console.ilog('my header', myVar1, myVar2, [more...]);
* -> my header: [browser displays the objects]
* console.ilog('my header', 'var1 = %o and var2 = %o', myVar1, myVar2);
* -> my header: var1 = [browser displays myVar1] and var2 = [browser displays myVar2]
* console.iwarn('my header', 'var1 = %o and var2 = %o', myVar1, myVar2);
* -> my header: var1 = [browser displays myVar1] and var2 = [browser displays myVar2]
* console.ierror('my header', 'var1 = %o and var2 = %o', myVar1, myVar2);
* -> my header: var1 = [browser displays myVar1] and var2 = [browser displays myVar2]
* console.iinfo('my header', 'var1 = %o and var2 = %o', myVar1, myVar2);
* -> my header: var1 = [browser displays myVar1] and var2 = [browser displays myVar2]
*/
const iLogCfg = {
css: {
header: 'color: #a6cd94',
log: 'color: #d5d5d5',
warn: 'color: #ffdc9e',
error: 'color: #ff7f7f',
info: 'color: #d5d5d5',
defaultheader: 'color: #a6cd94',
defaultlog: 'color: #d5d5d5',
defaultwarn: 'color: #ffdc9e',
defaulterror: 'color: #ff7f7f',
defaultinfo: 'color: #d5d5d5',
},
stackTrace: {
log: true,
warn: true,
error: true,
info: true
}
}
Object.defineProperty(iLogCfg, "css", {
writable: false
});
Object.defineProperty(iLogCfg, "stackTrace", {
writable: false
});
Object.defineProperty(iLogCfg.css, "defaultheader", {
writable: false
});
Object.defineProperty(iLogCfg.css, "defaultlog", {
writable: false
});
Object.defineProperty(iLogCfg.css, "defaultwarn", {
writable: false
});
Object.defineProperty(iLogCfg.css, "defaulterror", {
writable: false
});
console.ilog = function () {
logType.apply(this, ['log'].concat(
Array.prototype.slice.call(arguments)));
};
console.iwarn = function () {
logType.apply(this, ['warn'].concat(
Array.prototype.slice.call(arguments)));
};
console.ierror = function () {
logType.apply(this, ['error'].concat(
Array.prototype.slice.call(arguments)));
};
console.iinfo = function () {
logType.apply(this, ['info'].concat(
Array.prototype.slice.call(arguments)));
};
const logType = function (type, header, msg) {
if (type !== 'log'
&& type !== 'warn'
&& type !== 'error'
&& type !== 'info') return;
if (console[type]) {
if (typeof header !== 'string'
|| arguments.length == 2) {
console[type].apply(this, Array.prototype.slice.call(Array.prototype.slice.call(arguments, 1)));
return;
}
let slicedArgs;
if (typeof msg !== 'string') {
// Not a message, so interpret as object
slicedArgs = Array.prototype.slice.call(arguments, 2);
msg = "";
}
else {
slicedArgs = Array.prototype.slice.call(arguments, 3);
}
if (typeof msg !== 'string') return;
let headerColor = (typeof iLogCfg.css.header === 'string') ?
iLogCfg.css.header : iLogCfg.css.defaultheader;
let msgColor = (typeof iLogCfg.css[type] === 'string') ?
iLogCfg.css[type] : iLogCfg.css['default' + type];
(console[type]).apply(this,
[('%c' + header + ':%c ' + msg), headerColor, msgColor]
.concat(Array.prototype.slice.call(slicedArgs))
);
if (iLogCfg.stackTrace[type] === true) {
console.trace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment