azify-logger 1.0.35 → 1.0.36
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/middleware-express.js +70 -41
- package/package.json +1 -1
- package/register.js +6 -31
package/middleware-express.js
CHANGED
|
@@ -306,11 +306,6 @@ function createExpressLoggingMiddleware(options = {}) {
|
|
|
306
306
|
const originalEnd = res.end.bind(res)
|
|
307
307
|
|
|
308
308
|
res.end = (chunk, encoding) => {
|
|
309
|
-
if (logSent) {
|
|
310
|
-
return originalEnd(chunk, encoding)
|
|
311
|
-
}
|
|
312
|
-
logSent = true
|
|
313
|
-
|
|
314
309
|
const result = originalEnd(chunk, encoding)
|
|
315
310
|
|
|
316
311
|
if (chunk != null && config.captureResponseBody && !responseChunkCaptured) {
|
|
@@ -318,43 +313,77 @@ function createExpressLoggingMiddleware(options = {}) {
|
|
|
318
313
|
responseChunkCaptured = true
|
|
319
314
|
}
|
|
320
315
|
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
const statusCode = res.statusCode || 200
|
|
325
|
-
const duration = Date.now() - startTime
|
|
326
|
-
const response = { statusCode, durationMs: duration }
|
|
327
|
-
|
|
328
|
-
if (config.captureHeaders) {
|
|
329
|
-
ensureHeaders()
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
const requestObj = {
|
|
333
|
-
id: requestId,
|
|
334
|
-
method,
|
|
335
|
-
url,
|
|
336
|
-
path,
|
|
337
|
-
ip: clientIp
|
|
338
|
-
}
|
|
339
|
-
if (query) requestObj.query = query
|
|
340
|
-
if (config.captureHeaders && cachedHeaders) requestObj.headers = cachedHeaders
|
|
341
|
-
|
|
342
|
-
const meta = {
|
|
343
|
-
traceId: reqCtx.traceId,
|
|
344
|
-
spanId: reqCtx.spanId,
|
|
345
|
-
parentSpanId: reqCtx.parentSpanId || null,
|
|
346
|
-
requestId,
|
|
347
|
-
request: requestObj,
|
|
348
|
-
response,
|
|
349
|
-
timestamp: Date.now(),
|
|
350
|
-
hostname
|
|
351
|
-
}
|
|
352
|
-
if (serviceObj) meta.service = serviceObj
|
|
353
|
-
if (config.environment) meta.environment = config.environment
|
|
316
|
+
if (!logSent) {
|
|
317
|
+
logSent = true
|
|
354
318
|
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
319
|
+
process.nextTick(() => {
|
|
320
|
+
try {
|
|
321
|
+
ensureIds()
|
|
322
|
+
|
|
323
|
+
const statusCode = res.statusCode || 200
|
|
324
|
+
const duration = Date.now() - startTime
|
|
325
|
+
const response = { statusCode, durationMs: duration }
|
|
326
|
+
|
|
327
|
+
if (config.captureHeaders) {
|
|
328
|
+
ensureHeaders()
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
const requestObj = {
|
|
332
|
+
id: requestId,
|
|
333
|
+
method,
|
|
334
|
+
url,
|
|
335
|
+
path,
|
|
336
|
+
ip: clientIp
|
|
337
|
+
}
|
|
338
|
+
if (query) requestObj.query = query
|
|
339
|
+
if (config.captureHeaders && cachedHeaders) requestObj.headers = cachedHeaders
|
|
340
|
+
|
|
341
|
+
const meta = {
|
|
342
|
+
traceId: reqCtx.traceId,
|
|
343
|
+
spanId: reqCtx.spanId,
|
|
344
|
+
parentSpanId: reqCtx.parentSpanId || null,
|
|
345
|
+
requestId,
|
|
346
|
+
request: requestObj,
|
|
347
|
+
response,
|
|
348
|
+
timestamp: Date.now(),
|
|
349
|
+
hostname
|
|
350
|
+
}
|
|
351
|
+
if (serviceObj) meta.service = serviceObj
|
|
352
|
+
if (config.environment) meta.environment = config.environment
|
|
353
|
+
|
|
354
|
+
const chunkToProcess = (responseChunk !== null && responseChunkCaptured) ? responseChunk : null
|
|
355
|
+
emitResponseLog(meta, chunkToProcess)
|
|
356
|
+
} catch (err) {
|
|
357
|
+
try {
|
|
358
|
+
ensureIds()
|
|
359
|
+
const statusCode = res.statusCode || 200
|
|
360
|
+
const duration = Date.now() - startTime
|
|
361
|
+
const response = { statusCode, durationMs: duration }
|
|
362
|
+
const requestObj = {
|
|
363
|
+
id: requestId,
|
|
364
|
+
method,
|
|
365
|
+
url,
|
|
366
|
+
path,
|
|
367
|
+
ip: clientIp || 'unknown'
|
|
368
|
+
}
|
|
369
|
+
const meta = {
|
|
370
|
+
traceId: reqCtx?.traceId || traceId,
|
|
371
|
+
spanId: reqCtx?.spanId || spanId,
|
|
372
|
+
parentSpanId: reqCtx?.parentSpanId || parentSpanId || null,
|
|
373
|
+
requestId: requestId || fastUUID(),
|
|
374
|
+
request: requestObj,
|
|
375
|
+
response,
|
|
376
|
+
timestamp: Date.now(),
|
|
377
|
+
hostname
|
|
378
|
+
}
|
|
379
|
+
if (serviceObj) meta.service = serviceObj
|
|
380
|
+
if (config.environment) meta.environment = config.environment
|
|
381
|
+
emitResponseLog(meta, null)
|
|
382
|
+
} catch (_) {
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
})
|
|
386
|
+
}
|
|
358
387
|
|
|
359
388
|
return result
|
|
360
389
|
}
|
package/package.json
CHANGED
package/register.js
CHANGED
|
@@ -894,10 +894,7 @@ try {
|
|
|
894
894
|
|
|
895
895
|
markSource(requestMeta, 'http-client')
|
|
896
896
|
if (HTTP_CLIENT_MODE !== 'off') {
|
|
897
|
-
|
|
898
|
-
sendOutboundLog('info', `[REQUEST] ${method} ${url}`, requestMeta)
|
|
899
|
-
} catch (_) {
|
|
900
|
-
}
|
|
897
|
+
sendOutboundLog('info', `[REQUEST] ${method} ${url}`, requestMeta)
|
|
901
898
|
}
|
|
902
899
|
|
|
903
900
|
const childCtx = {
|
|
@@ -937,7 +934,7 @@ try {
|
|
|
937
934
|
const duration = Number((performance.now() - start).toFixed(2))
|
|
938
935
|
const shouldLogResponse = HTTP_CLIENT_MODE !== 'off'
|
|
939
936
|
|
|
940
|
-
if (shouldLogResponse) {
|
|
937
|
+
if (shouldLogResponse && response) {
|
|
941
938
|
const headersToObject = (headers) => {
|
|
942
939
|
try {
|
|
943
940
|
if (!headers) return {}
|
|
@@ -1030,32 +1027,10 @@ try {
|
|
|
1030
1027
|
responseMeta.responseBody = responseBodyString
|
|
1031
1028
|
}
|
|
1032
1029
|
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
sendOutboundLog(level, message, responseMeta)
|
|
1038
|
-
} catch (err) {
|
|
1039
|
-
try {
|
|
1040
|
-
const fallbackMeta = {
|
|
1041
|
-
traceId: requestMeta.traceId || randomUUID(),
|
|
1042
|
-
spanId: requestMeta.spanId || randomBytes(8).toString('hex'),
|
|
1043
|
-
parentSpanId: requestMeta.parentSpanId || null,
|
|
1044
|
-
requestId: requestMeta.requestId || randomUUID(),
|
|
1045
|
-
method,
|
|
1046
|
-
url,
|
|
1047
|
-
statusCode: response.status,
|
|
1048
|
-
responseTimeMs: duration,
|
|
1049
|
-
headers: requestMeta.headers || {},
|
|
1050
|
-
responseHeaders: {}
|
|
1051
|
-
}
|
|
1052
|
-
markSource(fallbackMeta, 'http-client')
|
|
1053
|
-
const message = `[RESPONSE] ${method} ${url} ${response.status} ${duration}ms`
|
|
1054
|
-
const level = response.status >= 500 ? 'error' : response.status >= 400 ? 'warn' : 'info'
|
|
1055
|
-
sendOutboundLog(level, message, fallbackMeta)
|
|
1056
|
-
} catch (_) {
|
|
1057
|
-
}
|
|
1058
|
-
}
|
|
1030
|
+
markSource(responseMeta, 'http-client')
|
|
1031
|
+
const message = `[RESPONSE] ${method} ${url} ${response.status} ${duration}ms`
|
|
1032
|
+
const level = response.status >= 500 ? 'error' : response.status >= 400 ? 'warn' : 'info'
|
|
1033
|
+
sendOutboundLog(level, message, responseMeta)
|
|
1059
1034
|
}
|
|
1060
1035
|
|
|
1061
1036
|
return response
|