mirror of
https://github.com/whekin/household-bot.git
synced 2026-03-31 12:04:02 +00:00
feat(observability): add structured pino logging
This commit is contained in:
@@ -1,24 +1,49 @@
|
||||
import { createEnv } from '@t3-oss/env-core'
|
||||
import { z } from 'zod'
|
||||
|
||||
function parseOptionalCsv(value: string | undefined): readonly string[] | undefined {
|
||||
const trimmed = value?.trim()
|
||||
|
||||
if (!trimmed) {
|
||||
return undefined
|
||||
}
|
||||
|
||||
return trimmed
|
||||
.split(',')
|
||||
.map((entry) => entry.trim())
|
||||
.filter(Boolean)
|
||||
}
|
||||
|
||||
const server = {
|
||||
NODE_ENV: z.enum(['development', 'test', 'production']).default('development'),
|
||||
LOG_LEVEL: z.enum(['debug', 'info', 'warn', 'error']).default('info'),
|
||||
APP_URL: z.string().url(),
|
||||
PORT: z.coerce.number().int().min(1).max(65535).default(3000),
|
||||
DATABASE_URL: z.string().url(),
|
||||
SUPABASE_URL: z.string().url(),
|
||||
SUPABASE_PUBLISHABLE_KEY: z.string().min(1),
|
||||
SUPABASE_SERVICE_ROLE_KEY: z.string().min(1),
|
||||
HOUSEHOLD_ID: z.string().uuid(),
|
||||
SUPABASE_URL: z.string().url().optional(),
|
||||
SUPABASE_PUBLISHABLE_KEY: z.string().min(1).optional(),
|
||||
SUPABASE_SERVICE_ROLE_KEY: z.string().min(1).optional(),
|
||||
TELEGRAM_BOT_TOKEN: z.string().min(1),
|
||||
TELEGRAM_WEBHOOK_SECRET: z.string().min(1),
|
||||
TELEGRAM_BOT_USERNAME: z.string().min(1),
|
||||
OPENAI_API_KEY: z.string().min(1),
|
||||
TELEGRAM_WEBHOOK_PATH: z.string().min(1).default('/webhook/telegram'),
|
||||
TELEGRAM_HOUSEHOLD_CHAT_ID: z.string().min(1).optional(),
|
||||
TELEGRAM_PURCHASE_TOPIC_ID: z.coerce.number().int().positive().optional(),
|
||||
TELEGRAM_FEEDBACK_TOPIC_ID: z.coerce.number().int().positive().optional(),
|
||||
MINI_APP_ALLOWED_ORIGINS: z
|
||||
.string()
|
||||
.optional()
|
||||
.transform((value) => parseOptionalCsv(value)),
|
||||
SCHEDULER_OIDC_ALLOWED_EMAILS: z
|
||||
.string()
|
||||
.optional()
|
||||
.transform((value) => parseOptionalCsv(value)),
|
||||
OPENAI_API_KEY: z.string().min(1).optional(),
|
||||
PARSER_MODEL: z.string().min(1).default('gpt-4.1-mini'),
|
||||
SENTRY_DSN: z.string().url().optional(),
|
||||
GCP_PROJECT_ID: z.string().min(1),
|
||||
GCP_PROJECT_ID: z.string().min(1).optional(),
|
||||
GCP_REGION: z.string().min(1).default('europe-west1'),
|
||||
CLOUD_RUN_SERVICE_BOT: z.string().min(1).default('household-bot'),
|
||||
SCHEDULER_SHARED_SECRET: z.string().min(1)
|
||||
SCHEDULER_SHARED_SECRET: z.string().min(1).optional()
|
||||
}
|
||||
|
||||
export const env = createEnv({
|
||||
|
||||
@@ -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