我正在使用winston,expressWinston,winston-mongoDB来管理我的NodeJs应用程序中的日志。
在使用winston-mongodb传输和express-winston时,我可以用以下格式将日志存储到我的mongoDB集合中。(meta:true)
{
"_id" : ObjectId("5a124f9781d911337ebeb98d"),
"timestamp" : ISODate("2017-11-20T03:44:23.336Z"),
"level" : "info",
"message" : "GET /stylesheets/bootstrap.css 304 2ms",
"meta" : {
"res" : {
"statusCode" : 304
},
"req" : {
"url" : "/stylesheets/bootstrap.css",
"headers" : {
"host" : "localhost:3000",
"connection" : "keep-alive",
},
"method" : "GET",
"httpVersion" : "1.1",
"originalUrl" : "/stylesheets/bootstrap.css",
"query" : {}
},
"responseTime" : 2
}我可以获取元数据中的请求/响应信息。
这些细节有没有可能直接成为集合的一部分?,类似于:
{
"_id" : ObjectId("5a12537d81b6b634cb7d4696"),
"timestamp" : ISODate("2017-11-20T04:01:01.229Z"),
"level" : "info",
"message" : "GET /stylesheets/bootstrap.css 304 11ms",
"status": 200,
"requestUrl": '/',
"requestMethod": 'GET',
"remoteIp": '::1'
"meta" : {}}
发布于 2018-01-15 18:45:06
您可以覆盖默认的日志函数实现,以保存您想要的任何内容
let logger = new winston.Logger(options);
logger.log = function () {
var args = arguments;
// here you can transform your meta obj
winston.Logger.prototype.log.apply(this, args);
};希望它能对你有所帮助
https://stackoverflow.com/questions/47385078
复制相似问题