node-bunyan
node-bunyan copied to clipboard
Question: How to create custom Levels?
Just curious to how I can create a custom level name and level value?
This way I can have more useful log files.
example idea {level: 'loginError', value:51, msg: 'etc'}
Currently, no easily. While the 'level' is a number to theoretically allow using values other than the named ones... the code currently makes that difficult. The std set of level names are hardcoded in various vars in both bunyan.js and the bunyan CLI, also in dtrace integration.
To start, what you'd want is something like how the std levels are added to the Logger class:
...
Logger.prototype.fatal = mkLogEmitter(FATAL);
But the mkLogEmitter function isn't currently exported.
Precisely where I was at before finding this issue... :)
How about having a generic log emitter which would take the level as the first parameter (like Winston has)?
I needed a 'notice' level between info and warn; this is what I came up with: https://github.com/trentm/node-bunyan/pull/300
Awesome! Thanks a bunch :) Essentially I'll use this to clear out the clutter. So I can see only the messages that I want to see :)
If anyone finds this via Google like I did, here's our workaround
const bunyan = require('bunyan')
bunyan.levelFromName['info_extended'] = 35
bunyan.nameFromLevel[35] = 'info_extended'
bunyan.INFO_EXTENDED = 35
bunyan.prototype.infoEx = function infoExtended () {
const customLogLevel = 35
if (typeof arguments[0] === 'string') {
this.info.apply(this, { level: customLogLevel }, arguments)
}
if (typeof arguments[0] === 'object') {
arguments[0].level = customLogLevel
this.info.apply(this, arguments)
}
}