functions that generate log messages as optimization

31 views
Skip to first unread message

Jordan Baker

unread,
Dec 27, 2016, 5:50:38 PM12/27/16
to bunyan-logging
Hi everyone,

Been using bunyan a couple months now and really liking a lot of things about it!

I'm running into a fairly common pattern in my code where I'd like to be able to configure some run-time debugging but the generation of the actual log data is pretty expensive.

So what I'd love to do is be able to pass a function as the 1st argument that would generate the arguments. Like so:


  logger.debug( () => [ { a: 1, b: this.somethingExpensive()}, 'my message', {extra: 'stuff'} ] )


This would only execute the function if debug was an allowable log level.

Is something like this possible ? Would it be useful for anyone else?

Thanks

Jordan

Trent Mick

unread,
Dec 28, 2016, 1:52:58 AM12/28/16
to Jordan Baker, bunyan-logging
Does using:

if (logger.debug())
logger.debug(msg, args...)

work for you? Without args is a special case for the log methods to just return a bool for whether that log level is enabled.

--Trent
Reply all
Reply to author
Forward
0 new messages