Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Winston:如何等待Winston将所有日志写入特定传输(例如FileTransport)_Node.js_Logging_Winston - Fatal编程技术网

Node.js Winston:如何等待Winston将所有日志写入特定传输(例如FileTransport)

Node.js Winston:如何等待Winston将所有日志写入特定传输(例如FileTransport),node.js,logging,winston,Node.js,Logging,Winston,我正在使用库登录基于Express的web应用程序。我想要的是在api控制器中动态添加一个传输,以拦截特定请求的所有日志事件,并将累积的日志返回给客户端 不幸的是,它不容易实现,因为winston日志有时是异步的。不总是,但有时。因此,在我删除添加的传输时,可能不是所有事件都被记录下来 router.post('/engine/:id/start', async (req: express.Request, res: express.Response, next) => { const

我正在使用库登录基于Express的web应用程序。我想要的是在api控制器中动态添加一个传输,以拦截特定请求的所有日志事件,并将累积的日志返回给客户端

不幸的是,它不容易实现,因为winston日志有时是异步的。不总是,但有时。因此,在我删除添加的传输时,可能不是所有事件都被记录下来

router.post('/engine/:id/start', async (req: express.Request, res: express.Response, next) => {
  const logger = req.log; // this is a winston logger for this particular request 

  const logFilename = path.join(getTempDir(), 'log_run_' + getCurrentDateTimestamp());
  let fileTransport = new winston.transports.File({
    tailable: true,
    filename: logFilename,
    format: winston.format.printf(
        (info) => `${info.timestamp} ${info.level}: ${info.message}`,
    ),
    close: () => {
      // this one is called but at the moment of execution not all events are written (looks like a bug to me)
    }
  });
  fileTransport.on('finish', () => {
    // not ever called 
  });
  fileTransport.on('close', () => {
    // not ever called
  });
  logger.add(fileTransport);
  
  // execute main operation
  let contoller = new Controller(logger);
  await controller.run();

  // here's a problem - at the moment of removal of transport, there could be some events pending to be written, and so the log written from the file won't contain all log events.
  logger.remove(fileTransport!);
  const log = fs.readFileSync(logFilename, 'utf8');
  // send log via email
  notifyOnSuccess(log);
});
我在这里看到了很多关于如何等到FileTransport刷新后再退出进程的类似主题的问题,但这是一个完全不同的用例,因为我不需要停止在这里的日志记录。我只需要确保所有事件都已刷新

我可以想出一个解决方法-不用FileTransport,而是使用某种自定义流传输,在其中观察所有事件,并在特定事件上发送信号(将标志翻转为true)到传输移除之前的位置。但是我想使用标准的文件传输

有没有办法等到winston刷新事件队列或某个特定的传输写入所有内容后再删除它