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 toLOG_LEVEL<LOG_LEVEL: levels less thanLOG_LEVEL>LOG_LEVEL: levels greater thanLOG_LEVEL>=LOG_LEVEL: levels greater than or equal toLOG_LEVEL+LOG_LEVEL: injectLOG_LEVEL-LOG_LEVEL: removeLOG_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
stringvalue naming your writer type - setConfig: a
function(logEmitter, config)which attacheslog:<chanel>andlogForce:<channel>event handlers onto givenlogEmitter.
Take a look at the simple console writer for more details.