feat(observability): add structured pino logging

This commit is contained in:
2026-03-09 01:03:08 +04:00
parent 0ed22641ec
commit 8645a0a096
14 changed files with 279 additions and 45 deletions

View File

@@ -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"
}
}

View File

@@ -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
})
}