import { ConsoleLogger , ConsoleLoggerOptions , Injectable , Scope } from '@nestjs/common' ; import { RequestContext } from './request-context.service' ; /**
 * https://docs.nestjs.com/techniques/logger
 * To disable color in the default logger's messages, set the `NO_COLOR` environment variable to some non-empty string.
 */ @ Injectable ( { scope : Scope . TRANSIENT } ) export class Logger extends ConsoleLogger { protected isProduction : boolean = process . env . NODE_ENV === 'production' ; protected override options : ConsoleLoggerOptions = { logLevels : [ 'log' , 'error' , 'warn' , 'debug' , 'verbose' ] , timestamp : ! this . isProduction , } ; constructor ( private req , context ) { super ( context ) ; } private get reqContext ( ) : string { return this . req . context ?. id || '' ; } public override log ( message , ... optionalParams ) { super . log ( message , ... this . parseContext ( optionalParams ) ) ; } public override warn ( message , ... optionalParams ) { super . warn ( message , ... this . parseContext ( optionalParams ) ) ; } public override error ( message , ... optionalParams ) { super . error ( message , ... this . parseContext ( optionalParams ) ) ; } protected override getTimestamp ( ) { return super . getTimestamp ( ) ; } private parseContext ( params ) { if ( this . reqContext ) { let context = this . reqContext ; if ( this . context ) { context += `] [${ this . context }` ; } params . push ( context ) ; } return params ; } }