All files index.ts

72.34% Statements 34/47
60% Branches 12/20
100% Functions 3/3
73.91% Lines 34/46

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 92 93 94 95 96 97 98 99 100 101 102 103 1041x 1x 1x 1x   1x 1x 1x     1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 2x   4x                                 1x     2x 2x     2x 2x   2x       2x 2x 4x   2x                   2x 2x 2x                                               2x       2x            
import { ElasticsearchTransport, ElasticsearchTransportOptions } from 'winston-elasticsearch';
import * as winstonESTransformer from '@restorecommerce/winston-elasticsearch-transformer';
import * as rTracer from 'cls-rtracer';
import { WinstonLoggerOptions, Transports, format, WinstonLogger, TransportStreamArray, log } from './winston';
 
const mappingTemplate = winstonESTransformer.mappingTemplate;
const transformer = winstonESTransformer.transformer;
const { timestamp, printf } = format;
 
// a custom format that outputs request id
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} [rid:${rid}] : ${message} ${((object))}`
    : `${level} : ${time} : ${message} ${(object)}`;
});
 
export interface RestoreLoggerOptions extends WinstonLoggerOptions {
  console?: Transports.ConsoleTransportInstance & {
    // Custom console opts here
  };
  file?: Transports.FileTransportOptions & {
    // Custom file opts here
  };
  elasticsearch?: ElasticsearchTransportOptions & {
    source?: any;
  };
}
 
export class Logger extends WinstonLogger {
  constructor(opts: RestoreLoggerOptions) {
    // Set up logging
    (log as any).namespaces = true;
    Iif (!opts) throw new Error('Options are missing');
 
    // Provide TransportStream array and add opts.transports
    let transports: TransportStreamArray = [];
    Iif (opts.transports && !Array.isArray(transports)) {
      transports = [transports];
    } else Iif (Array.isArray(opts.transports)) {
      transports = opts.transports;
    }
 
    let transportsCount = 0;
    Object.keys(opts).forEach((transport) => {
      switch (transport) {
        case 'console': {
          const consoleOpts = {
            ...opts[transport],
            format: format.combine(
              format.colorize(),
              format.simple(),
              timestamp(),
              rTracerFormat
            )
          };
 
          transportsCount++;
          transports.push(new Transports.Console(consoleOpts));
          break;
        }
        case 'file': {
          transportsCount++;
          transports.push(new Transports.File(opts[transport]));
          break;
        }
        case 'elasticsearch': {
          transportsCount++;
          const elasticsearchOpts = {
            mappingTemplate,
            ...opts[transport],
          };
 
          transformer.source = opts.elasticsearch.source;
          elasticsearchOpts.transformer = transformer;
          transports.push(new ElasticsearchTransport(elasticsearchOpts));
          break;
        }
        default:
        // ignore
      }
    });
 
    Iif (transportsCount <= 0) {
      throw new Error('Provide at least one supported transport');
    }
 
    super({
      ...opts,
      transports
    });
  }
}