在Node.js中使用Winston在JSON对象中输出日志 [英] Outputting Logs inside JSON Object with Winston in Node.js
问题描述
所以我正在使用 winston.js 登录到文件.我已将记录器设置为以Winston默认JSON格式输出JSON.
So I am using winston.js to log to a file. I have set up the logger to output JSON with the Winston default JSON format.
这是我的Logger配置.
This is my Logger configuration.
logger.util.js:
'use strict';
const winston = require('winston');
const moment = require('moment');
const logger = winston.createLogger({
format: winston.format.json(),
transports: [
new winston.transports.File({
filename: './logs/' + moment(moment.now()).format('DD-MM-YYYY') + '-API.json',
handleExceptions: true
})
],
});
module.exports = logger;
在记录这样的事件时:
logger.log({
'level': 'info',
'timestamp': moment.now(),
'account_id': res.account_id || null,
'action': JSON.stringify(payload),
'request_id': res.request_id,
'status': success.status.code,
'route' : res.method + ' ' + res.route,
'bytes': res.socket.bytesRead || null,
'elapsed': res.elapsed || null,
});
我将以下输出输出到我的日志文件中
I get the following output into my log file
输出:
{
"level": "error",
"timestamp": 1544708669700,
"account_id": 7,
"action": "SequelizeDatabaseError: column \"alert\" does not exist",
"request_id": "27cc338b-3980-4818-a9e7-83380b1b2c3a",
"status": 500,
"route": "POST /post/new",
"bytes": 714,
"elapsed": 35
}
{
"level": "info",
"timestamp": 1544709322038,
"action": "{\"device\":{\"id\":57},\"removed\":55}",
"status": 200,
"route": "undefined undefined",
"bytes": 517,
"elapsed": null
}
我希望这样将日志记录到我的文件中.因此可以在以后搜索数据.
I want the log to out to my file like so. So the data can be searched at a later date.
所需的输出:
[
{
"level": "error",
"timestamp": 1544708669700,
"account_id": 7,
"action": "SequelizeDatabaseError: column \"alert\" does not exist",
"request_id": "27cc338b-3980-4818-a9e7-83380b1b2c3a",
"status": 500,
"route": "POST /post/new",
"bytes": 714,
"elapsed": 35
},
{
"level": "info",
"timestamp": 1544709322038,
"action": "{\"device\":{\"id\":57},\"removed\":55}",
"status": 200,
"route": "undefined undefined",
"bytes": 517,
"elapsed": null
}
]
我知道我可能需要使用Winston创建自定义格式,并且想知道是否有人可以给我一个例子?经过研究,我一直找不到足够类似的东西来工作,而且文档也没有给出类似的例子.
I understand I may need to create a custom format with Winston and wondered if someone could give me an example? After my research, I haven't been able to find something which is similar enough to work off and the docs don't give a similar example either.
推荐答案
我希望这就是您想要的.
I hope this is what you were looking for.
通过以下方式创建自定义传输:
Create a custom transport in the following way:
const Transport = require('winston-transport');
const util = require('util');
const fs = require('fs');
module.exports = class CustomTransport extends Transport {
constructor(opts) {
super(opts);
this.filename = opts.filename;
this.setup();
}
initialize() {
try {
fs.writeFileSync(this.filename, [], 'utf8');
} catch (error) {
console.log(error);
}
}
setup() {
// This checks if the file exists
if (fs.existsSync(this.filename)) {
// The content of the file is checked to know if it is necessary to adapt the array
try {
const data = fs.readFileSync(this.filename, 'utf8');
// If the content of the file is not an array, it is set
const content = JSON.parse(data);
if (!Array.isArray(content)) {
this.initialize();
}
} catch (error) {
this.initialize();
console.log(error);
}
}
// Otherwise create the file with the desired format
else {
this.initialize();
}
}
readLog() {
let data = null;
try {
data = fs.readFileSync(this.filename, 'utf8');
} catch (error) {
console.log(error);
}
return data;
}
writeLog(info) {
const data = this.readLog();
let arr = [];
if (data) {
arr = JSON.parse(data);
}
//add data
arr.push(info);
//convert it back to json
const json = JSON.stringify(arr);
try {
// Writing the array again
fs.writeFileSync(this.filename, json, 'utf8');
} catch (error) {
console.log(error)
}
}
log(info, callback) {
setImmediate(() => {
this.emit('logged', info);
});
// Perform the writing
this.writeLog(info);
callback();
}
};
,然后按照您放置的代码进行操作,以适应自定义运输工具:
and then following the code that you placed this would be the adaptation with the custom transporter:
'use strict';
const winston = require('winston');
const CustomTransport = require('./CustomTransport');
const moment = require('moment');
const logger = winston.createLogger({
format: winston.format.json(),
transports: [
new CustomTransport({
filename: moment(moment.now()).format('DD-MM-YYYY') + '-API.json',
handleExceptions: true
})
],
});
logger.log({
'level': 'info',
'timestamp': moment.now(),
'account_id': res.account_id || null,
'action': JSON.stringify(payload),
'request_id': res.request_id,
'status': success.status.code,
'route': res.method + ' ' + res.route,
'bytes': res.socket.bytesRead || null,
'elapsed': res.elapsed || null,
});
我尝试了使用伪数据的服务,这就是结果:
I tried the service with dummy data and this was the result:
[
{
"level": "info",
"timestamp": 1544723041103,
"account_id": 1,
"action": 6,
"request_id": 2,
"status": 3,
"route": 4,
"bytes": 5,
"elapsed": 5
},
{
"level": "info",
"timestamp": 1544724862768,
"account_id": 1,
"action": 6,
"request_id": 2,
"status": 3,
"route": 4,
"bytes": 5,
"elapsed": 5
}
]
这篇关于在Node.js中使用Winston在JSON对象中输出日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!