import { Injectable , LoggerService , Optional } from '@nestjs/common' ; import { Ogma } from '@ogma/logger' ; import { InjectOgma , InjectOgmaContext } from './decorators' ; import { OgmaServiceMeta } from './interfaces' ; import { RequestContext } from './interfaces/request-context.interface' ; @ Injectable ( ) export class OgmaService implements LoggerService { private readonly context ? : string ; private readonly ogma : Ogma ; param message param context public fine = this . verbose ; param message param context public log = this . info ; constructor ( @ InjectOgma ( ) ogma ? , @ Optional ( ) @ InjectOgmaContext ( ) context ? , @ Optional ( ) private readonly requestContext ? ) { this . context = context || '' ; this . ogma = ogma ?? new Ogma ( ) ; } private getRequestId ( requestContext ) { if ( typeof requestContext . getContext !== 'undefined' ) { return requestContext . getContext ( ) . requestId ; } return requestContext . requestId ; } param message param context param requestId public info ( message , meta ? ) { if ( typeof meta === 'string' ) { meta = { context : meta } ; } this . printMessage ( message , 'info' , meta ) ; } param message param meta param context public error ( message , trace = '' , context = { } ) { let meta = { } ; if ( typeof trace === 'object' ) { meta = trace ; trace = '' ; } if ( context && typeof context === 'string' ) { meta . context = context ; } else if ( context && typeof context === 'object' ) { meta = context ; } this . printMessage ( message , 'error' , meta ) ; if ( trace ) { this . printMessage ( trace , 'error' , meta ) ; } } param message param context param requestId public warn ( message , meta ? ) { if ( typeof meta === 'string' ) { meta = { context : meta } ; } this . printMessage ( message , 'warn' , meta ) ; } param message param context param requestId public debug ( message , meta ? ) { if ( typeof meta === 'string' ) { meta = { context : meta } ; } this . printMessage ( message , 'debug' , meta ) ; } param message param context param requestId public fatal ( message , meta ? ) { if ( typeof meta === 'string' ) { meta = { context : meta } ; } this . printMessage ( message , 'fatal' , meta ) ; } param message param context param requestId public silly ( message , meta ? ) { if ( typeof meta === 'string' ) { meta = { context : meta } ; } this . printMessage ( message , 'silly' , meta ) ; } param message param context param requestId public verbose ( message , meta ? ) { if ( typeof meta === 'string' ) { meta = { context : meta } ; } this . printMessage ( message , 'verbose' , meta ) ; } param error param context param requestId public printError ( error , meta = { } ) { if ( ! meta . correlationId && this . requestContext && this . context ) { meta . correlationId = this . getRequestId ( this . requestContext ) ; } meta . context = meta . context ?? this . context ; this . ogma . printError ( error , meta ) ; } private printMessage ( message , levelString , meta = { } ) { meta . context = meta . context ?? this . context ; if ( ! meta . correlationId && this . requestContext && this . context ) { meta . correlationId = this . getRequestId ( this . requestContext ) ; } this . ogma [ levelString ] ( message , meta ) ; } }