All files / lib slf4js.js

100% Statements 6/6
100% Branches 5/5
100% Functions 2/2
100% Lines 6/6
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                                                    1x   1x   1x   9x                   9x           9x          
/*
 * Requirements:
 * (1) To be able to configure the pattern of the log/error messages.
 * (2) To be told the exact file/class/function that the log/error message came from.
 * (3) To be able to set separate log levels for separate files/classes/functions.
 * (4) To be able to use a different logger from the default console logger, as long as it implements ILogger.
 * (5) To minimize the work involved in creating new ILogger class:  just simple implementations for
 *      log, info, debug, and error--without explicitly checking the log configuration (e.g., not
 *      having to call isLogEnabled() within the new log() function).
 *
 *
 * DESIGN:
 *  Read log configuration
 *  Create slf4js singleton
 *
 *  slf4js.getLogger()
 *      If the a Logger type was specified in the configuration
 *          Create a new Logger of that type
 *      Otherwise
 *          Create a ConsoleLogger
 *      Pass the Logger instance to a new LoggingDecorator
 *      Return the LoggingDecorator
 */
'use strict';
 
 
module.exports = function(logger, logConfig, debugEnabled = true){
 
    const Decorator = require('./LoggingDecorator')(logConfig, debugEnabled);
 
    return {
        getLogger: function getLogger(that) {
            let name = (typeof that === 'string') ? that : (that.name || "");
            /*
             Definitions of the properties listed below:
             (1) subject:  the class, function, object, file, etc. being logged.
             (2) fileName:
             (3) logLevel: the minimum log level of the logger.
             (4) logEvent: the LogEvent object containing the event name (e.g., INFO),
             date/time of the event, and the location (e.g., the method
             containing the log statement.
             */
            let options = {
                subject: name,
                logLevel: null,
                logEvent: null
            };
 
            return Object.freeze(new Decorator(logger, options));
        }
    }
};