1
+ /* eslint-disable no-console */
1
2
import { NestInterceptor , RequestMethod } from '@nestjs/common' ;
2
3
import { getLoggerToken , Logger , LoggerErrorInterceptor , LoggerModule , Params , PinoLogger } from 'nestjs-pino' ;
3
4
import { storage , Store } from 'nestjs-pino/storage' ;
@@ -10,135 +11,85 @@ export function getErrorInterceptor(): NestInterceptor {
10
11
}
11
12
export { Logger , LoggerModule , PinoLogger , storage , Store , getLoggerToken } ;
12
13
13
- const loggingLevelArr = [ 'error' , 'warn' , 'info' , 'verbose' , 'debug' ] ;
14
-
15
14
const loggingLevelSet = {
16
- error : 50 ,
17
- warn : 40 ,
15
+ trace : 10 ,
16
+ debug : 20 ,
18
17
info : 30 ,
19
- verbose : 20 ,
20
- debug : 10 ,
18
+ warn : 40 ,
19
+ error : 50 ,
20
+ fatal : 60 ,
21
+ none : 70 ,
21
22
} ;
22
-
23
- interface ILoggingVariables {
24
- env : string ;
25
- level : string ;
26
-
27
- hostingPlatform : string ;
28
- tenant : string ;
29
- }
23
+ const loggingLevelArr = Object . keys ( loggingLevelSet ) ;
30
24
31
25
export function getLogLevel ( ) {
32
- let logLevel = process . env . LOGGING_LEVEL ?? 'info' ;
33
-
34
- if ( loggingLevelArr . indexOf ( logLevel ) === - 1 ) {
35
- // eslint-disable-next-line no-console
36
- console . log ( `${ logLevel } is not a valid log level of ${ loggingLevelArr } . Reverting to info.` ) ;
26
+ let logLevel = null ;
37
27
28
+ if ( process . env . LOGGING_LEVEL || process . env . LOG_LEVEL ) {
29
+ logLevel = process . env . LOGGING_LEVEL || process . env . LOG_LEVEL ;
30
+ } else {
31
+ console . log ( `Environment variable LOG_LEVEL is not set. Falling back to info level.` ) ;
38
32
logLevel = 'info' ;
39
33
}
40
- // eslint-disable-next-line no-console
41
- console . log ( `Log Level Chosen: ${ logLevel } ` ) ;
42
-
43
- return logLevel ;
44
- }
45
-
46
- // TODO: should be moved into a config framework
47
- function getLoggingVariables ( ) : ILoggingVariables {
48
- const env = process . env . NODE_ENV ?? 'local' ;
49
-
50
- // eslint-disable-next-line no-console
51
- console . log ( `Environment: ${ env } ` ) ;
52
-
53
- const hostingPlatform = process . env . HOSTING_PLATFORM ?? 'Docker' ;
54
-
55
- // eslint-disable-next-line no-console
56
- console . log ( `Platform: ${ hostingPlatform } ` ) ;
57
34
58
- const tenant = process . env . TENANT ?? 'OS' ;
35
+ if ( ! loggingLevelArr . includes ( logLevel ) ) {
36
+ console . log ( `${ logLevel } is not a valid log level of ${ loggingLevelArr } . Falling back to info level.` ) ;
59
37
60
- // eslint-disable-next-line no-console
61
- console . log ( `Tenant: ${ tenant } ` ) ;
38
+ return 'info' ;
39
+ }
62
40
63
- return {
64
- env,
65
- level : getLogLevel ( ) ,
66
- hostingPlatform,
67
- tenant,
68
- } ;
41
+ return logLevel ;
69
42
}
70
43
71
44
export function createNestLoggingModuleOptions ( settings : ILoggerSettings ) : Params {
72
- const values : ILoggingVariables = getLoggingVariables ( ) ;
73
-
74
- let redactFields : string [ ] = sensitiveFields . map ( ( val ) => val ) ;
75
-
45
+ let redactFields : string [ ] = sensitiveFields ;
76
46
redactFields . push ( 'req.headers.authorization' ) ;
77
-
78
47
const baseWildCards = '*.' ;
79
48
const baseArrayWildCards = '*[*].' ;
80
49
for ( let i = 1 ; i <= 6 ; i += 1 ) {
81
50
redactFields = redactFields . concat ( sensitiveFields . map ( ( val ) => baseWildCards . repeat ( i ) + val ) ) ;
82
-
83
51
redactFields = redactFields . concat ( sensitiveFields . map ( ( val ) => baseArrayWildCards . repeat ( i ) + val ) ) ;
84
52
}
85
53
86
- const transport = [ 'local' , 'test' , 'debug' ] . includes ( process . env . NODE_ENV ) ? { target : 'pino-pretty' } : undefined ;
87
-
88
- // eslint-disable-next-line no-console
89
- console . log ( loggingLevelSet ) ;
90
-
91
- // eslint-disable-next-line no-console
92
- console . log ( `Selected Log Transport ${ ! transport ? 'None' : 'pino-pretty' } ` , loggingLevelSet ) ;
54
+ const configSet = {
55
+ transport : [ 'local' , 'test' , 'debug' ] . includes ( process . env . NODE_ENV ) ? { target : 'pino-pretty' } : undefined ,
56
+ platform : process . env . HOSTING_PLATFORM ?? 'Docker' ,
57
+ tenant : process . env . TENANT ?? 'OS' ,
58
+ level : getLogLevel ( ) ,
59
+ levels : loggingLevelSet ,
60
+ } ;
61
+ console . log ( 'Logging Configuration:' , {
62
+ level : configSet . level ,
63
+ environment : process . env . NODE_ENV ,
64
+ transport : ! configSet . transport ? 'None' : 'pino-pretty' ,
65
+ platform : configSet . platform ,
66
+ tenant : configSet . tenant ,
67
+ levels : JSON . stringify ( configSet . levels ) ,
68
+ } ) ;
93
69
94
70
return {
95
71
exclude : [ { path : '*/health-check' , method : RequestMethod . GET } ] ,
72
+ assignResponse : true ,
96
73
pinoHttp : {
97
- customLevels : loggingLevelSet ,
98
- level : values . level ,
74
+ useOnlyCustomLevels : true ,
75
+ customLevels : configSet . levels ,
76
+ level : configSet . level ,
99
77
redact : {
100
78
paths : redactFields ,
101
- censor : customRedaction ,
102
79
} ,
103
80
base : {
104
81
pid : process . pid ,
105
82
serviceName : settings . serviceName ,
106
83
serviceVersion : settings . version ,
107
- platform : values . hostingPlatform ,
108
- tenant : values . tenant ,
84
+ platform : configSet . platform ,
85
+ tenant : configSet . tenant ,
109
86
} ,
110
- transport,
111
- autoLogging : ! [ 'test' , 'local' ] . includes ( process . env . NODE_ENV ) ,
112
- /**
113
- * These custom props are only added to 'request completed' and 'request errored' logs.
114
- * Logs generated during request processing won't have these props by default.
115
- * To include these or any other custom props in mid-request logs,
116
- * use `PinoLogger.assign(<props>)` explicitly before logging.
117
- */
118
- customProps : ( req : any , res : any ) => ( {
119
- user : {
120
- userId : req ?. user ?. _id || null ,
121
- environmentId : req ?. user ?. environmentId || null ,
122
- organizationId : req ?. user ?. organizationId || null ,
123
- } ,
124
- authScheme : req ?. authScheme ,
125
- rateLimitPolicy : res ?. rateLimitPolicy ,
126
- } ) ,
87
+ transport : configSet . transport ,
88
+ autoLogging : ! [ 'test' ] . includes ( process . env . NODE_ENV ) ,
127
89
} ,
128
90
} ;
129
91
}
130
92
131
- const customRedaction = ( value : any , path : string [ ] ) => {
132
- /*
133
- * Logger.
134
- * if (obj.email && typeof obj.email === 'string') {
135
- * obj.email = '[REDACTED]';
136
- * }
137
- *
138
- * return JSON.parse(JSON.stringify(obj));
139
- */
140
- } ;
141
-
142
93
interface ILoggerSettings {
143
94
serviceName : string ;
144
95
version : string ;
0 commit comments