Skip to content

Instantly share code, notes, and snippets.

@sbrl
Last active January 12, 2020 21:28
Show Gist options
  • Select an option

  • Save sbrl/613a18631561a1d63e022ae2ec324f05 to your computer and use it in GitHub Desktop.

Select an option

Save sbrl/613a18631561a1d63e022ae2ec324f05 to your computer and use it in GitHub Desktop.

Revisions

  1. sbrl revised this gist Jan 12, 2020. 1 changed file with 7 additions and 5 deletions.
    12 changes: 7 additions & 5 deletions Log.mjs
    Original file line number Diff line number Diff line change
    @@ -22,27 +22,27 @@ class Log {


    debug(...message) {
    if(this.level > LOG_LEVELS.DEBUG) continue;
    if(this.level > LOG_LEVELS.DEBUG) return;
    this.__do_log("debug", ...message);
    }

    info(...message) {
    if(this.level > LOG_LEVELS.INFO) continue;
    if(this.level > LOG_LEVELS.INFO) return;
    this.__do_log("info", ...message);
    }

    log(...message) {
    if(this.level > LOG_LEVELS.LOG) continue;
    if(this.level > LOG_LEVELS.LOG) return;
    this.__do_log("log", ...message);
    }

    warn(...message) {
    if(this.level > LOG_LEVELS.WARN) continue;
    if(this.level > LOG_LEVELS.WARN) return;
    this.__do_log("warn", ...message);
    }

    error(...message) {
    if(this.level > LOG_LEVELS.ERROR) continue;
    if(this.level > LOG_LEVELS.ERROR) return;
    this.__do_log("error", ...message);
    }

    @@ -67,5 +67,7 @@ class Log {
    }
    }

    // You won't normally need these
    export { LOG_LEVELS };

    export default new Log();
  2. sbrl created this gist Dec 22, 2019.
    71 changes: 71 additions & 0 deletions Log.mjs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,71 @@
    "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) continue;
    this.__do_log("debug", ...message);
    }

    info(...message) {
    if(this.level > LOG_LEVELS.INFO) continue;
    this.__do_log("info", ...message);
    }

    log(...message) {
    if(this.level > LOG_LEVELS.LOG) continue;
    this.__do_log("log", ...message);
    }

    warn(...message) {
    if(this.level > LOG_LEVELS.WARN) continue;
    this.__do_log("warn", ...message);
    }

    error(...message) {
    if(this.level > LOG_LEVELS.ERROR) continue;
    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);
    }
    }

    export { LOG_LEVELS };
    export default new Log();