All files / src/server winston.js

91.66% Statements 11/12
50% Branches 3/6
100% Functions 2/2
90.9% Lines 10/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 625x                     5x                               19x           5x 10x               10x 10x   10x 4x                 5x            
const { Container, transports } = require('winston');
 
/**
 * Features
 * - make it easy to pass in logging config
 * - export multiple loggers
 * - export a default logger
 * - try not to be too much of a breaking change
 * - expose core container to allow for use in different implementations
 */
 
const container = new Container();
 
/**
 * @description Logging container that can be used to modify any loggers
 * availablie in the current application
 */
 
/**
 * @function get
 * @description Retrieve a logger by name, same as container.get except with a
 * default value applied to it
 * @param {String} name - Name of the logger
 * @param {Object} options - Options for the logger, this is also an alias for
 * adding a logger
 * @return {winston.logger}
 */
const get = (name = 'default', options) => container.get(name, options);
 
/**
 * @function initialize
 * @description Initialize a default console logger
 */
const initialize = (config = {}) => {
  let transport = new transports.Console({
    level: config.level,
    timestamp: true,
    colorize: true,
  });
  // If we already have a logger by the provided default name, make sure it
  // has a console transport added. This can happen when someone accesses the
  // logger before calling initialize
  if (container.has('default')) {
    let logger = container.get('default');
    // Only add the console logger if none is present, technically
    if (logger.transports.length === 0) {
      logger.add(transport);
    }
  } else E{
    container.add('default', {
      transports: [transport],
    });
  }
};
 
module.exports = {
  container,
  get,
  initialize,
  transports,
};