【问题标题】:Is winston logging framework is truly Asyncwinston 日志框架是真正的异步吗
【发布时间】:2014-10-25 08:45:49
【问题描述】:

我正在使用 nodejs 中的 winston 开发一个日志框架,我使用 Async 模块检查了 winston,但我需要验证它是否真的是异步性质的。

请提出建议。

【问题讨论】:

  • 赞成 - 这是一个很好的问题,虽然写得不好

标签: node.js winston


【解决方案1】:

我对 Winston 没有任何经验,但我知道它及其用途。在快速查看source on github 之后,我会说不,winston 在所有方面都不是真正的异步

  • emit 可以,但EventEmitter 不是异步的。
  • 方法具有异步风格的签名(w/callback),但并不总是异步的。

Console transport calls callback without a nextTick - 它具有异步风格的签名,但仍在同一个刻度内,即:

Console.prototype.log = function (level, msg, meta, callback) {
  if (this.silent) {
    return callback(null, true);
  }
  //...
  if (level === 'error' || level === 'debug') {
    process.stderr.write(output + '\n');
  } else {
    process.stdout.write(output + '\n');
  }

  //
  // Emit the `logged` event immediately because the event loop
  // will not exit until `process.stdout` has drained anyway.
  //
  self.emit('logged');
  callback(null, true);
};

Logger is similar to Console 如前所述,它立即发出回调而没有nextTick(或其他一些真正的异步操作)。它确实使用async module, but that is not asynchronous on all accounts either,即:

  function cb(err) {
    if (callback) {
      if (err) return callback(err);
      callback(null, level, msg, meta);
    }
    callback = null;
    if (!err) {
      self.emit('logged', level, msg, meta);
    }
  }

  async.forEach(this._names, emit, cb);

我会把它的File transport is actually async, but only because fs is 告诉温斯顿。

不要忘记“当目标是终端或文件时,控制台功能是同步的”。

【讨论】:

  • 大声笑,如果一切都是异步的,那么什么都不会发生,因为一切都会“在未来返回”:) 使用事件发射器并不是非异步操作的标志。重要的是 i/o 是异步的。
猜你喜欢
  • 2020-04-24
  • 1970-01-01
  • 2017-01-11
  • 1970-01-01
  • 2014-04-25
  • 2014-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多