'How to info message and error message stored separate file using winston log file?

I tried to logging info and errors in log file using winston.If any info stored info.log. If uncaught exception errors occurred its stored only error.log file.but If commom errors are occurred its stored both file(info .log and error.log) how to fix it any one give some solution.

winston.js

const winston = require('winston');
require('winston-mongodb');
require('express-async-errors');

const winstonLogger = function () {

    winston.add(new winston.transports.Console({
        format: winston.format.combine(
        winston.format.simple(),
        winston.format.timestamp(),
        winston.format.prettyPrint(),
        winston.format.colorize()
            ),
            handleExceptions: true
        }));
    // Info Log messages to file
    winston.add(new winston.transports.File({
        filename: 'logs/info.log',
        level: 'info'
    }));
    // Error Log Messages file
    winston.add(new winston.transports.File({
        filename: 'logs/error.log',
        level: 'error',
        handleExceptions: true,
    }));
};

module.exports = winstonLogger;

I want info message stored on info.log file and error message stored on error.log file.uncaught exception error stored in separate file using winston



Solution 1:[1]

Winston logging is based on the level you specify. All logs below that level go to a specified file.

logging levels 0 to 5 (highest to lowest):

0: error
1: warn
2: info
3: verbose
4: debug
5: silly

Below is the sample code from winston documentation :

const logger = winston.createLogger({
  level: 'info',
  format: winston.format.json(),
  defaultMeta: { service: 'user-service' },
  transports: [
    //
    // - Write to all logs with level `info` and below to `combined.log` 
    // - Write all logs error (and below) to `error.log`.
    //
    new winston.transports.File({ filename: 'error.log', level: 'error' }),
    new winston.transports.File({ filename: 'combined.log' })
  ]
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 dn_c