Skip to content

Instantly share code, notes, and snippets.

@sbrl
Last active January 12, 2020 21:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sbrl/613a18631561a1d63e022ae2ec324f05 to your computer and use it in GitHub Desktop.
Save sbrl/613a18631561a1d63e022ae2ec324f05 to your computer and use it in GitHub Desktop.
[Log.mjs] Logging utility class
"use strict";
import a from './Ansi.mjs';
const LOG_LEVELS = {
DEBUG: 0,
INFO: 1,
LOG: 2,
WARN: 4,
ERROR: 8,
NONE: 2048
};
class Log {
constructor() {
this.start = new Date();
this.level = LOG_LEVELS.DEBUG;
}
debug(...message) {
if(this.level > LOG_LEVELS.DEBUG) return;
this.__do_log("debug", ...message);
}
info(...message) {
if(this.level > LOG_LEVELS.INFO) return;
this.__do_log("info", ...message);
}
log(...message) {
if(this.level > LOG_LEVELS.LOG) return;
this.__do_log("log", ...message);
}
warn(...message) {
if(this.level > LOG_LEVELS.WARN) return;
this.__do_log("warn", ...message);
}
error(...message) {
if(this.level > LOG_LEVELS.ERROR) return;
this.__do_log("error", ...message);
}
__do_log(level, ...message) {
message.unshift(`${a.locol}[ ${((new Date() - this.start) / 1000).toFixed(3)}]${a.reset}`);
let part = `[ ${level} ]`;
switch(level) {
case "debug":
part = a.locol + part;
break;
case "warn":
part = a.fyellow + part;
break;
case "error":
part = a.fred + part;
break;
}
message.unshift(part)
console.error(...message);
}
}
// You won't normally need these
export { LOG_LEVELS };
export default new Log();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment