npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

logging-utils

v4.0.25

Published

Utilities for configuring simple log level based logging functionality on an object

Downloads

286

Readme

logging-utils v4.0.25

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:

  • logging.js

This module is exported as a Node.js module.

Installation

Using npm:

$ npm i --save logging-utils

Usage

1. Configure logging:

  1. Require logging-utils
// To use the logging utilities
const logging = require('logging-utils');

// Valid logging levels
const LogLevel = logging.LogLevel; 

// Logging configuration functions
const isLoggingConfigured = logging.isLoggingConfigured;
const configureLogging = logging.configureLogging;

// Convenience logging function
const log = logging.log;
  1. Provide a context object on which to configure logging, e.g:
const context = {}; // replace with your own target object to be configured
  1. Configure logging on the context object
  • To configure default logging on an existing object (WITHOUT overriding any existing logging on context)
configureLogging(context);
// which is equivalent to:
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}; // or your own settings
configureLogging(context, settings);
// OR with explicit forceConfiguration false
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);
// Alternatively ...
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; // ... or any LoggingOptions you want to use to partially or fully override the default logging settings
configureLogging(context, {underlyingLogger: console}, options);
const CustomLogger = {/* ... */}; // implement your own custom logger if required
configureLogging(context, {underlyingLogger: CustomLogger}, options, true);
  • To configure logging from options
const options = { logLevel: LogLevel.DEBUG, useLevelPrefixes: true, useConsoleTrace: false }; // replace with your own options
configureLogging(context, undefined, options);
// OR just ...
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);
// OR with explicit forceConfiguration false ...
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.
// For unit testing, set the `LOG_LEVEL` environment variable programmatically
process.env.LOG_LEVEL = LogLevel.DEBUG;

// Alternatively, if you configured your own `envLogLevelName` as 'MyLogLevel', e.g.
configureLogging(context, {envLogLevelName: 'MyLogLevel'});
// then for unit testing, set your `MyLogLevel` environment variable programmatically
process.env.MyLogLevel = LogLevel.TRACE;

2. Log messages

  • To log errors:
// Log an error with a strack trace
context.error('Error message 1', new Error('Boom'));

// Log an error without a stack trace
context.error('Error message 2');
  • To log warnings:
// Log a warning (or do nothing when warnings are disabled)
context.warn('Warning message 1');

// To avoid building the warning message (when warnings are disabled)
if (context.warnEnabled) context.warn('Warning message 2');
  • To log info messages:
// Log an info message (or do nothing when info messages are disabled)
context.info('Info message 1');

// To avoid building the info message (when info messages are disabled)
if (context.infoEnabled) context.info('Info message 2');
  • To log debug messages:
// Log a debug message (or do nothing when debug messages are disabled)
context.debug('Debug message 1');

// To avoid building the debug message (when debug messages are disabled)
if (context.debugEnabled) context.debug('Debug message 2');
  • To log trace messages:
// To log a trace message (or do nothing when trace messages are disabled)
context.trace('Trace message 1');

// To avoid building the trace message (when trace messages are disabled)
if (context.traceEnabled) context.trace('Trace message 2');
  • To log messages at a specified log level (using the log method):
// To log a message at LogLevel.TRACE (or do nothing when trace messages are disabled)
context.log(LogLevel.ERROR, 'Error message 1', new Error('Boom'));

// Note that this will also work with console, but you won't get any suppression according to log level
console.log(LogLevel.TRACE, 'Trace message 1');
  • To log messages at a specified log level (using the log function):
// Alternatively using log function
log(context, LogLevel.DEBUG, 'Debug message 1');

// Note that this will also work with console (and undefined), but you won't get any suppression according to log level
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 v6.10.3.

See the package source for more details.

Changes

See CHANGES.md