azify-logger 1.0.15 → 1.0.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/init.js +72 -0
- package/package.json +3 -1
- package/register.js +528 -0
package/init.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
if (process.env.AZIFY_LOGGER_DISABLE === '1') return
|
|
2
|
+
|
|
3
|
+
const Module = require('module')
|
|
4
|
+
const originalRequire = Module.prototype.require
|
|
5
|
+
|
|
6
|
+
Module.prototype.require = function(id) {
|
|
7
|
+
const result = originalRequire.call(this, id)
|
|
8
|
+
|
|
9
|
+
if (id === '@nestjs/common') {
|
|
10
|
+
if (result && result.Logger) {
|
|
11
|
+
const originalLoggerConstructor = result.Logger
|
|
12
|
+
|
|
13
|
+
function PatchedLogger(context) {
|
|
14
|
+
const instance = new originalLoggerConstructor(context)
|
|
15
|
+
|
|
16
|
+
instance.log = function(message, context) {
|
|
17
|
+
const { getRequestContext } = require('./store')
|
|
18
|
+
const ctx = getRequestContext()
|
|
19
|
+
if (ctx && ctx.traceId) {
|
|
20
|
+
return originalLoggerConstructor.prototype.log.call(this, message, context)
|
|
21
|
+
}
|
|
22
|
+
return originalLoggerConstructor.prototype.log.call(this, message, context)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
instance.error = function(message, trace, context) {
|
|
26
|
+
const { getRequestContext } = require('./store')
|
|
27
|
+
const ctx = getRequestContext()
|
|
28
|
+
if (ctx && ctx.traceId) {
|
|
29
|
+
return originalLoggerConstructor.prototype.error.call(this, message, trace, context)
|
|
30
|
+
}
|
|
31
|
+
return originalLoggerConstructor.prototype.error.call(this, message, trace, context)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
instance.warn = function(message, context) {
|
|
35
|
+
const { getRequestContext } = require('./store')
|
|
36
|
+
const ctx = getRequestContext()
|
|
37
|
+
if (ctx && ctx.traceId) {
|
|
38
|
+
return originalLoggerConstructor.prototype.warn.call(this, message, context)
|
|
39
|
+
}
|
|
40
|
+
return originalLoggerConstructor.prototype.warn.call(this, message, context)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
instance.debug = function(message, context) {
|
|
44
|
+
const { getRequestContext } = require('./store')
|
|
45
|
+
const ctx = getRequestContext()
|
|
46
|
+
if (ctx && ctx.traceId) {
|
|
47
|
+
return originalLoggerConstructor.prototype.debug.call(this, message, context)
|
|
48
|
+
}
|
|
49
|
+
return originalLoggerConstructor.prototype.debug.call(this, message, context)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
instance.verbose = function(message, context) {
|
|
53
|
+
const { getRequestContext } = require('./store')
|
|
54
|
+
const ctx = getRequestContext()
|
|
55
|
+
if (ctx && ctx.traceId) {
|
|
56
|
+
return originalLoggerConstructor.prototype.verbose.call(this, message, context)
|
|
57
|
+
}
|
|
58
|
+
return originalLoggerConstructor.prototype.verbose.call(this, message, context)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return instance
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
Object.setPrototypeOf(PatchedLogger, originalLoggerConstructor)
|
|
65
|
+
Object.assign(PatchedLogger, originalLoggerConstructor)
|
|
66
|
+
|
|
67
|
+
result.Logger = PatchedLogger
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return result
|
|
72
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "azify-logger",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.17",
|
|
4
4
|
"description": "Azify Logger Client - Centralized logging for OpenSearch",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -49,6 +49,8 @@
|
|
|
49
49
|
"files": [
|
|
50
50
|
"index.js",
|
|
51
51
|
"index.d.ts",
|
|
52
|
+
"init.js",
|
|
53
|
+
"register.js",
|
|
52
54
|
"store.js",
|
|
53
55
|
"register-otel.js",
|
|
54
56
|
"register-restify.js",
|
package/register.js
ADDED
|
@@ -0,0 +1,528 @@
|
|
|
1
|
+
if (process.env.AZIFY_LOGGER_DISABLE === '1') return
|
|
2
|
+
|
|
3
|
+
try {
|
|
4
|
+
const bunyan = require('bunyan')
|
|
5
|
+
const createBunyanStream = require('./streams/bunyan')
|
|
6
|
+
const { getRequestContext } = require('./store')
|
|
7
|
+
|
|
8
|
+
const originalCreate = bunyan.createLogger
|
|
9
|
+
bunyan.createLogger = function patchedCreateLogger(options) {
|
|
10
|
+
const logger = originalCreate.call(bunyan, options)
|
|
11
|
+
try {
|
|
12
|
+
const level = process.env.AZIFY_LOG_LEVEL || (options && options.level) || 'info'
|
|
13
|
+
const loggerUrl = process.env.AZIFY_LOGGER_URL || 'http://localhost:3001'
|
|
14
|
+
const serviceName = process.env.APP_NAME || (options && options.name) || 'app'
|
|
15
|
+
const environment = process.env.NODE_ENV || 'development'
|
|
16
|
+
|
|
17
|
+
logger.addStream({
|
|
18
|
+
level,
|
|
19
|
+
type: 'raw',
|
|
20
|
+
stream: createBunyanStream({ loggerUrl, serviceName, environment })
|
|
21
|
+
})
|
|
22
|
+
} catch (_) {}
|
|
23
|
+
return logger
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
try {
|
|
27
|
+
const Module = require('module')
|
|
28
|
+
const originalRequire = Module.prototype.require
|
|
29
|
+
const { getRequestContext } = require('./store')
|
|
30
|
+
|
|
31
|
+
Module.prototype.require = function(id) {
|
|
32
|
+
const result = originalRequire.call(this, id)
|
|
33
|
+
if (id === 'pino' || id.endsWith('/pino')) {
|
|
34
|
+
if (result && typeof result === 'function') {
|
|
35
|
+
const originalPino = result
|
|
36
|
+
|
|
37
|
+
const patchedPino = function(options, stream) {
|
|
38
|
+
const logger = originalPino(options, stream)
|
|
39
|
+
|
|
40
|
+
const originalInfo = logger.info
|
|
41
|
+
const originalError = logger.error
|
|
42
|
+
const originalWarn = logger.warn
|
|
43
|
+
const originalDebug = logger.debug
|
|
44
|
+
const originalFatal = logger.fatal
|
|
45
|
+
const originalTrace = logger.trace
|
|
46
|
+
|
|
47
|
+
logger.info = function(obj, msg, ...args) {
|
|
48
|
+
const ctx = getRequestContext()
|
|
49
|
+
if (ctx && ctx.traceId) {
|
|
50
|
+
if (typeof obj === 'object' && obj !== null) {
|
|
51
|
+
obj.traceId = ctx.traceId
|
|
52
|
+
obj.spanId = ctx.spanId
|
|
53
|
+
obj.parentSpanId = ctx.parentSpanId
|
|
54
|
+
obj.requestId = ctx.requestId
|
|
55
|
+
} else {
|
|
56
|
+
obj = { traceId: ctx.traceId, spanId: ctx.spanId, parentSpanId: ctx.parentSpanId, requestId: ctx.requestId, msg: obj }
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
// else {
|
|
60
|
+
// const messageStr = typeof obj === 'string' ? obj : (typeof msg === 'string' ? msg : '')
|
|
61
|
+
// const traceMatch = messageStr.match(/\[([a-f0-9-]{8}-[a-f0-9-]{4}-[a-f0-9-]{4}-[a-f0-9-]{4}-[a-f0-9-]{12})\]/)
|
|
62
|
+
// if (traceMatch) {
|
|
63
|
+
// const extractedTraceId = traceMatch[1]
|
|
64
|
+
// if (typeof obj === 'object' && obj !== null) {
|
|
65
|
+
// obj.traceId = extractedTraceId
|
|
66
|
+
// } else {
|
|
67
|
+
// obj = { traceId: extractedTraceId, msg: obj }
|
|
68
|
+
// }
|
|
69
|
+
// }
|
|
70
|
+
// }
|
|
71
|
+
return originalInfo.call(this, obj, msg, ...args)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
logger.error = function(obj, msg, ...args) {
|
|
75
|
+
const ctx = getRequestContext()
|
|
76
|
+
if (ctx && ctx.traceId) {
|
|
77
|
+
if (typeof obj === 'object' && obj !== null) {
|
|
78
|
+
obj.traceId = ctx.traceId
|
|
79
|
+
obj.spanId = ctx.spanId
|
|
80
|
+
obj.parentSpanId = ctx.parentSpanId
|
|
81
|
+
obj.requestId = ctx.requestId
|
|
82
|
+
} else {
|
|
83
|
+
obj = { traceId: ctx.traceId, spanId: ctx.spanId, parentSpanId: ctx.parentSpanId, requestId: ctx.requestId, msg: obj }
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
// else {
|
|
87
|
+
// const messageStr = typeof obj === 'string' ? obj : (typeof msg === 'string' ? msg : '')
|
|
88
|
+
// const traceMatch = messageStr.match(/\[([a-f0-9-]{8}-[a-f0-9-]{4}-[a-f0-9-]{4}-[a-f0-9-]{4}-[a-f0-9-]{12})\]/)
|
|
89
|
+
// if (traceMatch) {
|
|
90
|
+
// const extractedTraceId = traceMatch[1]
|
|
91
|
+
// if (typeof obj === 'object' && obj !== null) {
|
|
92
|
+
// obj.traceId = extractedTraceId
|
|
93
|
+
// } else {
|
|
94
|
+
// obj = { traceId: extractedTraceId, msg: obj }
|
|
95
|
+
// }
|
|
96
|
+
// }
|
|
97
|
+
// }
|
|
98
|
+
return originalError.call(this, obj, msg, ...args)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
logger.warn = function(obj, msg, ...args) {
|
|
102
|
+
const ctx = getRequestContext()
|
|
103
|
+
if (ctx && ctx.traceId) {
|
|
104
|
+
if (typeof obj === 'object' && obj !== null) {
|
|
105
|
+
obj.traceId = ctx.traceId
|
|
106
|
+
obj.spanId = ctx.spanId
|
|
107
|
+
obj.parentSpanId = ctx.parentSpanId
|
|
108
|
+
obj.requestId = ctx.requestId
|
|
109
|
+
} else {
|
|
110
|
+
obj = { traceId: ctx.traceId, spanId: ctx.spanId, parentSpanId: ctx.parentSpanId, requestId: ctx.requestId, msg: obj }
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
// else {
|
|
114
|
+
// const messageStr = typeof obj === 'string' ? obj : (typeof msg === 'string' ? msg : '')
|
|
115
|
+
// const traceMatch = messageStr.match(/\[([a-f0-9-]{8}-[a-f0-9-]{4}-[a-f0-9-]{4}-[a-f0-9-]{4}-[a-f0-9-]{12})\]/)
|
|
116
|
+
// if (traceMatch) {
|
|
117
|
+
// const extractedTraceId = traceMatch[1]
|
|
118
|
+
// if (typeof obj === 'object' && obj !== null) {
|
|
119
|
+
// obj.traceId = extractedTraceId
|
|
120
|
+
// } else {
|
|
121
|
+
// obj = { traceId: extractedTraceId, msg: obj }
|
|
122
|
+
// }
|
|
123
|
+
// }
|
|
124
|
+
// }
|
|
125
|
+
return originalWarn.call(this, obj, msg, ...args)
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
logger.debug = function(obj, msg, ...args) {
|
|
129
|
+
const ctx = getRequestContext()
|
|
130
|
+
if (ctx && ctx.traceId) {
|
|
131
|
+
if (typeof obj === 'object' && obj !== null) {
|
|
132
|
+
obj.traceId = ctx.traceId
|
|
133
|
+
obj.spanId = ctx.spanId
|
|
134
|
+
obj.parentSpanId = ctx.parentSpanId
|
|
135
|
+
obj.requestId = ctx.requestId
|
|
136
|
+
} else {
|
|
137
|
+
obj = { traceId: ctx.traceId, spanId: ctx.spanId, parentSpanId: ctx.parentSpanId, requestId: ctx.requestId, msg: obj }
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
// else {
|
|
141
|
+
// // Extract traceId from message if it exists
|
|
142
|
+
// const messageStr = typeof obj === 'string' ? obj : (typeof msg === 'string' ? msg : '')
|
|
143
|
+
// const traceMatch = messageStr.match(/\[([a-f0-9-]{8}-[a-f0-9-]{4}-[a-f0-9-]{4}-[a-f0-9-]{4}-[a-f0-9-]{12})\]/)
|
|
144
|
+
// if (traceMatch) {
|
|
145
|
+
// const extractedTraceId = traceMatch[1]
|
|
146
|
+
// if (typeof obj === 'object' && obj !== null) {
|
|
147
|
+
// obj.traceId = extractedTraceId
|
|
148
|
+
// } else {
|
|
149
|
+
// obj = { traceId: extractedTraceId, msg: obj }
|
|
150
|
+
// }
|
|
151
|
+
// }
|
|
152
|
+
// }
|
|
153
|
+
return originalDebug.call(this, obj, msg, ...args)
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
logger.fatal = function(obj, msg, ...args) {
|
|
157
|
+
const ctx = getRequestContext()
|
|
158
|
+
if (ctx && ctx.traceId) {
|
|
159
|
+
if (typeof obj === 'object' && obj !== null) {
|
|
160
|
+
obj.traceId = ctx.traceId
|
|
161
|
+
obj.spanId = ctx.spanId
|
|
162
|
+
obj.parentSpanId = ctx.parentSpanId
|
|
163
|
+
obj.requestId = ctx.requestId
|
|
164
|
+
} else {
|
|
165
|
+
obj = { traceId: ctx.traceId, spanId: ctx.spanId, parentSpanId: ctx.parentSpanId, requestId: ctx.requestId, msg: obj }
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
// else {
|
|
169
|
+
// const messageStr = typeof obj === 'string' ? obj : (typeof msg === 'string' ? msg : '')
|
|
170
|
+
// const traceMatch = messageStr.match(/\[([a-f0-9-]{8}-[a-f0-9-]{4}-[a-f0-9-]{4}-[a-f0-9-]{4}-[a-f0-9-]{12})\]/)
|
|
171
|
+
// if (traceMatch) {
|
|
172
|
+
// const extractedTraceId = traceMatch[1]
|
|
173
|
+
// if (typeof obj === 'object' && obj !== null) {
|
|
174
|
+
// obj.traceId = extractedTraceId
|
|
175
|
+
// } else {
|
|
176
|
+
// obj = { traceId: extractedTraceId, msg: obj }
|
|
177
|
+
// }
|
|
178
|
+
// }
|
|
179
|
+
// }
|
|
180
|
+
return originalFatal.call(this, obj, msg, ...args)
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
logger.trace = function(obj, msg, ...args) {
|
|
184
|
+
const ctx = getRequestContext()
|
|
185
|
+
if (ctx && ctx.traceId) {
|
|
186
|
+
if (typeof obj === 'object' && obj !== null) {
|
|
187
|
+
obj.traceId = ctx.traceId
|
|
188
|
+
obj.spanId = ctx.spanId
|
|
189
|
+
obj.parentSpanId = ctx.parentSpanId
|
|
190
|
+
obj.requestId = ctx.requestId
|
|
191
|
+
} else {
|
|
192
|
+
obj = { traceId: ctx.traceId, spanId: ctx.spanId, parentSpanId: ctx.parentSpanId, requestId: ctx.requestId, msg: obj }
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
// else {
|
|
196
|
+
// // Extract traceId from message if it exists
|
|
197
|
+
// const messageStr = typeof obj === 'string' ? obj : (typeof msg === 'string' ? msg : '')
|
|
198
|
+
// const traceMatch = messageStr.match(/\[([a-f0-9-]{8}-[a-f0-9-]{4}-[a-f0-9-]{4}-[a-f0-9-]{4}-[a-f0-9-]{12})\]/)
|
|
199
|
+
// if (traceMatch) {
|
|
200
|
+
// const extractedTraceId = traceMatch[1]
|
|
201
|
+
// if (typeof obj === 'object' && obj !== null) {
|
|
202
|
+
// obj.traceId = extractedTraceId
|
|
203
|
+
// } else {
|
|
204
|
+
// obj = { traceId: extractedTraceId, msg: obj }
|
|
205
|
+
// }
|
|
206
|
+
// }
|
|
207
|
+
// }
|
|
208
|
+
return originalTrace.call(this, obj, msg, ...args)
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
return logger
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
Object.setPrototypeOf(patchedPino, originalPino)
|
|
215
|
+
Object.assign(patchedPino, originalPino)
|
|
216
|
+
|
|
217
|
+
return patchedPino
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
return result
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
} catch (_) {}
|
|
225
|
+
|
|
226
|
+
try {
|
|
227
|
+
const { Logger } = require('@nestjs/common')
|
|
228
|
+
if (Logger) {
|
|
229
|
+
const originalLog = Logger.prototype.log
|
|
230
|
+
const originalError = Logger.prototype.error
|
|
231
|
+
const originalWarn = Logger.prototype.warn
|
|
232
|
+
const originalDebug = Logger.prototype.debug
|
|
233
|
+
const originalVerbose = Logger.prototype.verbose
|
|
234
|
+
|
|
235
|
+
Logger.prototype.log = function(message, context) {
|
|
236
|
+
const { getRequestContext } = require('./store')
|
|
237
|
+
const ctx = getRequestContext()
|
|
238
|
+
if (ctx && ctx.traceId) {
|
|
239
|
+
return originalLog.call(this, message, context)
|
|
240
|
+
}
|
|
241
|
+
return originalLog.call(this, message, context)
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
Logger.prototype.error = function(message, trace, context) {
|
|
245
|
+
const { getRequestContext } = require('./store')
|
|
246
|
+
const ctx = getRequestContext()
|
|
247
|
+
if (ctx && ctx.traceId) {
|
|
248
|
+
return originalError.call(this, message, trace, context)
|
|
249
|
+
}
|
|
250
|
+
return originalError.call(this, message, trace, context)
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
Logger.prototype.warn = function(message, context) {
|
|
254
|
+
const { getRequestContext } = require('./store')
|
|
255
|
+
const ctx = getRequestContext()
|
|
256
|
+
if (ctx && ctx.traceId) {
|
|
257
|
+
return originalWarn.call(this, message, context)
|
|
258
|
+
}
|
|
259
|
+
return originalWarn.call(this, message, context)
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
Logger.prototype.debug = function(message, context) {
|
|
263
|
+
const { getRequestContext } = require('./store')
|
|
264
|
+
const ctx = getRequestContext()
|
|
265
|
+
if (ctx && ctx.traceId) {
|
|
266
|
+
return originalDebug.call(this, message, context)
|
|
267
|
+
}
|
|
268
|
+
return originalDebug.call(this, message, context)
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
Logger.prototype.verbose = function(message, context) {
|
|
272
|
+
const { getRequestContext } = require('./store')
|
|
273
|
+
const ctx = getRequestContext()
|
|
274
|
+
if (ctx && ctx.traceId) {
|
|
275
|
+
return originalVerbose.call(this, message, context)
|
|
276
|
+
}
|
|
277
|
+
return originalVerbose.call(this, message, context)
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
}
|
|
281
|
+
} catch (_) {}
|
|
282
|
+
|
|
283
|
+
try {
|
|
284
|
+
const originalConsoleLog = console.log
|
|
285
|
+
const originalConsoleError = console.error
|
|
286
|
+
const originalConsoleWarn = console.warn
|
|
287
|
+
|
|
288
|
+
console.log = function(...args) {
|
|
289
|
+
return originalConsoleLog.apply(this, args)
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
console.error = function(...args) {
|
|
293
|
+
return originalConsoleError.apply(this, args)
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
console.warn = function(...args) {
|
|
297
|
+
return originalConsoleWarn.apply(this, args)
|
|
298
|
+
}
|
|
299
|
+
} catch (_) {}
|
|
300
|
+
|
|
301
|
+
try {
|
|
302
|
+
const Module = require('module')
|
|
303
|
+
const originalRequire = Module.prototype.require
|
|
304
|
+
|
|
305
|
+
Module.prototype.require = function(id) {
|
|
306
|
+
const result = originalRequire.call(this, id)
|
|
307
|
+
|
|
308
|
+
if (id === 'nestjs-pino' || id.endsWith('/nestjs-pino') || id.includes('nestjs-pino')) {
|
|
309
|
+
if (result && result.Logger) {
|
|
310
|
+
const originalLoggerConstructor = result.Logger
|
|
311
|
+
|
|
312
|
+
function PatchedNestJSPinoLogger(context) {
|
|
313
|
+
const instance = new originalLoggerConstructor(context)
|
|
314
|
+
|
|
315
|
+
instance.error = function(message, trace, context) {
|
|
316
|
+
const { getRequestContext } = require('./store')
|
|
317
|
+
const ctx = getRequestContext()
|
|
318
|
+
if (ctx && ctx.traceId) {
|
|
319
|
+
return originalLoggerConstructor.prototype.error.call(this, message, trace, context)
|
|
320
|
+
}
|
|
321
|
+
return originalLoggerConstructor.prototype.error.call(this, message, trace, context)
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
instance.log = function(message, context) {
|
|
325
|
+
const { getRequestContext } = require('./store')
|
|
326
|
+
const ctx = getRequestContext()
|
|
327
|
+
if (ctx && ctx.traceId) {
|
|
328
|
+
return originalLoggerConstructor.prototype.log.call(this, message, context)
|
|
329
|
+
}
|
|
330
|
+
return originalLoggerConstructor.prototype.log.call(this, message, context)
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
instance.warn = function(message, context) {
|
|
334
|
+
const { getRequestContext } = require('./store')
|
|
335
|
+
const ctx = getRequestContext()
|
|
336
|
+
if (ctx && ctx.traceId) {
|
|
337
|
+
return originalLoggerConstructor.prototype.warn.call(this, message, context)
|
|
338
|
+
}
|
|
339
|
+
return originalLoggerConstructor.prototype.warn.call(this, message, context)
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
instance.debug = function(message, context) {
|
|
343
|
+
const { getRequestContext } = require('./store')
|
|
344
|
+
const ctx = getRequestContext()
|
|
345
|
+
if (ctx && ctx.traceId) {
|
|
346
|
+
return originalLoggerConstructor.prototype.debug.call(this, message, context)
|
|
347
|
+
}
|
|
348
|
+
return originalLoggerConstructor.prototype.debug.call(this, message, context)
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
instance.verbose = function(message, context) {
|
|
352
|
+
const { getRequestContext } = require('./store')
|
|
353
|
+
const ctx = getRequestContext()
|
|
354
|
+
if (ctx && ctx.traceId) {
|
|
355
|
+
return originalLoggerConstructor.prototype.verbose.call(this, message, context)
|
|
356
|
+
}
|
|
357
|
+
return originalLoggerConstructor.prototype.verbose.call(this, message, context)
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
return instance
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
Object.setPrototypeOf(PatchedNestJSPinoLogger, originalLoggerConstructor)
|
|
364
|
+
Object.assign(PatchedNestJSPinoLogger, originalLoggerConstructor)
|
|
365
|
+
|
|
366
|
+
result.Logger = PatchedNestJSPinoLogger
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
if (result && result.LoggerModule && result.LoggerModule.forRoot) {
|
|
370
|
+
const originalForRoot = result.LoggerModule.forRoot
|
|
371
|
+
|
|
372
|
+
result.LoggerModule.forRoot = function(options) {
|
|
373
|
+
const createPinoStream = require('./streams/pino')
|
|
374
|
+
const stream = createPinoStream()
|
|
375
|
+
|
|
376
|
+
const mergedOptions = {
|
|
377
|
+
...options,
|
|
378
|
+
pinoHttp: {
|
|
379
|
+
...(options && options.pinoHttp),
|
|
380
|
+
stream,
|
|
381
|
+
},
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
return originalForRoot.call(this, mergedOptions)
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
Object.setPrototypeOf(result.LoggerModule.forRoot, originalForRoot)
|
|
388
|
+
Object.assign(result.LoggerModule.forRoot, originalForRoot)
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
return result
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
} catch (_) {}
|
|
396
|
+
|
|
397
|
+
try {
|
|
398
|
+
const undici = require('undici')
|
|
399
|
+
if (undici && undici.request) {
|
|
400
|
+
const originalRequest = undici.request
|
|
401
|
+
undici.request = function(url, options, callback) {
|
|
402
|
+
const { getRequestContext } = require('./store')
|
|
403
|
+
const ctx = getRequestContext()
|
|
404
|
+
|
|
405
|
+
if (ctx && ctx.traceId) {
|
|
406
|
+
const headers = options?.headers || {}
|
|
407
|
+
options = {
|
|
408
|
+
...options,
|
|
409
|
+
headers: {
|
|
410
|
+
...headers,
|
|
411
|
+
'X-Trace-ID': ctx.traceId,
|
|
412
|
+
'X-Span-ID': ctx.spanId || '',
|
|
413
|
+
'X-Parent-Span-ID': ctx.parentSpanId || '',
|
|
414
|
+
'X-Request-ID': ctx.requestId || require('uuid').v4()
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
return originalRequest.call(this, url, options, callback)
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
} catch (_) {}
|
|
423
|
+
|
|
424
|
+
try {
|
|
425
|
+
const axios = require('axios')
|
|
426
|
+
if (axios && axios.create) {
|
|
427
|
+
const originalCreate = axios.create
|
|
428
|
+
axios.create = function(config) {
|
|
429
|
+
const instance = originalCreate.call(this, config)
|
|
430
|
+
|
|
431
|
+
const originalRequest = instance.interceptors.request.use
|
|
432
|
+
instance.interceptors.request.use = function(fulfilled, rejected) {
|
|
433
|
+
return originalRequest.call(this, (config) => {
|
|
434
|
+
const { getRequestContext } = require('./store')
|
|
435
|
+
const ctx = getRequestContext()
|
|
436
|
+
|
|
437
|
+
if (ctx && ctx.traceId) {
|
|
438
|
+
config.headers = {
|
|
439
|
+
...config.headers,
|
|
440
|
+
'X-Trace-ID': ctx.traceId,
|
|
441
|
+
'X-Span-ID': ctx.spanId || '',
|
|
442
|
+
'X-Parent-Span-ID': ctx.parentSpanId || '',
|
|
443
|
+
'X-Request-ID': ctx.requestId || require('uuid').v4()
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
return fulfilled ? fulfilled(config) : config
|
|
448
|
+
}, rejected)
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
return instance
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
} catch (_) {}
|
|
455
|
+
|
|
456
|
+
try {
|
|
457
|
+
const Bull = require('bull')
|
|
458
|
+
const { runWithRequestContext } = require('./store')
|
|
459
|
+
|
|
460
|
+
const originalAdd = Bull.prototype.add
|
|
461
|
+
Bull.prototype.add = function(name, data, opts) {
|
|
462
|
+
const ctx = getRequestContext()
|
|
463
|
+
|
|
464
|
+
if (ctx && ctx.traceId) {
|
|
465
|
+
data = {
|
|
466
|
+
...data,
|
|
467
|
+
traceId: ctx.traceId,
|
|
468
|
+
spanId: ctx.spanId,
|
|
469
|
+
parentSpanId: ctx.parentSpanId,
|
|
470
|
+
requestId: ctx.requestId,
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
return originalAdd.call(this, name, data, opts)
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
const originalProcess = Bull.prototype.process
|
|
478
|
+
Bull.prototype.process = function(name, concurrency, handler) {
|
|
479
|
+
let actualName, actualConcurrency, actualHandler
|
|
480
|
+
|
|
481
|
+
if (typeof name === 'function') {
|
|
482
|
+
actualHandler = name
|
|
483
|
+
actualName = '__default__'
|
|
484
|
+
actualConcurrency = 1
|
|
485
|
+
} else if (typeof concurrency === 'function') {
|
|
486
|
+
actualHandler = concurrency
|
|
487
|
+
actualName = name
|
|
488
|
+
actualConcurrency = 1
|
|
489
|
+
} else {
|
|
490
|
+
actualName = name
|
|
491
|
+
actualConcurrency = concurrency
|
|
492
|
+
actualHandler = handler
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
const wrappedHandler = function(job, done) {
|
|
496
|
+
const { traceId, spanId, parentSpanId, requestId, ...jobData } = job.data
|
|
497
|
+
|
|
498
|
+
if (traceId && spanId) {
|
|
499
|
+
const ctx = {
|
|
500
|
+
traceId,
|
|
501
|
+
spanId,
|
|
502
|
+
parentSpanId,
|
|
503
|
+
requestId,
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
return runWithRequestContext(ctx, () => {
|
|
507
|
+
return actualHandler.call(this, job, done)
|
|
508
|
+
})
|
|
509
|
+
} else {
|
|
510
|
+
const { startRequestContext } = require('./store')
|
|
511
|
+
const newCtx = startRequestContext({ requestId: require('uuid').v4() })
|
|
512
|
+
|
|
513
|
+
return runWithRequestContext(newCtx, () => {
|
|
514
|
+
return actualHandler.call(this, job, done)
|
|
515
|
+
})
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
if (typeof name === 'function') {
|
|
520
|
+
return originalProcess.call(this, wrappedHandler)
|
|
521
|
+
} else if (typeof concurrency === 'function') {
|
|
522
|
+
return originalProcess.call(this, actualName, wrappedHandler)
|
|
523
|
+
} else {
|
|
524
|
+
return originalProcess.call(this, actualName, actualConcurrency, wrappedHandler)
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
} catch (_) {}
|
|
528
|
+
} catch (_) {}
|