温斯顿日志格式
Winston log format
我正在使用 Winston ^3.0.0-rc6,如下所示:
var options = {
file: {
level: 'info',
filename: `${appRoot}/logs/app.log`,
handleExceptions: true,
json: true,
prettyPrint: true,
maxsize: 5242880, // 5MB
maxFiles: 5,
colorize: true,
}
};
const jsonFormatter = (logEntry) => {
if (logEntry.type) {
const base = {
timestamp: new Date()
};
const json = Object.assign(base, logEntry);
logEntry[MESSAGE] = JSON.stringify(json);
} else {
logEntry = "";
}
return logEntry;
}
const logger = winston.createLogger({
format: winston.format(jsonFormatter)(),
transports: [
new winston.transports.File(options.file)
],
exceptionHandlers: [
new winston.transports.File(options.uncaughtExceptions)
]
});
我的日志输出:
{"timestamp":"2018-06-10T07:41:03.387Z","type":"Authentication","status":"failed","level":"error","message":"Incorrect password"}
但我希望他们像:
{
"timestamp": "2018-06-10T07:41:03.387Z",
"type": "Authentication",
"status": "failed",
"level": "error",
"message": "Incorrect password"
}
我尝试使用 json:true 和 prettyPrint,但没有成功。
谁能帮忙
谢谢。
这已被弃用:您可以检查 link here。
I tried to play around with json: true, and prettyPrint but it did not do the trick.
像这样的简单代码适合您:
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
transports: [
//
// - Write to all logs with level `info` and below to `combined.log`
// - Write all logs error (and below) to `error.log`.
//
new winston.transports.File({ filename: 'error.log', level: 'error' }),
new winston.transports.File({ filename: 'combined.log' })
]
});
如果这不起作用,请告诉我,以便我即兴创作。
我在你的代码中注意到在线
logEntry[MESSAGE] = JSON.stringify(json);
您正在使用 JSON.stringify()
,它有两个可选参数
JSON.stringify(value[, replacer[, space]])
如果您将 space
设置为您想要的空格数量,您将获得您正在寻找的输出。所以将初始行更改为:
logEntry[MESSAGE] = JSON.stringify(json, null, 2); // or 4 ;)
(replacer
参数是 null
因为我们不想更改默认行为。)
我正在使用 Winston ^3.0.0-rc6,如下所示:
var options = {
file: {
level: 'info',
filename: `${appRoot}/logs/app.log`,
handleExceptions: true,
json: true,
prettyPrint: true,
maxsize: 5242880, // 5MB
maxFiles: 5,
colorize: true,
}
};
const jsonFormatter = (logEntry) => {
if (logEntry.type) {
const base = {
timestamp: new Date()
};
const json = Object.assign(base, logEntry);
logEntry[MESSAGE] = JSON.stringify(json);
} else {
logEntry = "";
}
return logEntry;
}
const logger = winston.createLogger({
format: winston.format(jsonFormatter)(),
transports: [
new winston.transports.File(options.file)
],
exceptionHandlers: [
new winston.transports.File(options.uncaughtExceptions)
]
});
我的日志输出:
{"timestamp":"2018-06-10T07:41:03.387Z","type":"Authentication","status":"failed","level":"error","message":"Incorrect password"}
但我希望他们像:
{
"timestamp": "2018-06-10T07:41:03.387Z",
"type": "Authentication",
"status": "failed",
"level": "error",
"message": "Incorrect password"
}
我尝试使用 json:true 和 prettyPrint,但没有成功。
谁能帮忙
谢谢。
这已被弃用:您可以检查 link here。
I tried to play around with json: true, and prettyPrint but it did not do the trick.
像这样的简单代码适合您:
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
transports: [
//
// - Write to all logs with level `info` and below to `combined.log`
// - Write all logs error (and below) to `error.log`.
//
new winston.transports.File({ filename: 'error.log', level: 'error' }),
new winston.transports.File({ filename: 'combined.log' })
]
});
如果这不起作用,请告诉我,以便我即兴创作。
我在你的代码中注意到在线
logEntry[MESSAGE] = JSON.stringify(json);
您正在使用 JSON.stringify()
,它有两个可选参数
JSON.stringify(value[, replacer[, space]])
如果您将 space
设置为您想要的空格数量,您将获得您正在寻找的输出。所以将初始行更改为:
logEntry[MESSAGE] = JSON.stringify(json, null, 2); // or 4 ;)
(replacer
参数是 null
因为我们不想更改默认行为。)