how to manage logging using winston for production and development in node js "github" 1

how to manage logging using winston for production and development in node js “github”

const winston = require('winston');
const { createLogger, format, transports } = require('winston');

const dotenv = require('dotenv');
dotenv.config();

var logger = createLogger({
    level: 'info',
    format: format.combine(
        format.timestamp({
            format: 'YYYY-MM-DD HH:mm:ss',
        }),
        format.printf((info) =>
            JSON.stringify({
                t: info.timestamp,
                l: info.level,
                m: info.message,
                s: info.splat !== undefined ? `${info.splat}` : '',
            }) + ','
        )
    ),
});

if (process.env.NODE_ENV !== 'PRODUCTION') {
    logger.add(new transports.Console({ format: winston.format.cli() }));

    // Turn these on to create logs as if it were production
    // logger.add(new transports.File({ filename: 'log/output/error.log', level: 'error' }));
    // logger.add(new transports.File({ filename: 'log/output/warn.log', level: 'warn' }));
    // logger.add(new transports.File({ filename: 'log/output/info.log', level: 'info' }));
} else {
    logger.add(new transports.File({ filename: 'log/output/error.log', level: 'error' }));
    logger.add(new transports.File({ filename: 'log/output/warn.log', level: 'warn' }));
    logger.add(new transports.File({ filename: 'log/output/info.log', level: 'info' }));
}

module.exports = {
    logger,
};

Here is what the above code is Doing:
1. We’re creating a logger object using the winston library.
2. We’re setting the log level to info.
3. We’re setting the format of the log to be a JSON string.
4. We’re checking if the environment is production.
5. If it’s not production, we’re adding a console transport to the logger.
6. If it’s production, we’re adding a file transport to the logger.

Now, let’s create a logger.js file in the root of the project and paste the above code in it.

Next, let’s create a log folder in the root of the project. Inside the log folder, let’s create an output folder.

Now, let’s create a logger.js file in the root of the project and paste the above code in it.

Next, let’s create a log folder in the root of the project. Inside the log folder, let’s create an output folder.

Now, let’s create a logger.js file in the root of the project and paste the above code in it.

Next, let’s create a log folder in the root of the project. Inside the log folder, let’s create an output folder.

Now, let’s create a logger.js file in the root of the project and paste the above code in it.

Next, let’s create a log folder in the root of the project. Inside the log folder, let’s create an output folder.

Now, let’s create a logger.js file in the root of the project and paste the above code in it.

Next, let’s create a log folder in the root of the project. Inside the log folder, let’s create an output folder.

Now, let’s create a logger.js file in the root of the project and paste the above code in it.

Next, let’s create a log folder in the root of the project. Inside the log folder, let’s create an output folder.

Now, let’s create a logger.js file in the root of the project and paste the above code in it.

Next, let’s create a log folder in the root of the project. Inside the log folder, let’s create an output folder.

Now, let’s create a logger.js file in the root of the project and paste the above code in it.

Next, let’s create a log folder in the root of the project. Inside the log folder, let’s create an output folder.

Now, let’s create a logger.js file in the root of the project and paste the above code in it.

Next, let’s create a log folder in the root of the project. Inside the log folder, let’s create an output folder.

Now, let’s create a logger.js file in the root of the project and paste the above code in it.

Next, let’s create a log folder in the root of the project. Inside the log folder, let’s create an output folder.

Now, let’s create a logger.js file in the root of the project and paste the above code in it.

Next, let’s create a log folder in the root of the project. Inside the log folder, let’s create an output folder.

Now, let’s create a logger.js file in the root of the project and paste the above code in it.

Next, let’s create a log folder in the root of the project. Inside the log folder, let’s create an output folder.

Now, let’s create a logger.js file in the root of the project and paste the above code in it.

Next, let’s create a log folder in the root of the project. Inside the log folder, let

Similar Posts