61 lines
1.5 KiB
TypeScript
61 lines
1.5 KiB
TypeScript
import pino, { type DestinationStream, type Logger, type LoggerOptions } from "pino";
|
|
|
|
export type LogLevel = "trace" | "debug" | "info" | "warn" | "error" | "fatal" | "silent";
|
|
|
|
export type ToolctrlLogger = Pick<Logger, "debug" | "info" | "warn" | "error" | "child">;
|
|
|
|
const REDACT_PATHS = [
|
|
"authorization",
|
|
"headers.authorization",
|
|
"req.headers.authorization",
|
|
"request.headers.authorization",
|
|
"cookie",
|
|
"headers.cookie",
|
|
"req.headers.cookie",
|
|
"request.headers.cookie",
|
|
"password",
|
|
"*.password",
|
|
"token",
|
|
"*.token",
|
|
"secret",
|
|
"*.secret",
|
|
"apiKey",
|
|
"*.apiKey",
|
|
"redisUrl",
|
|
"*.redisUrl",
|
|
"connectionString",
|
|
"*.connectionString",
|
|
] as const;
|
|
|
|
export interface CreateLoggerOptions extends Omit<LoggerOptions, "base" | "level" | "redact"> {
|
|
level?: LogLevel;
|
|
stream?: DestinationStream;
|
|
}
|
|
|
|
export function resolveLogLevel(env: NodeJS.ProcessEnv = process.env): LogLevel {
|
|
return (env.TOOLCTRL_LOG_LEVEL ?? env.LOG_LEVEL ?? "info") as LogLevel;
|
|
}
|
|
|
|
export function createLogger(options: CreateLoggerOptions = {}): ToolctrlLogger {
|
|
const { level = resolveLogLevel(), stream, ...pinoOptions } = options;
|
|
|
|
return pino(
|
|
{
|
|
...pinoOptions,
|
|
base: undefined,
|
|
level,
|
|
redact: {
|
|
paths: [...REDACT_PATHS],
|
|
censor: "[Redacted]",
|
|
},
|
|
},
|
|
stream,
|
|
);
|
|
}
|
|
|
|
export function createChildLogger(logger: ToolctrlLogger, bindings: Record<string, unknown>): ToolctrlLogger {
|
|
return logger.child(bindings);
|
|
}
|
|
|
|
export const defaultLogger = createLogger();
|