logging-utils v4.0.19
Utilities for configuring simple log level based logging functionality on an object.
The log levels supported are the following:
- ERROR - only logs error messages
- WARN - only logs warning and error messages
- INFO - logs info, warning and error messages
- DEBUG - logs debug, info, warning and error messages
- TRACE - logs trace, debug, info, warning and error messages (i.e. all)
Main module:
This module is exported as a Node.js module.
Installation
Using npm:
$ npm i --save logging-utils
Usage
1. Configure logging:
- Require logging-utils
const logging = require('logging-utils');
const LogLevel = logging.LogLevel;
const isLoggingConfigured = logging.isLoggingConfigured;
const configureLogging = logging.configureLogging;
const log = logging.log;
- Provide a context object on which to configure logging, e.g:
const context = {};
- Configure logging on the context object
- To configure default logging on an existing object (WITHOUT overriding any existing logging on context)
configureLogging(context);
configureLogging(context, {logLevel: LogLevel.INFO});
- To configure WARN level logging on an existing object (WITHOUT overriding any existing logging on context)
configureLogging(context, {logLevel: LogLevel.WARN});
- To configure specific logging (WITHOUT overriding any existing logging on context)
const settings = {logLevel: LogLevel.DEBUG, useLevelPrefixes: false, useConsoleTrace: false, underlyingLogger: console};
configureLogging(context, settings);
configureLogging(context, settings, undefined, false);
- To configure specific logging (OVERRIDING any existing logging on context!)
configureLogging(context, settings, undefined, true);
- To configure default logging on a new object
const log = configureLogging({});
- To configure default logging on an existing object with overriding options and forceConfiguration true
configureLogging(context, undefined, options, true);
configureLogging(context, options, undefined, true);
- To configure default logging on an existing object with overriding options, an explicit logger and forceConfiguration true
const options = undefined;
configureLogging(context, {underlyingLogger: console}, options);
const CustomLogger = {};
configureLogging(context, {underlyingLogger: CustomLogger}, options, true);
- To configure logging from options
const options = { logLevel: LogLevel.DEBUG, useLevelPrefixes: true, useConsoleTrace: false };
configureLogging(context, undefined, options);
configureLogging(context, options);
- To configure logging from EITHER logging settings OR logging options (OR defaults if neither) - WITHOUT overriding any existing logging on context
configureLogging(context, settings, options);
configureLogging(context, settings, options, false);
- To configure logging from EITHER logging settings OR logging options (OR defaults if neither) - OVERRIDING any existing logging on context!
configureLogging(context, settings, options, true);
- To OVERRIDE any pre-configured
logLevel
setting or option during runtime configuration, set a logging level on
the environment variable named by the envLogLevelName
setting, which is also configurable and defaults to 'LOG_LEVEL'
.
Any valid logLevel
found with process.env[envLogLevelName]
will take precedence over any other logLevel
setting or option.
process.env.LOG_LEVEL = LogLevel.DEBUG;
configureLogging(context, {envLogLevelName: 'MyLogLevel'});
process.env.MyLogLevel = LogLevel.TRACE;
2. Log messages
context.error('Error message 1', new Error('Boom'));
context.error('Error message 2');
context.warn('Warning message 1');
if (context.warnEnabled) context.warn('Warning message 2');
context.info('Info message 1');
if (context.infoEnabled) context.info('Info message 2');
context.debug('Debug message 1');
if (context.debugEnabled) context.debug('Debug message 2');
context.trace('Trace message 1');
if (context.traceEnabled) context.trace('Trace message 2');
- To log messages at a specified log level (using the
log
method):
context.log(LogLevel.ERROR, 'Error message 1', new Error('Boom'));
console.log(LogLevel.TRACE, 'Trace message 1');
- To log messages at a specified log level (using the
log
function):
log(context, LogLevel.DEBUG, 'Debug message 1');
log(console, LogLevel.WARN, 'Warn message 1');
log(undefined, LogLevel.ERROR, 'Error message 1', new Error('Boom 2'));
Unit tests
This module's unit tests were developed with and must be run with tape. The unit tests have been tested on Node.js v4.3.2.
See the package source for more details.
Changes
See release_notes.md