由于对猫鼬嵌套对象进行字符串化处理,我正在尝试从Winston 2升级到3。它导致了递归调用,并崩溃了……(稍后我将为winston2打开一个问题)。我没有更改我的代码以进行字符串化,而是尝试升级winston。这样可以解决问题,并且可以成功打印。但是,我的代码也包含这样的代码存在的地方 logger.info(“像这样得到一些对象”,{一些对象}) 这在Winston 3中不再起作用。它仅打印第一部分。
winston2代码示例:
info: test1 { test: 'test2' } test3?
输出为:
const winston = require('winston');
const prettyJson = winston.format.printf(info => {
if (info.message.constructor === Object) {
info.message = JSON.stringify(info.message, null, 4)
}
console.log(info)
return `${info.timestamp} ${info.level}: ${info.message}`
});
var logger = winston.createLogger({
transports:[
// transport that prints logs into the console
new winston.transports.Console({
level: 'info',
format: winston.format.combine(
// winston.format.timestamp(),
// winston.format.colorize(),
// winston.format.prettyPrint(),
// winston.format.splat(),
winston.format.simple(),
// prettyJson
)
})
]
});
logger.info('test1',{'test':'test2'}, 'test3?');
符合预期
winston3代码:
info: test1 {"test":"test2"}
输出:SELECT b.*, s.services
FROM service_booking AS b
JOIN services AS s ON s.service_id = b.service_id
无论我尝试什么,我都无法重复winston2的相同输出。
答案 0 :(得分:0)
winston.createLogger({
transports: [
transportInfo,
],
exceptionHandlers: [
transportError,
],
rejectionHandlers: [
transportRejections,
],
exitOnError: false,
format: winston.format.combine(
winston.format.timestamp({
format: 'HH:mm:ss.SSS',
}),
winston.format.printf((info) => {
let meta = ''
let symbols = Object.getOwnPropertySymbols(info)
if (symbols.length == 2) {
meta = JSON.stringify(info[symbols[1]])
}
return `${info.timestamp} ${[info.level.toUpperCase()]}: ${info.message} ${meta}`;
})
)})
所以代码:
logger.log('ret')
logger.log('test', 'a', {
a: 9
}, [4, 5])
结果
11:57:10.739 INFO: ret
11:57:10.741 INFO: test ["a",{"a":9},[4,5]]