nodeJS Winston 模块示例未记录到控制台

nodeJS Winston module example not logging to console

我正在尝试测试一些 winston 日志记录,以示例为例 posted Here 我无法使它正常工作。

我已经 copy/pasted 它到我的 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 文件。

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

示例配置:

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

编辑: 忘记提及您可以 运行 您直接在节点中编写的代码,它会正确记录消息。