refactor(logging): move the console line format into src/logging/format.ts

The printf template of config/logger.ts becomes formatLogLine, with the same
logic, so it can be unit tested without winston.

No behaviour change.

Refs libretech/mp#71
This commit is contained in:
2026-10-08 22:10:28 +02:00
parent 9873c184d6
commit 7fd4aeff17
2 changed files with 16 additions and 8 deletions
+2 -8
View File
@@ -1,16 +1,10 @@
import { winston } from "@strapi/logger"; import { winston } from "@strapi/logger";
import { formatLogLine } from "../src/logging/format";
export default { export default {
transports: [ transports: [
new winston.transports.Console({ new winston.transports.Console({
format: winston.format.combine( format: winston.format.combine(winston.format.printf(formatLogLine))
winston.format.printf(({ timestamp, level, message, ...rest }) => {
let restString = JSON.stringify(rest, undefined, 2);
restString = restString === "{}" ? "" : restString;
return `${timestamp}Z ${level}: ${message} ${restString}`;
})
)
}) })
] ]
}; };
+14
View File
@@ -0,0 +1,14 @@
/** What winston hands a printf template (logform's TransformableInfo); Strapi's default format has added `timestamp`. */
export type LogInfo = {
level: string;
message: unknown;
[key: string | symbol]: unknown;
};
/** One console line: `<timestamp>Z <level>: <message> <all other fields as indented JSON, if any>`. */
export const formatLogLine = ({ timestamp, level, message, ...rest }: LogInfo): string => {
let restString = JSON.stringify(rest, undefined, 2);
restString = restString === "{}" ? "" : restString;
return `${timestamp}Z ${level}: ${message} ${restString}`;
};