# Code coverage report for lib/logger.js

## Statements: 95% (19 / 20) Branches: 60% (6 / 10) Functions: 100% (2 / 2) Lines: 100% (19 / 19) Ignored: none

All files » lib/ » logger.js

    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
    411
    1
    1

    1

    1
    1
    1
    1

    1
    1
    1
    1

    1

    1
    1
    2
    2

    1
    1
     var path = require('path');
    var winston = require('winston');
    var fs = require('fs');

    var packageName = require('../package').name;

    function logger (opts) {
      var logFile = path.join(opts.dir || '.', opts.filename || packageName + '.log');
      logger.stream = fs.createWriteStream(logFile, {flags: 'a'});
      var winstonLogger = new winston.Logger({
        transports: [
          new winston.transports.File({
            stream: logger.stream,
            level: opts.level || 'info',
            handleExceptions: true,
            exitOnError: false,
            json: false,
            prettyPrint: true,
            colorize: true
          })
        ]
      });

      var levels = winston.config.npm.levels;
      Eif (levels[opts.level] < levels.info) {
        require('longjohn');
        require('bluebird').longStackTraces();
      }

      winstonLogger.extend(logger);
    }

    var _consoleError = console.error;
    console.error = function () {
      Iif (!logger.stream) return _consoleError.apply(this, arguments);
      return logger.stream.write([].join.call(arguments, ' ') + '\n');
    };

    module.exports = logger;
    global.logger = logger;