import { VError } from 'verror' ; import * as utils from 'util' ; import { LoggerClient , LogLevel } from './logger-client' ; import { serializeError as serializeErrorExt } from 'serialize-error' ; export class Logger { protected initialized = false ; protected isDebugEnabled = false ; constructor ( protected readonly loggerClients , protected readonly currentProcess ) { } public async setup ( baseProperties ? ) { if ( this . initialized === true ) { return ; } await Promise . all ( this . loggerClients . map ( async ( client ) => { await client . setup ( baseProperties ) ; } ) ) ; this . isDebugEnabled = /--debug|--inspect/i . test ( this . currentProcess . execArgv . join ( ' ' ) ) ; this . initialized = true ; } public log ( message , logLevel , properties ? ) { this . ensureInitialized ( ) ; this . invokeLoggerClient ( ( client ) => client . log ( message , logLevel , properties ) ) ; } public logInfo ( message , properties ? ) { this . log ( message , LogLevel . info , properties ) ; } public logVerbose ( message , properties ? ) { if ( this . isDebugEnabled ) { this . log ( message , LogLevel . verbose , properties ) ; } } public logWarn ( message , properties ? ) { this . log ( message , LogLevel . warn , properties ) ; } public logError ( message , properties ? ) { this . log ( message , LogLevel . error , properties ) ; } public trackException ( error ) { this . ensureInitialized ( ) ; this . invokeLoggerClient ( ( client ) => client . trackException ( error ) ) ; } public trackExceptionAny ( underlyingErrorData , message ) { const parsedErrorObject = underlyingErrorData instanceof Error ? underlyingErrorData : new Error ( this . serializeError ( underlyingErrorData ) ) ; this . trackException ( new VError ( parsedErrorObject , message ) ) ; } public serializeError ( error ) { return utils . inspect ( serializeErrorExt ( error ) , false , null ) ; } private invokeLoggerClient ( action ) { this . loggerClients . forEach ( ( client ) => { action ( client ) ; } ) ; } private ensureInitialized ( ) { if ( this . initialized === true ) { return ; } throw new Error ( 'The logger instance is not initialized. Ensure the setup() method is invoked by derived class implementation.' ) ; } }