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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: How to create custom Levels? #228

Open
JemiloII opened this issue Feb 23, 2015 · 6 comments
Open

Question: How to create custom Levels? #228

JemiloII opened this issue Feb 23, 2015 · 6 comments

Comments

@JemiloII
Copy link

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

@trentm
Copy link
Owner

trentm commented Apr 13, 2015

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.

@pvoisin
Copy link

pvoisin commented Jul 7, 2015

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

@aahoughton
Copy link

I needed a 'notice' level between info and warn; this is what I came up with: #300

@JemiloII
Copy link
Author

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

@g-wilson
Copy link

g-wilson commented Feb 6, 2018

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

@s-a
Copy link

s-a commented Aug 25, 2020

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

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

6 participants