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:
+2
-8
@@ -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}`;
|
|
||||||
})
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
]
|
]
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -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}`;
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user