返回提交历史
Modified
package-lock.json
+1
-0
Modified
package.json
+1
-0
Modified
src/utils/logger.ts
+33
-17
XFEstudio/SpaceNinjaServer
fix: readd 'metadata' to log messages (#3971)
It didn't occur to me that that's the thing we're using when logging objects alongside messages (: Reviewed-on: https://onlyg.it/OpenWF/SpaceNinjaServer/pulls/3971 Co-authored-by: Sainan <63328889+Sainan@users.noreply.github.com> Co-committed-by: Sainan <63328889+Sainan@users.noreply.github.com>
fdb6383d
代码差异
3 个文件
+35
-17
@@ -42,6 +42,7 @@
42
42
"@types/websocket": "^1.0.10",
43
43
"@types/ws": "^8.18.1",
44
44
"@typescript/native-preview": "^7.0.0-dev.20260308.1",
45
"logform": "^2.7.0",
45
46
"ncp": "^2.0.0"
46
47
}
47
48
},
@@ -47,6 +47,7 @@
47
47
"@types/websocket": "^1.0.10",
48
48
"@types/ws": "^8.18.1",
49
49
"@typescript/native-preview": "^7.0.0-dev.20260308.1",
50
"logform": "^2.7.0",
50
51
"ncp": "^2.0.0"
51
52
},
52
53
"devDependencies": {
@@ -1,33 +1,44 @@
1
1
import type { LeveledLogMethod } from "winston";
2
import type { Format } from "logform";
2
3
import { createLogger, format, transports, addColors } from "winston";
3
4
import "winston-daily-rotate-file";
5
import * as util from "node:util";
6
import { isEmptyObject } from "../helpers/general.ts";
4
7
import { config } from "../services/configService.ts";
5
8
6
const printf = format.printf(info => {
7
return (config.logger.format ?? "%timestamp% [%level%] %message%")
8
.replaceAll("%timestamp%", info.timestamp as string)
9
.replaceAll("%level%", info.level)
10
.replaceAll("%message%", info.message as string);
11
});
9
const createFormat = (colors: boolean, localTime: boolean): Format =>
10
format.combine(
11
colors ? format.colorize() : format.uncolorize(),
12
localTime ? format.timestamp({ format: "YYYY-MM-DDTHH:mm:ss:SSS" }) : format.timestamp(),
13
format.metadata({ fillExcept: ["message", "level", "timestamp", "version"] }),
14
format.printf(info =>
15
(config.logger.format ?? "%timestamp% [%level%] %message%")
16
.replaceAll("%timestamp%", info.timestamp as string)
17
.replaceAll("%level%", info.level)
18
.replaceAll(
19
"%message%",
20
(info.message as string) +
21
(isEmptyObject(info.metadata)
22
? ""
23
: " " +
24
util.inspect(info.metadata, {
25
showHidden: false,
26
depth: null,
27
colors
28
}))
29
)
30
)
31
);
12
32
13
33
const fileLog = new transports.DailyRotateFile({
14
34
filename: `logs/${config.logger.level}.log`,
15
format: format.combine(
16
format.uncolorize(),
17
format.timestamp(), // zulu time
18
printf
19
),
35
format: createFormat(false, false),
20
36
datePattern: "YYYY-MM-DD"
21
37
});
22
38
23
39
const consoleLog = new transports.Console({
24
40
forceConsole: false,
25
format: format.combine(
26
format.colorize(),
27
format.timestamp({ format: "YYYY-MM-DDTHH:mm:ss:SSS" }), // uses local timezone
28
format.errors({ stack: true }),
29
printf
30
)
41
format: createFormat(true, true)
31
42
});
32
43
33
44
const transportOptions = config.logger.files ? [consoleLog, fileLog] : [consoleLog];
@@ -75,3 +86,8 @@ export const logError = (err: Error, context: string): void => {
75
86
logger.error(`uncaught error while ${context}: ${err.message}`);
76
87
}
77
88
};
89
90
/*logger.debug(`guess what it's called when you put an object after a message:`, {
91
thatsRight: "it's called metadata (:"
92
});
93
logError(new Error("I'm not feeling so good"), "starting up");*/