fix(logging): use getRequestPath to more robustly identify debug logs (#3005)

This commit is contained in:
Iain Sproat 2024-09-16 14:01:34 +01:00 коммит произвёл GitHub
Родитель 8eeddad57b
Коммит cd14619790
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
3 изменённых файлов: 22 добавлений и 8 удалений

Просмотреть файл

@ -1,7 +1,9 @@
import { REQUEST_ID_HEADER } from '@/domain/const.js'
import { logger } from '@/observability/logging.js'
import { randomUUID } from 'crypto'
import type { Request } from 'express'
import type { IncomingHttpHeaders, IncomingMessage } from 'http'
import { get } from 'lodash'
import { pinoHttp } from 'pino-http'
function determineRequestId(headers: IncomingHttpHeaders, uuidGenerator = randomUUID) {
@ -13,6 +15,13 @@ function determineRequestId(headers: IncomingHttpHeaders, uuidGenerator = random
const generateReqId = (req: IncomingMessage) => determineRequestId(req.headers)
export const getRequestPath = (req: IncomingMessage | Request) => {
const path = ((get(req, 'originalUrl') || get(req, 'url') || '') as string).split(
'?'
)[0]
return path?.length ? path : null
}
export const loggingExpressMiddleware = pinoHttp({
genReqId: generateReqId,
logger,
@ -21,6 +30,9 @@ export const loggingExpressMiddleware = pinoHttp({
// and we don't really care about 3xx stuff
// all the user related 4xx responses are treated as info
customLogLevel: (req, res, error) => {
const path = getRequestPath(req)
const shouldBeDebug = ['/metrics'].includes(path || '') ?? false
if (res.statusCode >= 400 && res.statusCode < 500) {
return 'info'
} else if (res.statusCode >= 500 || error) {
@ -29,6 +41,6 @@ export const loggingExpressMiddleware = pinoHttp({
return 'silent'
}
return 'info' //default
return shouldBeDebug ? 'debug' : 'info'
}
})

Просмотреть файл

@ -44,6 +44,10 @@ export const LoggingExpressMiddleware = HttpLogger({
autoLogging: true,
genReqId: GenerateRequestId,
customLogLevel: (req, res, err) => {
const path = getRequestPath(req)
const shouldBeDebug =
['/metrics', '/readiness', '/liveness'].includes(path || '') ?? false
if (res.statusCode >= 400 && res.statusCode < 500) {
return 'info'
} else if (res.statusCode >= 500 || err) {
@ -52,9 +56,7 @@ export const LoggingExpressMiddleware = HttpLogger({
return 'info'
}
if (req.url === '/readiness' || req.url === '/liveness') return 'debug'
if (req.url === '/metrics') return 'debug'
return 'info'
return shouldBeDebug ? 'debug' : 'info'
},
customReceivedMessage() {

Просмотреть файл

@ -1,10 +1,10 @@
import type { Request } from 'express'
import type { IncomingMessage } from 'http'
import type express from 'express'
import { get } from 'lodash'
export const getRequestPath = (req: IncomingMessage | express.Request) => {
const path = (get(req, 'originalUrl') || get(req, 'url') || '').split(
export const getRequestPath = (req: IncomingMessage | Request) => {
const path = ((get(req, 'originalUrl') || get(req, 'url') || '') as string).split(
'?'
)[0] as string
)[0]
return path?.length ? path : null
}