All files / lib/loggers ConsoleLogger.js

0% Statements 0/22
0% Branches 0/8
0% Functions 0/7
0% Lines 0/22
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                                                                                                         
'use strict';
 
module.exports = function inject(logConfig) {
    logConfig = logConfig || {};
    const extend = require('object-util').extend;
    const colors = require('colors');
    const theme = extend(require('../conf/themes'), logConfig.theme || {});
    colors.setTheme(theme);
 
    const methodNames = [
        'log', 'error', 'warn', 'info', 'assert', 'debug'
    ];
 
    function colorize(name, ...varargs) {
        const colorize = colors[name.toUpperCase()];
        let args = [...varargs];
        return args.map(arg => {
            if (typeof arg === 'object') {
                return colorize(JSON.stringify(arg));
            } else {
                return colorize(arg);
            }
        });
    }
 
    function wrap(name) {
        if (name !== 'debug') {
            ConsoleLogger.prototype[name] = function (...varargs) {
                let args = colorize(name, ...varargs);
                console[name](...args);
            }
        }
    }
 
    /**
     * A default logger with default implementations so Loggers don't have to implement all of the methods.
     * Sends messages to the console.
     *
     * @class
     * @implements {Logger}
     */
    function ConsoleLogger() {
    }
 
    ConsoleLogger.prototype.debug = function (...varargs) {
        let args = colorize('debug', ...varargs);
        console.log(...args);
    };
    methodNames.forEach(wrap);
 
    return new ConsoleLogger();
};