Search code examples
node.jsasynchronousloggingsynchronouspm2

PM2 console.log asynchronous?


As per Node.js documentation:

The console functions are synchronous when the destination is a terminal or a file (to avoid lost messages in case of premature exit) and asynchronous when it's a pipe (to avoid blocking for long periods of time).

Now we know that pm2 is listening on data event:

process.stderr.on('data', function(data){
 var std = file. createWriteStream([LOG_PATH]);
 std.write([LOG]);
});

Does this make using console.log with pm2 asynchronous?


Solution

  • Pm2 indeed makes console.log become asynchronous.

    Though console.log and console.error are based on:

    process.stdout.write()
    process.stderr.write()
    

    Pm2 flushes the logs by using createWriteStream from your application and reloads them for display. As your snippet mentioned above, all the logs coming from your application will be written asynchronously into log folder, which is located in /root/.pm2/logs by default.

    Also, you can notice that there is an issue for discussing Pm2 logging mechanism.

    Is logging asynchronous?