1.1.0 • Published 6 years ago
bunyan-debug-glob v1.1.0
bunyan-debug-glob
Description
bunyan-debug-glob is a simple wrapper around the Bunyan logging library and
inspired by the debug utility, adding the ability to set loggers levels
through a given string (eg. process.env.LOG
) using glob patterns.
You can then change the level of any logger you use in your application with ease and modularity.
Install
npm install --save bunyan-debug-glob
Usage
Example
In your application:
// You don't really need to pass process.env.LOG as argument as this is the default. And, of course, you can use here
// any string variable you need.
const loggerFactory = require("bunyan-debug-glob")(process.env.LOG);
// Now use loggerFactory just like you would do with Bunyan
const httpLogger = loggerFactory.createLogger({"name": "my-app/http"});
const usersLogger = loggerFactory.createLogger({"name": "my-app/users"});
/* HTTP */
httpLogger.info("Info from HTTP logger");
httpLogger.error("Error from HTTP logger");
/* Users */
usersLogger.info("Info from users logger");
Now you can easily set your loggers level using the LOG
environment variable:
# All loggers set to 'warn' level except 'app/http' set to 'debug'
LOG="**/*=warn;my-app/http=debug" node .
Levels
The levels string defines what log level should be set to the different loggers, using the following format:
<log_name_pattern>=<log_level>[;<...>]
<log_name_pattern>
is a glob pattern to match against loggers names.<log_level>
is the level to apply to loggers whose name matches the glob pattern. Levels are level names defined by Bunyan, to which we added the levelnone
meaning no log should be output from the corresponding logger(s). An empty log level defaults tonone
. By default, the factory will use theLOG
environment variable if it exists, or an empty string (""
) if not (in which case all loggers will have their level set to"none"
).
Examples:
"**/*=warn" // All loggers levels set to `warn`
"**/*=warn;app/server=debug" // All loggers levels set to `warn` but logger `app/server` set to `debug`
"**/*=warn;app/**/*=debug" // All loggers levels set to `warn` but loggers whose name starts with `app/` set to debug
"**/*=warn;app/**/debug=debug" // All loggers levels set to `warn` but loggers whose name ends with `/debug` set to debug