gpt4 book ai didi

javascript - Winston 日志记录 - Winston 3.x 中漂亮的 JSON 格式

转载 作者:行者123 更新时间:2023-12-03 08:30:28 25 4
gpt4 key购买 nike

几年前我和 winston 一起工作。当我们在 localhost 上进行开发时,我们的 winston 被配置为输出格式良好、易于阅读的 JSON。

2.x winston

npm install <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e3948a8d90978c8da3d1cdd1cdd3" rel="noreferrer noopener nofollow">[email protected]</a>

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);
}

有这个输出

{
"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 winston

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);
}

有这个输出

{"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"}

问题

是否有一些 native /简单的方法可以像我在最新(3.3.3)版本中的 2.x 版本中获得相同的输出?如果没有,您知道实现这一目标的最佳方法吗?

奖励问题

Webstorm 无法检测(在上述两个示例中)堆栈跟踪中的文件和行,因此它不可单击。当我只是“console.error”时,堆栈跟踪如下所示:

webstorm

我可以直接单击该文件以导航到导致错误的行中的该文件。你知道如何在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);
}

哪个有这个输出

{
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 中包含可点击的链接

关于javascript - Winston 日志记录 - Winston 3.x 中漂亮的 JSON 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65277652/

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