enabled logging as well 👌😪

This commit is contained in:
user
2026-02-28 19:50:55 +02:00
parent cf8d72cac3
commit 9914218b81
5 changed files with 50 additions and 128 deletions

View File

@@ -1,15 +1,13 @@
import DailyRotateFile from "winston-daily-rotate-file";
import { OpenTelemetryTransportV3 } from "@opentelemetry/winston-transport";
import { settings } from "@pkg/settings";
import type { Err } from "@pkg/result";
import winston from "winston";
import util from "util";
import path from "path";
process.on("warning", (warning) => {
const msg = String(warning?.message || "");
const name = String((warning as any)?.name || "");
// Ignore the noisy timer warning from Node/kafkajs interplay
if (
name === "TimeoutNegativeWarning" ||
msg.includes("TimeoutNegativeWarning") ||
@@ -18,7 +16,6 @@ process.on("warning", (warning) => {
return;
}
// Keep other warnings visible
console.warn(warning);
});
@@ -28,7 +25,7 @@ const levels = {
info: 2,
http: 3,
debug: 4,
};
} as const;
const colors = {
error: "red",
@@ -40,13 +37,12 @@ const colors = {
const level = () => {
const envLevel = process.env.LOG_LEVEL?.toLowerCase();
if (envLevel && envLevel in Object.keys(levels)) {
return envLevel;
if (envLevel && Object.prototype.hasOwnProperty.call(levels, envLevel)) {
return envLevel as keyof typeof levels;
}
return settings.isDevelopment ? "debug" : "warn";
};
// Console format with colors
const consoleFormat = winston.format.combine(
winston.format.errors({ stack: true }),
winston.format.timestamp({ format: "YYYY-MM-DD HH:mm:ss:ms" }),
@@ -54,19 +50,13 @@ const consoleFormat = winston.format.combine(
winston.format.printf((info) => {
const { level, message, timestamp, ...extra } = info;
let formattedMessage = "";
if (message instanceof Error) {
formattedMessage = message.stack || message.message;
} else if (typeof message === "object") {
formattedMessage = util.inspect(message, {
depth: null,
colors: true,
});
} else {
formattedMessage = message as any as string;
}
const formattedMessage =
message instanceof Error
? message.stack || message.message
: typeof message === "object"
? util.inspect(message, { depth: null, colors: true })
: String(message);
// Handle extra fields (if any)
const formattedExtra =
Object.keys(extra).length > 0
? `\n${util.inspect(extra, { depth: null, colors: true })}`
@@ -76,53 +66,18 @@ const consoleFormat = winston.format.combine(
}),
);
// JSON format for file logging
const fileFormat = winston.format.combine(
winston.format.errors({ stack: true }),
winston.format.timestamp(),
winston.format.json(),
);
// File transport with daily rotation
const fileTransport = new DailyRotateFile({
filename: path.join("logs", "app-%DATE%.log"),
datePattern: "YYYY-MM-DD",
maxSize: "20m",
maxFiles: "14d",
format: fileFormat,
});
// Error file transport with daily rotation
const errorFileTransport = new DailyRotateFile({
filename: path.join("logs", "error-%DATE%.log"),
datePattern: "YYYY-MM-DD",
maxSize: "20m",
maxFiles: "14d",
level: "error",
format: fileFormat,
});
const transports: winston.transport[] = [
new winston.transports.Console({ format: consoleFormat }),
fileTransport,
errorFileTransport,
];
winston.addColors(colors);
const logger = winston.createLogger({
level: level(),
levels,
transports,
format: fileFormat,
exceptionHandlers: [
format: consoleFormat,
transports: [
new winston.transports.Console({ format: consoleFormat }),
fileTransport,
],
rejectionHandlers: [
new winston.transports.Console({ format: consoleFormat }),
fileTransport,
new OpenTelemetryTransportV3(),
],
exceptionHandlers: [new winston.transports.Console({ format: consoleFormat })],
rejectionHandlers: [new winston.transports.Console({ format: consoleFormat })],
});
const stream = { write: (message: string) => logger.http(message.trim()) };

View File

@@ -9,10 +9,9 @@
"typescript": "^5.9.3"
},
"dependencies": {
"@axiomhq/winston": "^1.3.1",
"@opentelemetry/winston-transport": "^0.22.0",
"@pkg/result": "workspace:*",
"@pkg/settings": "workspace:*",
"winston": "^3.17.0",
"winston-daily-rotate-file": "^5.0.0"
"winston": "^3.17.0"
}
}