-
Notifications
You must be signed in to change notification settings - Fork 1
/
log_config.js
51 lines (47 loc) · 1.39 KB
/
log_config.js
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
const log4js = require("log4js");
const path = require("path");
const fs = require("fs");
const basePath = path.resolve(__dirname, "./logs");
const errorPath = basePath + "/errors/";
const resPath = basePath + "/responses/";
const errorFilename = errorPath + "/error";
const resFilename = resPath + "/response";
/**
* 确定目录是否存在,如果不存在则创建目录
*/
const confirmPath = function(pathStr) {
if (!fs.existsSync(pathStr)) {
fs.mkdirSync(pathStr);
console.log("createPath: " + pathStr);
}
};
log4js.configure({
appenders: {
errorLog: {
type: "dateFile", //日志类型
filename: errorFilename, //日志输出位置
alwaysIncludePattern: true, //是否总是有后缀名
pattern: "-yyyy-MM-dd.log" //后缀,每小时创建一个新的日志文件
},
responseLog: {
type: "dateFile",
filename: resFilename,
alwaysIncludePattern: true,
pattern: "-yyyy-MM-dd.log"
}
},
categories: {
errorLog: { appenders: ['errorLog'], level: 'error' },
responseLog: { appenders: ["responseLog"], level: "info" },
default: { appenders: ['responseLog','errorLog',], level: 'trace' }
},
disableClustering: true
});
//创建log的根目录'logs'
if (basePath) {
confirmPath(basePath);
//根据不同的logType创建不同的文件目录
confirmPath(errorPath);
confirmPath(resPath);
}
module.exports = log4js;