1
1
import { Inject , Injectable , Scope } from '@nestjs/common' ;
2
2
import { INQUIRER } from '@nestjs/core' ;
3
- import Logger , {
4
- LoggerBaseKey
5
- } from '@credebl/logger/logger.interface' ;
3
+ import Logger , { LoggerBaseKey } from '@credebl/logger/logger.interface' ;
6
4
import { LogData , LogLevel } from '@credebl/logger/log' ;
7
5
import { ConfigService } from '@nestjs/config' ;
8
- import ContextStorageService , {
9
- ContextStorageServiceKey
10
- } from '@credebl/context/contextStorageService.interface' ;
6
+ import ContextStorageService , { ContextStorageServiceKey } from '@credebl/context/contextStorageService.interface' ;
11
7
import { MICRO_SERVICE_NAME } from '@credebl/common/common.constant' ;
8
+ import { otelLogger } from '../../../apps/api-gateway/src/tracer' ;
12
9
13
10
@Injectable ( { scope : Scope . TRANSIENT } )
14
11
export default class LoggerService implements Logger {
@@ -25,45 +22,92 @@ export default class LoggerService implements Logger {
25
22
private readonly contextStorageService : ContextStorageService ,
26
23
@Inject ( MICRO_SERVICE_NAME ) private readonly microserviceName : string
27
24
) {
28
- // Set the source class from the parent class
29
25
this . sourceClass = parentClass ?. constructor ?. name ;
30
- // Set the organization, context and app from the environment variables
31
26
this . organization = configService . get < string > ( 'ORGANIZATION' ) ;
32
27
this . context = configService . get < string > ( 'CONTEXT' ) ;
33
28
this . app = configService . get < string > ( 'APP' ) ;
34
29
}
35
30
36
- public log (
37
- level : LogLevel ,
38
- message : string | Error ,
39
- data ?: LogData ,
40
- profile ?: string ,
41
- ) : void {
42
- return this . logger . log ( level , message , this . getLogData ( data ) , profile ) ;
31
+ public log ( level : LogLevel , message : string | Error , data ?: LogData , profile ?: string ) : void {
32
+ this . emitToOtel ( level , message , data ) ;
33
+ this . logger . log ( level , message , this . getLogData ( data ) , profile ) ;
43
34
}
44
35
45
- public debug ( message : string , data ?: LogData , profile ?: string ) : void {
46
- return this . logger . debug ( message , this . getLogData ( data ) , profile ) ;
36
+ public debug ( message : string , data ?: LogData , profile ?: string ) : void {
37
+ this . emitToOtel ( 'DEBUG' , message , data ) ;
38
+ this . logger . debug ( message , this . getLogData ( data ) , profile ) ;
47
39
}
48
40
49
- public info ( message : string , data ?: LogData , profile ?: string ) : void {
50
- return this . logger . info ( message , this . getLogData ( data ) , profile ) ;
41
+ public info ( message : string , data ?: LogData , profile ?: string ) : void {
42
+ this . emitToOtel ( 'INFO' , message , data ) ;
43
+ this . logger . info ( message , this . getLogData ( data ) , profile ) ;
51
44
}
52
45
53
- public warn ( message : string | Error , data ?: LogData , profile ?: string ) : void {
54
- return this . logger . warn ( message , this . getLogData ( data ) , profile ) ;
46
+ public warn ( message : string | Error , data ?: LogData , profile ?: string ) : void {
47
+ this . emitToOtel ( 'WARN' , message , data ) ;
48
+ this . logger . warn ( message , this . getLogData ( data ) , profile ) ;
55
49
}
56
50
57
- public error ( message : string | Error , data ?: LogData , profile ?: string ) : void {
58
- return this . logger . error ( message , this . getLogData ( data ) , profile ) ;
51
+ public error ( message : string | Error , data ?: LogData , profile ?: string ) : void {
52
+ this . emitToOtel ( 'ERROR' , message , data ) ;
53
+ this . logger . error ( message , this . getLogData ( data ) , profile ) ;
59
54
}
60
55
61
- public fatal ( message : string | Error , data ?: LogData , profile ?: string ) : void {
62
- return this . logger . fatal ( message , this . getLogData ( data ) , profile ) ;
56
+ public fatal ( message : string | Error , data ?: LogData , profile ?: string ) : void {
57
+ this . emitToOtel ( 'FATAL' , message , data ) ;
58
+ this . logger . fatal ( message , this . getLogData ( data ) , profile ) ;
63
59
}
64
60
65
- public emergency ( message : string | Error , data ?: LogData , profile ?: string ) : void {
66
- return this . logger . emergency ( message , this . getLogData ( data ) , profile ) ;
61
+ public emergency ( message : string | Error , data ?: LogData , profile ?: string ) : void {
62
+ this . emitToOtel ( 'EMERGENCY' , message , data ) ;
63
+ this . logger . emergency ( message , this . getLogData ( data ) , profile ) ;
64
+ }
65
+
66
+ public startProfile ( id : string ) : void {
67
+ this . logger . startProfile ( id ) ;
68
+ }
69
+
70
+ private emitToOtel ( severityText : string , message : string | Error , data ?: LogData ) : void {
71
+ try {
72
+ if ( ! otelLogger ) {
73
+ return ;
74
+ }
75
+ const correlationId = data ?. correlationId || this . contextStorageService . getContextId ( ) ;
76
+
77
+ const attributes = {
78
+ app : data ?. app || this . app ,
79
+ organization : data ?. organization || this . organization ,
80
+ context : data ?. context || this . context ,
81
+ sourceClass : data ?. sourceClass || this . sourceClass ,
82
+ correlationId,
83
+ microservice : this . microserviceName ,
84
+ ...( data ?? { } )
85
+ } ;
86
+
87
+ if ( data ?. error ) {
88
+ attributes . error =
89
+ 'string' === typeof data . error
90
+ ? data . error
91
+ : data . error instanceof Error
92
+ ? {
93
+ name : data . error . name ,
94
+ message : data . error . message ,
95
+ stack : data . error . stack
96
+ }
97
+ : 'object' === typeof data . error
98
+ ? JSON . parse ( JSON . stringify ( data . error ) )
99
+ : String ( data . error ) ;
100
+ }
101
+
102
+ otelLogger . emit ( {
103
+ body : `${ correlationId } ${ 'string' === typeof message ? message : message . message } ` ,
104
+ severityText,
105
+ attributes
106
+ } ) ;
107
+ } catch ( err ) {
108
+ // eslint-disable-next-line no-console
109
+ console . error ( 'Failed to emit log to OpenTelemetry:' , err ) ;
110
+ }
67
111
}
68
112
69
113
private getLogData ( data ?: LogData ) : LogData {
@@ -73,12 +117,7 @@ export default class LoggerService implements Logger {
73
117
context : data ?. context || this . context ,
74
118
app : data ?. app || this . app ,
75
119
sourceClass : data ?. sourceClass || this . sourceClass ,
76
- correlationId :
77
- data ?. correlationId || this . contextStorageService . getContextId ( )
120
+ correlationId : data ?. correlationId || this . contextStorageService . getContextId ( )
78
121
} ;
79
122
}
80
-
81
- public startProfile ( id : string ) : void {
82
- this . logger . startProfile ( id ) ;
83
- }
84
123
}
0 commit comments