@catalyststack/subsystem-logger

An opinionated logging subsystem for Node.js applications.

Usage no npm install needed!

<script type="module">
  import catalyststackSubsystemLogger from 'https://cdn.skypack.dev/@catalyststack/subsystem-logger';
</script>

README

subsystem-logger

An opinionated logging subsystem for Node.js applications.

This logging subsystem provides a flexible, configurable event emission based logging system with support for logging contexts. When a logger function is called, an event is raised for the given level and context. Writers then listen for these events and subsequently handle the log persistence.

This subsystem supports configuration per context, writer and level.

Installation

npm install --save '@catalyststack/subsystem-logger'

Usage

To setup this subsystem and the default emergency logger:

const logger = require('@catalyststack/subsystem-logger');

To use the logger and write to configured writers:

const logger = require('@catalyststack/subsystem-logger')('<context>');

logger.debug('your amazing log');

Configuration

The configuration system supports simple log level weight comparison which allows queries such as the following to be used >=notice. An individual level configuration can be either a string or an array. This means that along with the previous mentioned string query, you can also chain together an array of these string queries like so ['>=notice', debug]. An array of queries are executed in the order they are written.

To configure the logger call the setConfig function with the configuration object:

const logger = require('@catalyststack/subsystem-logger');

logger.setConfig({
  contexts:
    <context_name>: ">=debug"
  writers:
    <writer_name>: ">=notice"
});

If a context is not configured, by default all available log level functions will be registered to emit log events.

If a writer is not configured, by default it will not register to handle any log events.

The supported query operations are:

  • <=LOG_LEVEL: levels less than or equal to LOG_LEVEL
  • <LOG_LEVEL: levels less than LOG_LEVEL
  • >LOG_LEVEL: levels greater than LOG_LEVEL
  • >=LOG_LEVEL: levels greater than or equal to LOG_LEVEL
  • +LOG_LEVEL: inject LOG_LEVEL
  • -LOG_LEVEL: remove LOG_LEVEL

Creating a Writer

This package comes with a simple console writer, however implementing your own writer is a fairly trivial task.

All that is required is an object which exposes the following 2 properties:

  • name: a string value naming your writer type
  • setConfig: a function(logEmitter, config) which attaches log:<chanel> and logForce:<channel> event handlers onto given logEmitter.

Take a look at the simple console writer for more details.

License

MIT