logging-utils v1.0.4
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 configureLogging = logging.configureLogging;
const isLoggingConfigured = logging.isLoggingConfigured;
const configureDefaultLogging = logging.configureDefaultLogging;
const ERROR = logging.ERROR;
const WARN = logging.WARN;
const INFO = logging.INFO;
const DEBUG = logging.DEBUG;
const TRACE = logging.TRACE;
- Provide a context object on which to configure logging, e.g:
const context = { a: 1, b: 2, c: 3 };
- Configure logging on the context object
- To configure default logging on an existing object:
configureLogging(context);
- To configure WARN level logging on an existing object
configureLogging(context, WARN);
- To configure specific logging (WITHOUT overriding any existing logging on context)
configureLogging(context, DEBUG, false, console, false, false);
- To configure specific logging (OVERRIDING any existing logging on context!)
configureLogging(context, DEBUG, false, console, false, true);
- To configure simple default logging on a new object
const log = configureLogging({});
- To configure default logging on an existing object
configureDefaultLogging(context);
- To configure default logging on an existing object with an explicit logger and forceConfiguration true
configureDefaultLogging(context, console, true);
configureDefaultLogging(context, console);
configureDefaultLogging(context, undefined, true);
- To configure logging from a config object with settings under config.logging
const config = { logging: { logLevel: DEBUG, useLevelPrefixes: true, useConsoleTrace: false } };
configureLoggingFromConfig(context, config);
configureLoggingFromConfig(context, config, console);
configureLoggingFromConfig(context, config, undefined, true);
- To configure logging from a config object with settings directly under config
const config = { logLevel: DEBUG, useLevelPrefixes: true, useConsoleTrace: false };
configureLoggingFromConfig(context, config);
2. Log messages
context.error('Error message 1', new Error('Boom').stack);
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');
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
1.0.1
- Simply set core-functions dependency to 1.1.1
1.0.2
- Added an explicit
configureDefaultLogging
function for clarity. - Minor JSDoc updates
- Updated core-functions dependency to 1.2.0
1.0.3
- Removed dependency on and replaced usage of
core-functions/functions
functions with standard JS functionality - Updated
core-functions
dependency to version 2.0.0
1.0.4
- Added optional
underlyingLogger
and forceConfiguration
arguments to the configureDefaultLogging
function - Added a new
configureLoggingFromConfig
function to simplify configuring from a config object/file - Added unit tests for
configureDefaultLogging
and configureLoggingFromConfig
functions - Updated
core-functions
dependency to version 2.0.1