node-bunyan icon indicating copy to clipboard operation
node-bunyan copied to clipboard

Question: How to create custom Levels?

Open JemiloII opened this issue 10 years ago • 6 comments

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'}

JemiloII avatar Feb 23 '15 15:02 JemiloII

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.

trentm avatar Apr 13 '15 04:04 trentm

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)?

pvoisin avatar Jul 07 '15 20:07 pvoisin

I needed a 'notice' level between info and warn; this is what I came up with: https://github.com/trentm/node-bunyan/pull/300

aahoughton avatar Sep 24 '15 16:09 aahoughton

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 :)

JemiloII avatar Sep 24 '15 19:09 JemiloII

If anyone finds this via Google like I did, here's our workaround

g-wilson avatar Feb 06 '18 12:02 g-wilson

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)
	}
}

s-a avatar Aug 25 '20 06:08 s-a