Search code examples
node.jspm2

How to segregate logs by date in pm2?


I am using PM2 for a NodeJS server, I have some npm commands in the package.json file which call pm2 in either production or development mode as follows:

"scripts": {
    "dev": "env NODE_ENVIRONMENT=development pm2-dev start ecosystem.config.js --ignore 'data'",
    "production": "env NODE_ENVIRONMENT=production pm2 start ecosystem.config.js"
}

When these commands are called, the ecosystem.config.js file uses either the node interpreter or the ts-node interpreter in production and development environments respectively.

const { mkdirSync } = require('fs');

var script = "";
var exec_interpreter = "";
var err_log = "";
var out_log = "";
var combined_log = "";
var date = new Date().toISOString();

if (process.env.NODE_ENVIRONMENT === 'production') {
    script = "dist/app.js";
    exec_interpreter = "node";

    mkdirSync(`./logs/${date}`, { recursive: true }, (err) => {
        if (!err) {
            err_log = './logs/' + date + '/error.log';
            out_log = './logs/' + date + '/output.log';
            combined_log = './logs/' + date + '/combined.log';
        } else { console.error(err), process.exit(1) };
    });
}

if (process.env.NODE_ENVIRONMENT === 'development') {
    script = "src/app.ts";
    exec_interpreter = "ts-node";
}

module.exports = {
    apps: [{
        name: 'Hyve',
        script: script,
        exec_interpreter: exec_interpreter,
        max_restarts: 10,
        error_file: err_log,
        out_file: out_log,
        log_file: combined_log,
        time: true
    }]
}

As you can see the logs are also directed to the log files in a production environment. But, when the script runs in the production environment, the log files do not go into separate folders as I intend them to, instead they are written to in the root of the logs folder. This will help visualize what I mean:

Intended result:

logs
├── 2021-05-30T06:56:31.477Z
│   ├── combined.log
│   ├── error.log
│   └── output.log
└── 2021-05-30T06:56:56.512Z
    ├── combined.log
    ├── error.log
    └── output.log

Actual result:

logs
├── 2021-05-30T06:56:31.477Z
├── 2021-05-30T06:56:56.512Z
├── combined.log
├── error.log
└── output.log

How can I fix this?


Solution

  • var date = new Date();
    var dat= date.getDate()+"-"+date.getMonth()+"-"+date.getFullYear();
    
    
    fs.mkdirSync(`./logs/${dat}`, { recursive: true });
    
    fs.exists("./logs/${dat}",(err) => {
        
        if (!err) {
            console.log('in');
            err_log = './logs/' + date.toISOString() + '/error.log';
            out_log = './logs/' + date.toISOString() + '/output.log';
            combined_log = './logs/' + date.toISOString() + '/combined.log';
        } else { 
            
            console.error(err)};
            console.log("created succesfully");
    });
    

    over here i have separated main folder with just the date and log file with ISOString which will help you to take time as well.