Last active
August 14, 2020 18:36
-
-
Save Jerrylum/ffc47984ca832d2e2ec3310cecafce15 to your computer and use it in GitHub Desktop.
This is a very simple and beautiful logger
This file contains hidden or 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
'use strict'; | |
var RESET = '[0m'; | |
var SAFE = '[32m[1m'; | |
var WARN = '[33m[1m'; | |
var ERROR = '[31m[1m'; | |
var VERBOSE = '[34m[1m'; | |
const join = function(source, to) { | |
for (const [key, value] of Object.entries(source)) | |
if (!(key in to)) | |
to[key] = value; | |
} | |
const overrides = { | |
log: function() { | |
console.log(...this._package_msg(SAFE, arguments)); | |
return this; | |
}, | |
warn: function() { | |
console.warn(...this._package_msg(WARN, arguments)); | |
return this; | |
}, | |
error: function() { | |
console.error(...this._package_msg(ERROR, arguments)); | |
return this; | |
}, | |
verbose: function() { | |
console.log(...this._package_msg(VERBOSE, arguments)); | |
return this; | |
}, | |
_package_msg: function(color, args) { | |
const prefix = color + this._sender + ' > ' + RESET; | |
if (typeof args[0] === 'string') { | |
args[0] = prefix + args[0]; | |
return args; | |
} else { | |
return [prefix, ...args]; | |
} | |
}, | |
_sender: null | |
}; | |
join(console, overrides); | |
module.exports = function(sender) { | |
function Logger() { | |
return Logger.log(...arguments); | |
}; | |
join(overrides, Logger); | |
Logger._sender = sender || 'Unknow'; | |
return Logger; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Code:
Result:
