import { Injectable , LoggerService , Inject } from '@nestjs/common' ; import { Level } from 'pino' ; import { PinoLogger } from './PinoLogger' ; import { Params , PARAMS_PROVIDER_TOKEN } from './params' ; @ Injectable ( ) export class Logger implements LoggerService { private readonly contextName : string ; constructor ( protected readonly logger , @ Inject ( PARAMS_PROVIDER_TOKEN ) { renameContext } ) { this . contextName = renameContext || 'context' ; } verbose ( message , ... optionalParams ) { this . call ( 'trace' , message , ... optionalParams ) ; } debug ( message , ... optionalParams ) { this . call ( 'debug' , message , ... optionalParams ) ; } log ( message , ... optionalParams ) { this . call ( 'info' , message , ... optionalParams ) ; } warn ( message , ... optionalParams ) { this . call ( 'warn' , message , ... optionalParams ) ; } error ( message , ... optionalParams ) { this . call ( 'error' , message , ... optionalParams ) ; } private call ( level , message , ... optionalParams ) { const objArg = { } ; let params = [ ] ; if ( optionalParams . length !== 0 ) { objArg [ this . contextName ] = optionalParams [ optionalParams . length - 1 ] ; params = optionalParams . slice ( 0 , - 1 ) ; } if ( typeof message === 'object' ) { if ( message instanceof Error ) { objArg . err = message ; } else { Object . assign ( objArg , message ) ; } this . logger [ level ] ( objArg , ... params ) ; } else if ( this . isWrongExceptionsHandlerContract ( level , message , params ) ) { objArg . err = new Error ( message ) ; objArg . err . stack = params [ 0 ] ; this . logger [ level ] ( objArg ) ; } else { this . logger [ level ] ( objArg , message , ... params ) ; } } see https see https see https see https see https private isWrongExceptionsHandlerContract ( level , message , params ) { return ( level === 'error' && typeof message === 'string' && params . length === 1 && typeof params [ 0 ] === 'string' && /\n\s*at / . test ( params [ 0 ] ) ) ; } }