apps/bot/src/lib/logger.ts (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
import { type KittenLogger, type LogLevel } from "@purrkit/router";
import pino, { type Logger as PinoInstance } from "pino";
const isProd = process.env.NODE_ENV === "production";
const logLevel = process.env.LOG_LEVEL ? process.env.LOG_LEVEL : isProd ? "info" : "debug";
export const logger = pino({
base: {
pid: false,
hostname: false,
},
level: logLevel.toLowerCase(),
});
export class KLogger implements KittenLogger {
constructor(private readonly pinoInstance: PinoInstance = logger) {}
private log(level: LogLevel, msg: string, data?: Record<string, unknown>): void {
if (data) this.pinoInstance[level](data, msg);
else this.pinoInstance[level](msg);
}
trace(msg: string, data?: Record<string, unknown>): void {
this.log("trace", msg, data);
}
debug(msg: string, data?: Record<string, unknown>): void {
this.log("debug", msg, data);
}
info(msg: string, data?: Record<string, unknown>): void {
this.log("info", msg, data);
}
warn(msg: string, data?: Record<string, unknown>): void {
this.log("warn", msg, data);
}
error(msg: string, data?: Record<string, unknown>): void {
this.log("error", msg, data);
}
fatal(msg: string, data?: Record<string, unknown>): void {
this.log("fatal", msg, data);
}
child(bindings: Record<string, unknown>): KittenLogger {
const childPino = this.pinoInstance.child(bindings);
return new KLogger(childPino);
}
}
|