Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is winston logging framework is truly Async

I am developing a logging framework using winston in nodejs , I had check that winston using Async module , but I need to verify whether it is truly Async nature or not.

Please suggest.

like image 734
Indrani Sen Avatar asked Sep 01 '14 07:09

Indrani Sen


1 Answers

I have no experience with Winston, other than I'm aware of it and its purpose. After a quick review of the source on github, I'd say that no, winston is not truly async in all aspects.

  • It does emit, but EventEmitter is not async.
  • Methods have an async-style signature (w/callback), but are not always async.

The Console transport calls callback without a nextTick - it has an async-style signature, but it is still within the same tick, i.e.:

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 as mentioned in previous in that it issues callbacks immediately w/o a nextTick (or some other real async op). It does use the async module, but that is not asynchronous on all accounts either, i.e.:

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

I will give Winston that its File transport is actually async, but only because fs is.

Don't forget that "console functions are synchronous when the destination is a terminal or a file" anyways.

like image 109
zamnuts Avatar answered Sep 19 '22 03:09

zamnuts