Unitech / pm2

Node.js Production Process Manager with a built-in Load Balancer.
https://pm2.keymetrics.io/docs/usage/quick-start/
Other
41.46k stars 2.62k forks source link

Retain blank lines in log stream #3237

Open shanehughes3 opened 7 years ago

shanehughes3 commented 7 years ago

What's going wrong?

Currently, if any process tries to log a blank line (i.e. \n\n), the line is removed in streaming logs yet retained in the log file and in the tail function.

The Log.*stream methods in lib/API/Log.js are checking for general falsiness of each element after splitting a chunk on \n, presumably to remove the resulting last element that will be an empty string (since the methods print a newline at the end of input anyway). Unfortunately, since the chunk is split on \n, any two newlines in a row will result in an element that is an empty string. The log methods then will skip over the element - not printing anything - thereby missing the extra newline.

I submitted #3193 to fix this issue, but it was closed in lieu of 4f5566e. Unfortunately, that commit only reinforces the problem - any element with a length of zero (any empty string) would be passed over. In fact, any element with a length of 0 would also return false on the first test (!line), thereby making it redundant.

How could we reproduce this issue?

In any instance, try logging a blank line (console.log('foo\n\nbar')). If tailed, the log will produce the correct output:

0|test- | foo
0|test- | 
0|test- | bar

... but when streaming, the newline will be eliminated:

0|test- | foo
0|test- | bar

Supporting information

$ pm2 report
===============================================================================
--- PM2 REPORT (Wed Oct 25 2017 13:54:38 GMT+0000 (UTC)) ----------------------
===============================================================================
--- Daemon -------------------------------------------------
pm2d version         : 2.6.1
node version         : 8.4.0
node path            : undefined
argv                 : /home/ubuntu/.nvm/versions/node/v8.4.0/bin/node,/home/ubuntu/.nvm/versions/node/v8.4.0/lib/node_modules/pm2/lib/Daemon.js
argv0                : node
user                 : ubuntu
uid                  : 1000
gid                  : 1000
uptime               : 8903min
===============================================================================
--- CLI ----------------------------------------------------
local pm2            : 2.6.1
node version         : 8.4.0
node path            : /home/ubuntu/.nvm/versions/node/v8.4.0/bin/pm2
argv                 : /home/ubuntu/.nvm/versions/node/v8.4.0/bin/node,/home/ubuntu/.nvm/versions/node/v8.4.0/bin/pm2,report
argv0                : node
user                 : ubuntu
uid                  : 1000
gid                  : 1000
===============================================================================
--- System info --------------------------------------------
arch                 : x64
platform             : linux
type                 : Linux
cpus                 : Intel(R) Xeon(R) CPU E5-2676 v3 @ 2.40GHz
cpus nb              : 1
freemem              : 152821760
totalmem             : 1039011840
home                 : /home/ubuntu
===============================================================================
vmarchaud commented 6 years ago

My bad i misunderstood the problem, if i understand correctly, the normal behavior should be to output even empty lines if both the logs and the streaming ?

mogita commented 6 years ago

Noticed the same issue just now. I used a \n to separate blocks of logs, but the output didn't work as expected. +1 for retaining blank lines.

shanehughes3 commented 6 years ago

@vmarchaud Correct, expected behavior is to print an empty line on both logs and streaming if one exists, i.e. if a chunk of log data contains \n\n. Currently this does happen in the trailing logs, but in streaming, the newline is omitted - any extra \ns beyond the first are removed.

kentnek commented 3 years ago

Is there any update on this? I also want to see the blank lines in my app's beautifully crafted logs.