# All files / infrastructure/logging logger.ts

100% Statements 54/54
94.12% Branches 16/17
100% Functions 9/9
100% Lines 47/47

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
    95
    961x

    1x
    1x

    1x

    1x

    1x

    1x
    1x
    1x

    1x
    1x
    1x

    1x

    6x
    2x
    1x

    1x

    6x
    2x
    1x

    1x

    6x
    2x
    1x

    1x

    6x
    2x
    1x

    1x

    6x
    2x
    1x

    1x

    15x
    5x

    5x

    2x
    2x

    1x
    1x

    1x
    1x

    1x
    1x

    5x

    1x

    1x
     const log = require('loglevel');

    const chalk = require('chalk');
    const loglevelpluginPrefix = require('loglevel-plugin-prefix');

    const LEVEL = {
        LOG: chalk.green,
        DEBUG: chalk.cyan,
        INFO: chalk.green,
        WARN: chalk.yellow,
        ERROR: chalk.red,
    };

    export class Logger {
        private static instance: Logger;

        public silent = false;

        constructor() {
            log.setLevel(0);
            loglevelpluginPrefix.reg(log);
            loglevelpluginPrefix.apply(log, {
                template: '[%t]',
            });
        }

        public static getInstance() {
            Eif (!Logger.instance) {
                Logger.instance = new Logger();
            }
            return Logger.instance;
        }

        public log(...args) {
            if (this.silent) {
                return;
            }
            return log.debug(this.format(LEVEL.LOG, ...args));
        }

        public debug(...args) {
            if (this.silent) {
                return;
            }
            return log.debug(this.format(LEVEL.DEBUG, ...args));
        }

        public info(...args) {
            if (this.silent) {
                return;
            }
            return log.info(this.format(LEVEL.INFO, ...args));
        }

        public warn(...args) {
            if (this.silent) {
                return;
            }
            return log.warn(this.format(LEVEL.WARN, ...args));
        }

        public error(...args) {
            if (this.silent) {
                return;
            }
            return log.error(this.format(LEVEL.ERROR, ...args));
        }

        private format(level, ...args) {
            let msg = args.join(' ');

            switch (level) {
                case LEVEL.INFO:
                case LEVEL.LOG:
                    msg = chalk.green(msg);
                    break;

                case LEVEL.DEBUG:
                    msg = chalk.cyan(msg);
                    break;

                case LEVEL.WARN:
                    msg = chalk.yellow(msg);
                    break;

                case LEVEL.ERROR:
                    msg = chalk.red(msg);
                    break;
            }

            return [msg].join('');
        }
    }

    export default Logger.getInstance();