Created
February 4, 2022 20:49
-
-
Save gkampitakis/b36819f38f8886598c20ed1af7245e3a to your computer and use it in GitHub Desktop.
pino-http ECS compliant
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Options, pinoHttp } from 'pino-http'; | |
import { formatError } from '@elastic/ecs-helpers'; | |
// Taken as it is from @elastic/ecs-pino-format | |
function bindings(bindingsObject: Record<string, unknown>) { | |
const { pid, hostname, ...ecsBindings } = bindingsObject; | |
if (pid !== undefined) { | |
ecsBindings.process = { pid }; | |
} | |
if (hostname !== undefined) { | |
ecsBindings.host = { hostname }; | |
} | |
return ecsBindings; | |
} | |
export const options = (options?: Options): Options => ({ | |
...options, | |
customAttributeKeys: { | |
req: 'http.request', | |
res: 'http.response', | |
responseTime: 'event.duration', | |
}, | |
messageKey: 'message', | |
timestamp: () => `"@timestamp":"${new Date().toISOString()}"`, | |
serializers: { | |
'http.response': (object) => { | |
const { statusCode, ...response } = object; | |
return { | |
...response, | |
status_code: statusCode, | |
}; | |
}, | |
'http.request': () => { | |
// here you can perform headers transformation | |
} | |
}, | |
formatters: { | |
bindings, | |
level: (label: string) => ({ 'log.level': label }), | |
log: (o: object) => { | |
const { error, ...ecsObject } = o; | |
ecsObject.ecs = { version: '1.6.0' }; | |
formatError(ecsObject, error); | |
return ecsObject; | |
} | |
} | |
}); | |
const { logger } = pinoHttp(options()); | |
logger.info({ | |
test: 'test' | |
}, 'msg'); |
fourthdimension
commented
Mar 16, 2023
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment