44 lines
1.1 KiB
TypeScript
44 lines
1.1 KiB
TypeScript
/**
|
|
* Structured logger built on pino.
|
|
*
|
|
* - level: from LOG_LEVEL env var (default "info")
|
|
* - dev: pretty-printed, colorized output
|
|
* - prod: JSON output suitable for ingestion by log shippers
|
|
*
|
|
* Use child loggers (`logger.child({ component: "name" })`) to tag log lines
|
|
* by subsystem so they remain easy to grep in production.
|
|
*/
|
|
import pino, { type LoggerOptions } from "pino";
|
|
|
|
const NODE_ENV = process.env.NODE_ENV ?? "development";
|
|
const IS_PROD = NODE_ENV === "production";
|
|
const LEVEL = process.env.LOG_LEVEL ?? "info";
|
|
|
|
const baseOptions: LoggerOptions = {
|
|
level: LEVEL,
|
|
base: { env: NODE_ENV },
|
|
timestamp: pino.stdTimeFunctions.isoTime,
|
|
};
|
|
|
|
const transport = IS_PROD
|
|
? undefined
|
|
: {
|
|
target: "pino-pretty",
|
|
options: {
|
|
colorize: true,
|
|
translateTime: "SYS:HH:MM:ss.l",
|
|
ignore: "pid,hostname,env",
|
|
singleLine: false,
|
|
},
|
|
};
|
|
|
|
export const logger = pino({
|
|
...baseOptions,
|
|
...(transport ? { transport } : {}),
|
|
});
|
|
|
|
export type Logger = typeof logger;
|
|
|
|
export function childLogger(component: string, extra: Record<string, unknown> = {}) {
|
|
return logger.child({ component, ...extra });
|
|
}
|