From 7fd4aeff17dc1aaf03671a3026fa8265a961722e Mon Sep 17 00:00:00 2001 From: Michael Czechowski Date: Thu, 8 Oct 2026 22:10:28 +0200 Subject: [PATCH] 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 https://git.librete.ch/libretech/mp/issues/71 --- config/logger.ts | 10 ++-------- src/logging/format.ts | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 8 deletions(-) create mode 100644 src/logging/format.ts diff --git a/config/logger.ts b/config/logger.ts index a0d1663..ece6c76 100644 --- a/config/logger.ts +++ b/config/logger.ts @@ -1,16 +1,10 @@ import { winston } from "@strapi/logger"; +import { formatLogLine } from "../src/logging/format"; export default { transports: [ new winston.transports.Console({ - format: winston.format.combine( - winston.format.printf(({ timestamp, level, message, ...rest }) => { - let restString = JSON.stringify(rest, undefined, 2); - restString = restString === "{}" ? "" : restString; - - return `${timestamp}Z ${level}: ${message} ${restString}`; - }) - ) + format: winston.format.combine(winston.format.printf(formatLogLine)) }) ] }; diff --git a/src/logging/format.ts b/src/logging/format.ts new file mode 100644 index 0000000..693645a --- /dev/null +++ b/src/logging/format.ts @@ -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: `Z : `. */ +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}`; +};