Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Winston gives an error in prodcution mode #2450

Open
vnkapoor opened this issue Apr 22, 2024 · 0 comments
Open

[Bug]: Winston gives an error in prodcution mode #2450

vnkapoor opened this issue Apr 22, 2024 · 0 comments

Comments

@vnkapoor
Copy link

馃攷 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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant