All files index.js

73.33% Statements 33/45
64.29% Branches 9/14
100% Functions 3/3
75% Lines 33/44

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 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    1x 1x   1x 1x 1x   1x 1x 1x       1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 3x   5x                           1x     1x 1x 1x 1x 2x   1x 1x               1x 1x                                         1x       1x       1x  
'use strict';
 
const winston = require('winston');
const Elasticsearch = require('winston-elasticsearch');
const elasticsearchTransport =
  require('@restorecommerce/winston-elasticsearch-transformer');
const { format } = require('winston');
const rTracer = require('cls-rtracer');
 
const mappingTemplate = elasticsearchTransport.mappingTemplate;
const transformer = elasticsearchTransport.transformer;
const { timestamp, printf } = format;
 
// a custom format that outputs request id
/* eslint-disable no-param-reassign */
const rTracerFormat = printf((info) => {
  const rid = rTracer.id();
  const time = info.timestamp;
  const level = info.level;
  const message = info.message;
  delete info.timestamp;
  delete info.level;
  delete info.message;
  let object = '';
  if (Object.entries(info).length !== 0 && info.constructor === Object) {
    object = JSON.stringify(info);
  }
  return rid
    ? `${level} : ${time} [request-id:${rid}] : ${message} ${((object))}`
    : `${level} : ${time} : ${message} ${(object)}`;
});
 
/**
 Logger
 @class
 @classdesc Logger wraps the winston logger with Restore specifics
 @param {Object} [opts] - configuration object with transport specific
options
 */
class Logger {
  constructor(opts) {
    Iif (!opts) throw new Error('Options are missing');
 
    // Set up logging
    winston.log.namespaces = true;
    let transportsCount = 0;
    const transports = [];
    Object.keys(opts).forEach((transport) => {
      switch (transport) {
        case 'console': {
          transportsCount += 1;
          const consoleOpts = Object.assign({}, opts[transport], {
            format: winston.format.combine(
              winston.format.colorize(),
              winston.format.simple(),
              timestamp(),
              rTracerFormat
            )
          });
          transports.push(new (winston.transports.Console)(consoleOpts));
          break;
        }
        case 'file': {
          transportsCount += 1;
          transports.push(new (winston.transports.File)(opts[transport]));
          break;
        }
        case 'elasticsearch': {
          transportsCount += 1;
          const esTransportOpts = opts.elasticsearch;
          esTransportOpts.mappingTemplate = mappingTemplate;
          transformer.source = opts.elasticsearch.source;
          esTransportOpts.transformer = transformer;
          transports.push(new Elasticsearch(esTransportOpts));
          break;
        }
        default:
        // ignore
      }
    });
 
    Iif (transportsCount <= 0) {
      throw new Error('Provide at least one supported transport');
    }
 
    return winston.createLogger({ transports });
  }
}
 
module.exports = Logger;