Code coverage report for http-proxy-middleware\lib\logger.js

# all files / http-proxy-middleware/lib/ logger.js

100% Statements 70/70
94.74% Branches 36/38
100% Functions 16/16
100% Lines 64/64

    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
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    1591×
    1×

    1×

    1×

    1×

    1×

    37×
    1×

    37×

    1×
    1×
    1×

    1×

    33×
    32×

    35×
    34×

    1×

    1×

    1×
    1×
    1×
    1×

    1×
    35×

    1×
    105×
    28×

    1×
    134×
    119×

    1×
    45×
    35×

    1×
    38×
    33×

    1×
    322×
    322×

    322×
    215×

    322×

    1×
    250×
    250×

    250×

    1×
    35×

    35×
    1×

    34×

    1×
    33×
    33×

    33×
    1×

    32×

    1×
    4×
    4×
    4×

    4×

    4×

     var util = require('util');
    var _ = require('lodash');

    var loggerInstance;

    var defaultProvider = {
        log: console.log,
        debug: console.log,    // use .log(); since console does not have .debug()
        info: console.info,
        warn: console.warn,
        error: console.error
    };

    // log level 'weight'
    var LEVELS = {
        debug: 10,
        info: 20,
        warn: 30,
        error: 50,
        silent: 80
    };

    module.exports = {
        // singleton
        getInstance: function() {
            if (!loggerInstance) {
                loggerInstance = new Logger();
            }

            return loggerInstance;
        },
        getArrow: getArrow
    };

    function Logger() {
        var logLevel;
        var provider;

        var api = {
            log: log,
            debug: debug,
            info: info,
            warn: warn,
            error: error,
            setLevel: function(v) {
                Eif (isValidLevel(v)) {
                    logLevel = v;
                }
            },
            setProvider: function(fn) {
                Eif (fn && isValidProvider(fn)) {
                    provider = fn(defaultProvider);
                }
            }
        };

        init();

        return api;

        function init() {
            api.setLevel('info');
            api.setProvider(function() {
                return defaultProvider;
            });
        }

        // log will log messages, regardless of logLevels
        function log() {
            provider.log(_interpolate.apply(null, arguments));
        }

        function debug() {
            if (_showLevel('debug')) {
                provider.debug(_interpolate.apply(null, arguments));
            }
        }

        function info() {
            if (_showLevel('info')) {
                provider.info(_interpolate.apply(null, arguments));
            }
        }

        function warn() {
            if (_showLevel('warn')) {
                provider.warn(_interpolate.apply(null, arguments));
            }
        }

        function error() {
            if (_showLevel('error')) {
                provider.error(_interpolate.apply(null, arguments));
            }
        }

        /**
         * Decide to log or not to log, based on the log levels 'weight'
         * @param  {String}  showLevel [debug, info, warn, error, silent]
         * @return {Boolean}
         */
        function _showLevel(showLevel) {
            var result = false;
            var currentLogLevel = LEVELS[logLevel];

            if (currentLogLevel && (currentLogLevel <= LEVELS[showLevel])) {
                result = true;
            }

            return result;
        }

        // make sure logged messages and its data are return interpolated
        // make it possible for additional log data, such date/time or custom prefix.
        function _interpolate() {
            var fn = _.spread(util.format);
            var result = fn(_.slice(arguments));

            return result;
        }

        function isValidProvider(fnProvider) {
            var result = true;

            if (fnProvider && !_.isFunction(fnProvider)) {
                throw new Error('[HPM] Log provider config error. Expecting a function.');
            }

            return result;
        }

        function isValidLevel(levelName) {
            var validLevels = _.keys(LEVELS);
            var isValid = _.includes(validLevels, levelName);

            if (!isValid) {
                throw new Error('[HPM] Log level error. Invalid logLevel.');
            }

            return isValid;
        }
    }

    /**
     * -> normal proxy
     * => proxyTable
     * ~> pathRewrite
     * ≈> proxyTable + pathRewrite
     */
    function getArrow(originalPath, newPath, originalTarget, newTarget) {
        var arrow = ['>'];
        var isNewTarget = (originalTarget !== newTarget); // router
        var isNewPath = (originalPath !== newPath); // pathRewrite

        if (isNewPath && !isNewTarget) {arrow.unshift('~');} else if (!isNewPath && isNewTarget) {arrow.unshift('=');} else if (isNewPath && isNewTarget) {arrow.unshift('≈');} else {arrow.unshift('-');}

        return arrow.join('');
    }