Lazy/conditional logging in JavaScript

I had some trace logs that I only wanted to enable conditionally, eg only when the logging level was set to TRACE.

I couldn’t find a node.js library that enables this kind of logging. For each of them, the logger always evaluated the string input.

Logger.trace(`long running task ${longRunningTask()}`)

The longRunningTask was always ran, even if trace logging was not enabled.


I created a fork from js-logger, which you can call with a function parameter

Logger.trace(() => `long running task ${longRunningTask()}`)

The logger checks the log level, and only then calls the function. This way the longRunningTask is only called when trace logging is enabled

Use it in your project

Add my js-logger fork with

yarn add 'js-logger@'