Search code examples
loggingpm2

Can't merge the standard and error log in PM2


I'm trying to have one file where all the log is saved, so I'm starting my app using

pm2 start app --merge-logs -l --log "~/webapps/infranodus/infranodus/infralogpm2.log"

the app starts but there's nothing in the file, only the standard logs are created.

i tried to touch create the file but that doesn't help either.


Solution

  • I tried few checks on RHEL 7.4 using NodeJS 10.15 and PM2 3.2.4 and following HelloWorld ExpressJS code.

    var http = require("http");
    
    http.createServer(function (request, response) {
        // Send the HTTP header 
        // HTTP Status: 200 : OK
        // Content Type: text/plain
        response.writeHead(200, {'Content-Type': 'text/plain'});
    
        // Send the response body as "Hello World"
        response.end('Hello World\n');
    }).listen(8081);
    
    // Console will print the message
    console.log('Server running at http://127.0.0.1:8081/');
    console.error('This is on STDERR');
    

    Assuming above code is within ./bin/app2 file, and required modules are present under node_modules,

    I ran application as pm2 start ./bin/app2 --merge-logs -l --log /var/tmp/sample-app2.log and other combinations described below.

    1. --merge-logs is not desired for a single instance. As per pm2 --help, it says "merge logs from different instances but keep error and out separated". Though in my case, STDOUT and STDERR, both appeared in desired log file, with and without --merge-logs option.

    2. -l and --log both should not be used. Ideally, you should have used only one of it as per Unix command line argument conventions. However, in my case, it worked the way you have suggested as well as only with --log.

    3. As part of counter check, ran pm2 start ./bin/app2 --output /var/tmp/sample-app2-stdout.log --error /var/tmp/sample-app2-error.log. It generates two files with output from console.log and console.error respectively.

    In your case, I suggest you check that adequate disk space is available on partition where log file is stored, and do pm2 delete app and then try again.

    If that does not resolve issue, is it possible to share a sample code and exact steps to reproduce a problem?