mirror of
https://github.com/whekin/household-bot.git
synced 2026-03-31 13:54:02 +00:00
feat(observability): add structured pino logging
This commit is contained in:
@@ -2,10 +2,16 @@
|
||||
"name": "@household/observability",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"exports": {
|
||||
".": "./src/index.ts"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "bun build src/index.ts --outdir dist --target bun",
|
||||
"typecheck": "tsgo --project tsconfig.json --noEmit",
|
||||
"test": "bun test --pass-with-no-tests",
|
||||
"lint": "oxlint \"src\""
|
||||
},
|
||||
"dependencies": {
|
||||
"pino": "^9.9.0"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1 +1,53 @@
|
||||
export const observabilityReady = true
|
||||
import pino, { type Bindings, type Logger, type LoggerOptions } from 'pino'
|
||||
|
||||
export type { Logger }
|
||||
|
||||
export type LogLevel = 'debug' | 'info' | 'warn' | 'error'
|
||||
|
||||
let rootLogger = pino({
|
||||
level: 'info',
|
||||
timestamp: pino.stdTimeFunctions.isoTime,
|
||||
base: null,
|
||||
formatters: {
|
||||
level(label) {
|
||||
return {
|
||||
level: label
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
export function configureLogger(
|
||||
options: {
|
||||
level?: LogLevel
|
||||
service?: string
|
||||
base?: Bindings
|
||||
} = {}
|
||||
): Logger {
|
||||
const loggerOptions: LoggerOptions = {
|
||||
level: options.level ?? 'info',
|
||||
timestamp: pino.stdTimeFunctions.isoTime,
|
||||
base: null,
|
||||
formatters: {
|
||||
level(label) {
|
||||
return {
|
||||
level: label
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
rootLogger = pino(loggerOptions).child({
|
||||
service: options.service ?? 'household',
|
||||
...options.base
|
||||
})
|
||||
|
||||
return rootLogger
|
||||
}
|
||||
|
||||
export function getLogger(name: string, bindings: Bindings = {}): Logger {
|
||||
return rootLogger.child({
|
||||
logger: name,
|
||||
...bindings
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user