all files / app/lib/ logger.js

88.89% Statements 32/36
64.29% Branches 9/14
100% Functions 8/8
91.43% Lines 32/35
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 8017× 17× 17×       17× 502× 502×   502×       17×           497× 497×                         17×     497×                                           17× 17×  
var winston = require('winston')
var winstonCommon = require('winston/lib/winston/common')
var logger = new (winston.Logger)({
  transports: [], exitOnError: false
})
 
winstonCommon.setLevels = function (target) {
  Object.keys(target.levels).forEach(function (level) {
    target[ level ] = function () {
      var args = Array.prototype.slice.call(arguments)
      args.splice(0, 0, level)
      // console.log.apply(console,arguments)
      target.log.apply(target, args)
    }
  })
}
winston.transports.File.prototype.reopen = function (callback) {
  Eif (this.opening) {
    //
    // If we are already attempting to open the next
    // available file then respond with a value indicating
    // that the message should be buffered.
    //
    var that = this
    this.on('flush', function () {
      that._createStream()
      return callback(true)
    })
  } else {
    //
    // If we don't have a stream or have exceeded our size, then create
    // the next stream and respond with a value indicating that
    // the message should be buffered.
    //
    this._createStream()
    return callback(true)
  }
}
 
var yaktorInit = function (yaktor) {
  Iif (logger.initialized) return logger
 
  process.on('SIGHUP', function () {
    logger.info('caught SIGHUP')
    Eif (yaktor.log && yaktor.log.filename) {
      logger.info('rotate logs')
      var fileTrans = logger.transports[ winston.transports.File.prototype.name ]
 
      fileTrans.reopen(function () {
        logger.info('done rotating')
      })
    }
  })
 
  Eif (yaktor.log.filename && yaktor.log.filename) {
    logger.add(winston.transports.File, {
      level: yaktor.log.level,
      colorize: false,
      timestamp: true,
      json: false,
      filename: yaktor.log.filename
    })
  }
  Iif (yaktor.log.stdout) {
    logger.add(winston.transports.Console, {
      level: yaktor.log.level,
      colorize: true,
      timestamp: true
    })
  }
  logger.setLevels(logger.levels)
 
  logger.initialized = true
 
  return logger
}
 
logger.yaktorInit = yaktorInit
module.exports = logger