【node】log4js日志

官方文档

https://log4js-node.github.io/log4js-node/

常用配置代码

更多详细参数查看官方文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import path from "node:path";
import log4js from "log4js";

log4js.configure({
appenders: {
// 标准输出流
out: { type: "stdout" },
// 日期文件输出
dateFile: {
type: "dateFile",
filename: path.resolve(import.meta.dirname, "../", "logs", "dateFile", "log"),
pattern: "yyyy-MM-dd_hh-mm-ss.log",
alwaysIncludePattern: true,
fileNameSep: '.',
encoding: "utf-8",
maxLogSize: "10M",
numBackups: 3,
mode: 0o666,
layout: {
type: "basic",
},
},
// 文件输出
file: {
type: "file",
filename: path.resolve(import.meta.dirname, "../", "logs", "file", "log.log"),
maxLogSize: "10M",
backups: 5,
fileNameSep: ".",
encoding: "utf-8",
mode: 0o666,
keepFileExt: true,
layout: {
type: "basic",
},
},
// 文件输出(同步)
fileSync: {
type: "fileSync",
filename: path.resolve(import.meta.dirname, "../", "logs", "fileSync", "log.log"),
maxLogSize: "10M",
backups: 5,
encoding: "utf-8",
mode: 0o666,
layout: {
type: "basic",
},
},
},
categories: {
default: { appenders: ["out"], level: "all" },
dateFile: { appenders: ["dateFile"], level: "all" },
file: { appenders: ["file"], level: "all" },
fileSync: { appenders: ["fileSync"], level: "all" },
},
});

export const logger = log4js.getLogger("default");
export const loggerDateFile = log4js.getLogger("dateFile");
export const loggerFile = log4js.getLogger("file");
export const loggerFileSync = log4js.getLogger("fileSync");