package import { BaseService , ConfigurationService } from "@triptyk/nfw-core" ; import * as Moment from "moment-timezone" ; import { autoInjectable , singleton } from "tsyringe" ; import * as Winston from "winston" ; import { Environments } from "../enums/environments.enum" ; @ singleton ( ) @ autoInjectable ( ) export class LoggerService extends BaseService { private _logger : Winston . Logger ; public constructor ( private configurationService ) { super ( ) ; const { env } = configurationService . config ; const directory = env === Environments . Test ? "test" : "dist" ; const timestampFnc = ( ) => Moment ( ) . format ( "YYYY-MM-DD HH:mm:ss.SSS" ) ; const timestampFormatJSON = Winston . format . printf ( ( { level , message , label } ) => JSON . stringify ( { label , level , message , timestamp : timestampFnc ( ) } ) ) ; const timestampFormatText = Winston . format . printf ( ( { level , message , label } ) => `${ timestampFnc ( ) } [${ label }] ${ level }: ${ message }` ) ; this . _logger = Winston . createLogger ( { format : timestampFormatJSON , level : "info" , transports : [ new Winston . transports . File ( { filename : `${ directory }/logs/error.log` , level : "error" } ) , new Winston . transports . File ( { filename : `${ directory }/logs/combined.log` } ) ] } ) ; if ( env !== Environments . Production ) { this . logger . add ( new Winston . transports . Console ( { format : Winston . format . combine ( Winston . format . label ( { label : env } ) , timestampFormatText ) } ) ) ; } if ( ! process . env . CLI ) { this . logger . stream ( { write : ( message ) => { this . logger . info ( message . trim ( ) ) ; } } ) ; } } public get logger ( ) { return this . _logger ; } public init ( ) { return true ; } }