我试图测试一些 winston 日志记录,以 posted Here 为例,我无法让这个工作。

我已经将它复制/粘贴到我的 js 文件中,并在 vscode 中按 F5 来测试脚本。

什么都没有记录。

对于学习者来说,当这样的例子不起作用时,它会加倍困难,因为你最终会绕着圈子试图修复自己的环境。

有人能告诉我这是否可行吗?

const { createLogger, format, transports } = require('winston');
const { combine, timestamp, label, prettyPrint } = format;

const logger = createLogger({
  format: combine(
    label({ label: 'right meow!' }),
    timestamp(),
    prettyPrint()
  ),
  transports: [new transports.Console()]
})

logger.log({
  level: 'info',
  message: 'What time is the testing at?'
});
// Outputs:
// { level: 'info',
//   message: 'What time is the testing at?',
//   label: 'right meow!',
//   timestamp: '2017-09-30T03:57:26.875Z' }


我的 nodeJS 调试控制台刚刚退出并显示
附上调试器。
等待调试器断开连接...

最佳答案

问题在于调试器配置。在 VSCode 中,您需要在配置调试器时将 "outputCapture": "std" 添加到 launch.json 文件中。

此选项允许调试器捕获从您的代码发送到 std 输出的数据。

示例配置:

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Launch Program",
      "program": "${workspaceFolder}/winston.js",
      "outputCapture": "std", // <-- ADD THIS LINE
    }
  ]
}

编辑: 忘了提到你可以直接运行你在 node 中编写的代码,它会正确记录消息。

关于javascript - nodeJS Winston 模块示例未登录到控制台,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55456007/

10-16 11:35