gpt4 book ai didi

node.js - 首先在nodejs Winston logger中写入时间戳

转载 作者:太空宇宙 更新时间:2023-11-03 22:05:03 33 4
gpt4 key购买 nike

我尝试让时间戳首先出现,但它总是添加到 json 的末尾。

我使用过这个配置:

    var myFormat = winston.format.combine(winston.format.timestamp({format:'YYYY-MM-DD HH:mm:ss.SSS'}),
winston.format.json());
this.winstonLogger = winston.createLogger();
this.winstonLogger.configure({
level: 'info',
format: myFormat,
transports: [
new winston.transports.Console(),
]
});

并得到如下日志:

{"level":"info","message":"app is loaded","timestamp":"2019-06-03 17:01:10.054"}

我想要的只是它看起来像:

{"timestamp":"2019-06-03 17:01:10.054","level":"info","message":"app is loaded"}

最佳答案

您可以开发自己的格式化程序

const winston = require('winston');

class TimestampFirst {
constructor(enabled = true) {
this.enabled = enabled;
}
transform(obj) {
if (this.enabled) {
return Object.assign({
timestamp: obj.timestamp
}, obj);
}
return obj;
}
}

var myFormat = winston.format.combine(
winston.format.timestamp({
format: 'YYYY-MM-DD HH:mm:ss.SSS'
}),
new TimestampFirst(true),
winston.format.json()
);

winstonLogger = winston.createLogger();
winstonLogger.configure({
level: 'info',
format: myFormat,
transports: [
new winston.transports.Console(),
]
});


winstonLogger.info('hello', {
message: 'test'
});

更多信息https://github.com/winstonjs/logform

关于node.js - 首先在nodejs Winston logger中写入时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56429016/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com