winstonjs / winston-mongodb

A MongoDB transport for winston
https://github.com/winstonjs/winston-mongodb
296 stars 125 forks source link

Ignoring level #226

Closed PenT1x closed 1 year ago

PenT1x commented 1 year ago

🔎 Search Terms

ignoring level, level

The problem

I have set the MongoDB transporter level to debug, but it also saves information with the info level. I thought it would only save things with the debug level.

What version of Winston presents the issue?

winston@3.9.0, winston-mongodb@5.1.1

What version of Node are you using?

v18.12.1

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

No response

Minimum Working Example

So I have my logger here

const logger = winston.createLogger({
    transports: [
        new winston.transports.Console(),
        new winston.transports.DailyRotateFile({
            filename: './logs/info-%DATE%.log',
            datePattern: 'DD-MM-YY',
            zippedArchive: true,
            maxFiles: '14d'
        }),
        new winston.transports.File({
            filename: './logs/errors.log',
            level: 'error'
        }),
        new winston.transports.MongoDB({
            level: 'debug',
            db: process.env.MONGO_URI,
        })
    ],
    format: winston.format.combine(
        winston.format.timestamp(),
        formatter
    )
})

I don't know if this helps but here is the formatter

const formatter = winston.format.printf((data) => {
    const dateNow = new Date(data.timestamp)
    if (data.level === 'debug') {
        data.date = `${dateNow.getDate()}/${dateNow.getMonth()}/${dateNow.getFullYear()}-${dateNow.getHours()}:${dateNow.getMinutes()}:${dateNow.getSeconds()}`
        return dbformatter.transform(data)
    }
    if (data.level === 'info') return `${dateNow.getHours()}:${dateNow.getMinutes()}:${dateNow.getSeconds()} - ${data.message}`;
    if (data.level === 'error') return `${dateNow.getDate()}/${dateNow.getMonth()}/${dateNow.getFullYear()}-${dateNow.getHours()}:${dateNow.getMinutes()}:${dateNow.getSeconds()} - ${data.message}`
});

Additional information

It could also just be me that have misunderstood something.

This is like my first issue on github, so I hope this is right and good enough.

curledUpSheep commented 1 year ago

@PenT1x this is simply how winston (and most logging solutions) work: you specify the lowest priority that should be logged, and the logger logs everything that has either that priority or a higher priority.

If you specify level 'info' for your transport, it will log messages with 'info' level but also 'error' etc. If you specify level 'debug' for your transport, it will log messages with 'debug' level but also 'info', 'error', etc.

From the winston documentation:

winston allows you to define a level property on each transport which specifies the maximum level of messages that a transport should log.

PenT1x commented 1 year ago

ohhh, okay. So I just put the priorities in the createLogger levels option? Can I like make custom levels by just changing the name ?

curledUpSheep commented 1 year ago

@PenT1x you might want to perform a Google search for 'winston log only one level' or similar.

Some sources I found which could be useful:

But in any case, nothing about this is specifically related to winston-mongodb.

PenT1x commented 1 year ago

Yes I see that now. This is a little weird for me right now.