I use process.stdout.write
in the output of the program so that the output is in one line.
See this example:
while(true) {
process.stdout.write("X");
process.stdout.write("Y");
console.log("");
await new Promise(resolve => setTimeout(resolve, 1000));
}
I try to see the output with pm2 log
. I expect XY
to fall together in a line. But in practice, sometimes Y
appears in the next line:
# pm2 log app
60|app | XY
60|app | XY
60|app | XY
60|app | XY
60|app | XY
60|app | X
60|app | Y
60|app | XY
60|app | XY
60|app | XY
60|app | XY
60|app | XY
60|app | XY
60|app | XY
60|app | XY
60|app | XY
60|app | X
60|app | Y
60|app | X
60|app | Y
60|app | XY
60|app | XY
60|app | XY
60|app | XY
But they are saved correctly in the log file:
# tailf -f /root/.pm2/logs/app-out.log
XY
XY
XY
XY
XY
XY
XY
In the next example, I add a very short pause of 1ms
:
while(true) {
process.stdout.write("X");
await new Promise(resolve => setTimeout(resolve, 1)); //added this line
process.stdout.write("Y");
console.log("");
await new Promise(resolve => setTimeout(resolve, 1000));
}
You can see that the output is permanently spaced between X
and Y
, which makes the problem worse:
# pm2 log app
60|app | X
60|app | Y
60|app | X
60|app | Y
60|app | X
60|app | Y
60|app | X
60|app | Y
How can I always be sure in the pm2 log
command that the output logs are used wherever process.stdout.write
is used and that it doesn’t appear until n
or console.log
is reached on the next line. Just like what is saved in the file and I can see it with the tail -f
command?