[英]Winston logging - pretty JSON format in Winston 3.x
幾年前我和溫斯頓一起工作。 當我們在 localhost 上開發時,我們的 winston 配置為 output 格式很好的 JSON 易於閱讀。
2.x 溫斯頓
npm install winston@2.2.0
const winston = require('winston');
const logger = new winston.Logger({
transports: [new (winston.transports.Console)({ json: true })],
});
logger.info('please', { iam: 'good' });
try {
throw new Error('ooh noo');
} catch (err) {
logger.error('Not good error', err);
}
有這個 output
{
"iam": "good",
"level": "info",
"message": "please"
}
{
"message": "Not good error",
"stack": "Error: ooh noo\n at Object.<anonymous> (C:\\Users\\libor\\WebstormProjects\\untitled\\usewinston.js:30:11)\n at Module._compile (internal/modules/cjs/loader.js:1063:30)\n at Object.Module._extensions..js (internal/
modules/cjs/loader.js:1092:10)\n at Module.load (internal/modules/cjs/loader.js:928:32)\n at Function.Module._load (internal/modules/cjs/loader.js:769:14)\n at Function.executeUserEntryPoint [as runMain] (internal/modules/run_
main.js:72:12)\n at internal/main/run_main_module.js:17:47",
"level": "error"
}
3.x 溫斯頓
npm install winston
(寫本文時最新版本或3.3.3)
const winston = require('winston');
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
});
logger.add(new winston.transports.Console({
format: winston.format.json(),
}));
logger.info('please', { iam: 'good' });
try {
throw new Error('ooh noo');
} catch (err) {
logger.error('Not good error', err);
}
有這個 output
{"iam":"good","level":"info","message":"please"}
{"level":"error","message":"Not good error ooh noo","stack":"Error: ooh noo\n at Object.<anonymous> (C:\\Users\\libor\\WebstormProjects\\untitled\\usewinston.js:21:11)\n at Module._compile (internal/modules/cjs/loader.js:1063:30)
\n at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)\n at Module.load (internal/modules/cjs/loader.js:928:32)\n at Function.Module._load (internal/modules/cjs/loader.js:769:14)\n at Function.executeU
serEntryPoint [as runMain] (internal/modules/run_main.js:72:12)\n at internal/main/run_main_module.js:17:47"}
問題
是否有一些本機/簡單的方法來獲得相同的 output 就像我在最新(3.3.3)版本中的 2.x 版本中一樣? 如果沒有,您知道實現它的最佳方法嗎?
獎金問題
Webstorm 無法檢測(在上述示例中)stacktrace 中的文件和行,因此它不可點擊。 當我只是“console.error”時,堆棧跟蹤如下所示:
我可以直接單擊該文件以在導致錯誤的行中導航到該文件。 你知道如何在 Winston 3.x 中實現它嗎?
我用自定義格式化程序解決了它,如下所示
const winston = require('winston');
const _ = require('lodash');
const logger = winston.createLogger({
level: 'info',
});
const logStackAndOmitIt = winston.format((info, opts) => {
if (info.stack){
console.error(info.stack);
return _.omit(info, 'stack');
}
return info;
});
logger.add(new winston.transports.Console({
format: winston.format.combine(
logStackAndOmitIt(),
winston.format.prettyPrint(),
),
})
);
const arr = Array(50).fill(20)
logger.info('pleases', { iam: 'abc', arr });
try {
throw new Error('ooh noo');
} catch (err) {
logger.error('Not good error', err);
}
哪個有這個 output
{
iam: 'abc',
arr: [
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
20, 20, 20, 20, 20, 20
],
level: 'info',
message: 'pleases'
}
Error: ooh noo
at Object.<anonymous> (C:\Users\libor\WebstormProjects\untitled\usewinston.js:71:11)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at internal/main/run_main_module.js:17:47
{ level: 'error', message: 'Not good error ooh noo' }
在 Webstorm 中包含可點擊的鏈接
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.