#2450·winston

[Bug]: Winston gives an error in prodcution mode

Author: vnkapoorCreated Apr 22, 2024Updated Apr 2, 2026
LabelsNeeds InvestigationBug

Search Terms

winston logs info only in development mode

The problem

I have implemented Winston to log all of my project

My issue is, When my app is in development mode it should log all info, warnings, errors etc.. and when my app is in production mode it should log only warnings and errors. I tried the below code when my app is in production mode, and tried to log info it gives me error.

When try to log this Logger.info({ message: "This is info" }); it gives me below error

Logger.info is undefined

What version of Winston presents the issue?

3.13.0

What version of Node are you using?

v18.17.1

If this worked in a previous version of Winston, which was it?

No response

Minimum Working Example

let logLevels = config.npm.levels
if (process.env.NODE_ENV = "production") {
    logLevels = {error: 0, warn: 1}
}


const options = {
    infoFile: {
        level: 'info',
        format: combine(
            label({ label: localConfig.APP_NAME }),
            timestamp(),
            json()
        ),
        filename: join(localConfig.LOG_PATH || join(__dirname, '..', 'logs'), 'info.log'),
        handleExceptions: true,
        json: true,
        maxsize: process.env.LOG_MAX_FILE_SIZE || 5242880, // 5MB
        maxFiles: process.env.LOG_MAX_FILES || 5,
        colorize: false
    },
    warnFile: {
        level: 'warn',
        format: combine(
            label({ label: localConfig.APP_NAME }),
            timestamp(),
            json()
        ),
        filename: join(localConfig.LOG_PATH || join(__dirname, '..', 'logs'), 'warn.log'),
        handleExceptions: true,
        json: true,
        maxsize: process.env.LOG_MAX_FILE_SIZE || 5242880, // 5MB
        maxFiles: process.env.LOG_MAX_FILES || 5,
        colorize: false
    },
    errorFile: {
        level: 'error',
        format: combine(
            label({ label: localConfig.APP_NAME }),
            timestamp(),
            json()
        ),
        filename: join(localConfig.LOG_PATH || join(__dirname, '..', 'logs'), 'error.log'),
        handleExceptions: true,
        json: true,
        maxsize: process.env.LOG_MAX_FILE_SIZE || 5242880, // 5MB
        maxFiles: process.env.LOG_MAX_FILES || 5,
        colorize: false
    }
};


const logger = createLogger({
    levels: logLevels,
    transports: [
        new transports.File(options.errorFile),
        new transports.File(options.warnFile),
        new transports.File(options.infoFile)
    ],
    exitOnError: false
})

module.exports = logger

Additional information

I think the issue is in production mode, I didn't register the info level for that reason it gives me an error. But I want to log info only in development mode. What should be best approach for this?