I am using Winston for logging with 2 different transports - File and MongoDB. I have set the level for File as "INFO" and for MongoDB as "ERROR". If I now log,
log.info('some info...');
log.warn('some Warning...');
log.error('some error...');
All of these would go to the LogFile, and only Error would go to DB. I want only Info messages to go to File, and none other.
I understand the system log levels in Winston, and that only Error goes to MongoDB because its the highest level. Since, INFO is a lower level, any log with the level INFO or higher goes to the file (as per my logger definiton)
I have read here but couldn't find an answer. Even if I create custom levels, how can I possibly restrict each transport to only one logging level?
I've answered this in another post:
According to Winston's documentation, the default behavior is to log all the messages which have at least the specifies importance aka logging level.
But there are ways to achieve your requirements.
I'll try to show you some of the possibilities, you can choose the method that works the best for you.
1. Custom Transports (Recommended):
You can create a custom transport and log only the levels you want.
Here is an example just to give you an idea:
2. Use
winston-levelonly
In the end, I would like to encourage you to read these relevant discussions:
see this answer. I have written a wrapper for winston which covers the basic api. it needs extending for logging metadata, but otherwise its a good start. of course you will have to adjust this for your MongoDB needs.
had a similiar issue and this is what I came up with. This can be done by just setting the log functions to empty functions. This is some code of mine I modified that did something similiar. This code takes customSilenceLevelLow and customSilenceLevelHigh and any log function with a value lower or equal to customSilenceLevelLow gets set to an empty function and any log function with value higher or equal to customSilenceLevelHigh gets set to an empty function
so in this code only logs of level info,info2 and info3 get logged to console. the rest do not.
NOTE: I didnt test these changes so there could be some syntax errors but the logic should be good.