0

I'm using PM2 to manage my node js application. Here i want to configure custom log path which is unique to the server/node that pm2 is running

module.exports = {
    apps : [{
            name: 'nodeapp-dev',
            script: 'src/index.js',

            instances: 1,
            exec_mode: 'fork',
            autorestart: true,
            watch: false,
            max_memory_restart: '512M',
            error_file: '/home/user/.pm2/logs/$HOSTNAME/nodeapp-dev-error.log',
            out_file: '/home/user/.pm2/logs/$HOSTNAME/nodeapp-dev-out.log',
            env: {
                    NODE_ENV: 'dev'
            }
        }]
}

I want to write the log files to folder created by sever hostname and this should be work on a other server as well. could you help me to achieve this as $HOSTNAME didn't work for me. it just created folder "$HOSTNAME". But in in linux systems has the env variable for $HOSTNAME

PM2 show output

| error log path    │ /home/user/.pm2/logs/$HOSTNAME/nodeapp-dev-error.log │
│ out log path      │ /home/user/.pm2/logs/$HOSTNAME/nodeapp-dev-out.log   

1 Answers1

0

Our development manger helped me on this as I'm not an expert on node js.

error_file: `/home/user/.pm2/logs/${process.env['HOSTNAME']}/bluster-error.log`,
out_file: `/home/user/.pm2/logs/${process.env['HOSTNAME']}/bluster-out.log`,

We need to use template literals https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

  • 1
    Well, for me, this just creates an undefined file. In your example it would look like this: /home/user/.pm2/logs/undefined/bluster-error.log Any help where did you set up the HOSTNAME variable? – Bert Sep 23 '21 at 13:13