為了得到日志
2021-12-27T20:46:59.136Z -> info: [socketconnection] 此訊息將包含一個完整的物件:{name: 'AAA'}
. 我創建了一個自定義格式化程式作為 winston config.js
const { createLogger, format, transports } = require('winston');
const { splat, combine, timestamp, label, printf, simple } = format;
const path = require('path');
const myFormat = printf(({ level, message, timestamp, meta }) => {
return `${timestamp} -> ${level}:\t${JSON.stringify(message)}`;
});
// define the custom settings for each transport (file, console)
const options = {
file: {
level: 'info',
filename: `${path.join(__dirname, '../logs/app.log')}`,
handleExceptions: true,
humanReadableUnhandledException: true,
json: true,
maxsize: 5242880, // 5MB
maxFiles: 5,
timestamp: true,
colorize: false,
},
console: {
level: 'debug',
handleExceptions: true,
json: true,
colorize: true,
},
};
module.exports = (moduleName) => {
let logger;
if (process.env.logging === 'off') {
logger = createLogger({
format: combine(
timestamp(),
label({ label: `${moduleName}`, message: true }),
myFormat
),
transports: [
new winston.transports.File(options.file),
],
exitOnError: false, // do not exit on handled exceptions
});
} else {
logger = createLogger({
format: combine(
timestamp(),
label({ label: `${moduleName}`, message: true }),
myFormat
),
transports: [
new transports.File(options.file),
new transports.Console(options.console),
],
exitOnError: false, // do not exit on handled exceptions
});
}
// create a stream object with a 'write' function that will be used by `morgan`
logger.stream = {
write(message) {
logger.info(message);
},
};
return logger;
};
我得到這樣的
2021-12-27T21:26:07.148Z -> info: [socketconnection] 此訊息將包含一個完整的物件:
登錄為,
logger.info('此訊息將包含一個完整的物件:', s);
其中 s 是
`var s = {'name':'AAA'};
元未附加到訊息。我在這里缺少什么
uj5u.com熱心網友回復:
#1編輯這一行:
const myFormat = printf(({ level, message, timestamp, ...meta }) => {
因為meta是一個陣列,所以你需要先傳播它才能使用它。
#2 溫斯頓的檔案說得很清楚:
除了級別和訊息之外的屬性被視為“元”。IE:
someta沒有附加到message,要使用它,請嘗試:
`${JSON.stringify(meta)}`
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/395857.html
上一篇:無法使用終端VisualStudio在本地服務器上啟動谷歌云功能
下一篇:比較詹金斯管道中的兩個串列
