All files / src/services logger.services.ts

54.54% Statements 18/33
37.5% Branches 3/8
33.33% Functions 2/6
51.61% Lines 16/31

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                 1x         92x   92x 1x     92x       170x 170x     170x                                                                    
import {inject} from '@loopback/core';
import {RequestContext, RestBindings} from '@loopback/rest';
import * as fs from 'fs';
import * as winston from 'winston';
 
const env = process.env.NODE_ENV
const logDir = 'logs';
export interface LoggerDefinitions<T = string> {
  winstonLogger: winston.LoggerOptions;
  createLogger(): winston.Logger;
}
const winstonConfig: {infoLog: object; errorLog: object; console: object} = {
  infoLog: {
    name: 'Info Logs',
    filename: `logs/info.log`,
    datePattern: 'YYYY-MM-DD',
    zippedArchive: false,
    maxSize: '10m',
    maxFiles: '1d',
    level: 'info',
    json: true,
    colorize: false,
  },
  errorLog: {
    name: 'Error Logs',
    filename: `logs/error.log`,
    datePattern: 'YYYY-MM-DD',
    zippedArchive: false,
    maxSize: '10m',
    maxFiles: '1d',
    level: 'warn',
    json: true,
    colorize: false,
  },
  console: {
    level: 'debug',
    handleExceptions: true,
    json: false,
    colorize: true,
  },
};
export const logger = winston.createLogger({
  format: winston.format.combine(
    winston.format.timestamp(),
    winston.format.json(),
  ),
  transports: [
    new winston.transports.Console(winstonConfig.console),
  ],
});
export class Logger {
  public logger: any;
 
  constructor(
    @inject(RestBindings.Http.CONTEXT)
    protected httpContext: RequestContext,
  ) {
    if (!fs.existsSync(logDir)) {
      fs.mkdirSync(logDir);
    }
 
    this.logger = logger;
  }
 
  info(message: any): any {
    const obj = {requestId: this.httpContext.name, ...message};
    Iif (env !== 'development') {
      this.logger.info(obj);
    }
    return;
  }
 
  error(message: any): void {
    const obj = {requestId: this.httpContext.name, ...message};
    this.logger.error(obj);
    return;
  }
 
  debug(message: any): void {
    const obj = {requestId: this.httpContext.name, ...message};
    if (env !== 'development') {
      this.logger.debug(obj);
    }
    return;
  }
 
  warn(message: any): void {
    const obj = {requestId: this.httpContext.name, ...message};
    this.logger.warn(obj);
    return;
  }
  verbose(message: any): void {
    const obj = {requestId: this.httpContext.name, ...message};
    if (env !== 'development') {
      this.logger.verbose(obj);
    }
    return;
  }
 
  // createLogger(): winston.Logger {
  //   return winston.createLogger(this.winstonLogger);
  // }
}