Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

logging-utils

Package Overview
Dependencies
Maintainers
1
Versions
54
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

logging-utils

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

  • 2.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
59
increased by11.32%
Maintainers
1
Weekly downloads
 
Created
Source

logging-utils v2.0.0

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 utilties
const logging = require('logging-utils');

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

// Log level constants
const ERROR = logging.ERROR;
const WARN = logging.WARN;
const INFO = logging.INFO;
const DEBUG = logging.DEBUG;
const TRACE = logging.TRACE;
  1. Provide a context object on which to configure logging, e.g:
const context = { a: 1, b: 2, c: 3 }; // replace with your own target object to be configured
  1. Configure logging on the context object
  • To configure default logging on an existing object:
configureDefaultLogging(context);
  • To configure WARN level logging on an existing object
configureLogging(context, {logLevel: WARN});
  • To configure specific logging (WITHOUT overriding any existing logging on context)
const settings = {logLevel: DEBUG, useLevelPrefixes: false, useConsoleTrace: false, underlyingLogger: console};
configureLogging(context, settings, false);
  • To configure specific logging (OVERRIDING any existing logging on context!)
configureLogging(context, settings, true);
  • To configure simple default logging on a new object
const log = configureDefaultLogging({});
  • To configure default logging on an existing object with an explicit logger and forceConfiguration true
configureDefaultLogging(context, console, true);

// Alternatives specifying only underlying logger or forceConfiguration 
configureDefaultLogging(context, console);
configureDefaultLogging(context, undefined, true);
  • To configure logging from a config object (or file) with logging options under config.loggingOptions
const config = { loggingOptions: { logLevel: DEBUG, useLevelPrefixes: true, useConsoleTrace: false } }; // replace with your own config object
const loggingSettings = getLoggingSettingsOrDefaults(config.loggingOptions);
configureLogging(context, loggingSettings);

// Alternatives specifying only optional underlying logger or forceConfiguration 
configureLogging(context, loggingSettings, console);
configureLogging(context, loggingSettings, undefined, true);
  • To configure logging from logging options
const options = { logLevel: DEBUG, useLevelPrefixes: true, useConsoleTrace: false }; // replace with your own config object
const loggingSettings = getLoggingSettingsOrDefaults(options);
configureLogging(context, loggingSettings);

2. Log messages

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

// 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');

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

2.0.0

  • Changed logging-utils configuration API to synchronize with similar changes done to aws-core-utils/stages configuration and aws-stream-consumer/stream-processing configuration.
    • Changed configureLogging function API to replace multiple arguments with single settings argument
    • Added getLoggingSettingsOrDefaults function to facilitate overriding default settings
    • Changed configureDefaultLogging function to use new getLoggingSettingsOrDefaults function
    • Added typedefs for LoggingSettings and LoggingOptions to better define parameter and return types
    • Removed obsolete configureLoggingFromConfig function
    • Removed obsolete finaliseLogLevel, finaliseUseLevelPrefixes and finaliseUseConsoleTrace functions
    • Removed defaultLogLevel, defaultUseLevelPrefixes and defaultUseConsoleTrace properties & config.json settings
    • Fixed unit tests to synchronize with API changes
  • Renamed logging-utils module to logging module.

1.0.6

  • Updated core-functions dependency to version 2.0.3

1.0.5

  • Updated core-functions dependency to version 2.0.2

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

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.2

  • Added an explicit configureDefaultLogging function for clarity.
  • Minor JSDoc updates
  • Updated core-functions dependency to 1.2.0

1.0.1

  • Simply set core-functions dependency to 1.1.1

FAQs

Package last updated on 20 Nov 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc