All files / src logger.ts

100% Statements 20/20
90% Branches 9/10
100% Functions 5/5
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 951x                         1x               923x 923x 922x 922x         1x   922x       326x                 2945x                 8x                 3279x 3279x 334x                 2945x             3279x 554x       2725x 252x         3279x      
import * as util from "util";
 
/**
 * A simple logger.  Supports info, and warn without any configuration
 *
 * Leverages [util.debuglog](https://nodejs.org/api/util.html#util_util_debuglog_section) to enable debug logging
 * To use debug logging:
 *
 * ```
 * export NODE_DEBUG=module-name,other-module
 * ```
 *
 */
export class Logger {
    private logger: (msg: string, ...param: string[]) => void;
 
 
    /**
     * Initialize a logger
     * @param modname Should always be the value module.id
     */
    constructor(public modname: string) {
        if (this.modname) {
            this.modname = this.modname.split(/\//u).pop() || this.modname;
            this.modname = this.modname.substring(
                0,
                this.modname.lastIndexOf(".")
            );
        } else {
            throw new Error("You must initialize the logger with module.id");
        }
        this.logger = util.debuglog(this.modname);
    }
 
    public info(message: string, ...args: string[]) {
        console.info(this.format(
            "info",
            message,
            true,
            args
        ));
    }
 
    public debug(message: string, ...args: string[]) {
        this.logger(this.format(
            "debug",
            message,
            false,
            args
        ));
    }
 
    public warn(message: string, ...args: string[]) {
        console.warn(this.format(
            "warn",
            message,
            true,
            args
        ));
    }
 
    private format(level: string, message: string, showName: boolean, args: string[]): string {
        let formatString = "";
        if (showName) {
            formatString = util.format(
                "%s %d: %s - [%s] - %s",
                this.modname.toUpperCase(),
                process.pid,
                new Date().toISOString(),
                level,
                message
            );
        } else {
            formatString = util.format(
                "%s - [%s] - %s",
                new Date().toISOString(),
                level,
                message
            );
        }
        if ((/%s/gmu).test(formatString)) {
            formatString = util.format(
                formatString,
                args
            );
        } else if ((/%j/gmu).test(formatString)) {
            formatString = util.format(
                formatString,
                util.inspect(args)
            );
        }
        return formatString;
    }
}