我正在 nodejs 中使用 winston 开发一个日志框架,我已经检查过 winston 使用 Async 模块,但我需要验证它是否是真正的异步性质。
请提出建议。
最佳答案
我没有使用过 Winston,只是知道它及其用途。在快速查看 source on github 之后,我会说不,winston 在所有方面都不是真正的异步。
- 它会
发出
,但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);
我会给 winston 它的 File
transport is actually async, but only because fs
is .
无论如何不要忘记“当目标是终端或文件时,控制台功能是同步的”。
关于node.js - winston 日志记录框架是真正的异步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25600459/