nodify-logger

Simple logger inspired by the VMS Message facility.

Usage no npm install needed!

<script type="module">
  import nodifyLogger from 'https://cdn.skypack.dev/nodify-logger';
</script>

README

nodify-logger

Simple Logging facility inspired by VMS MESSAGE.

Installation

The easiest way to install this package is to use npm:

    npm install nodify-logger

If you want to check out the source, use the git command:

    git clone git://github.com/nodify/nodify-logger.git

Usage

Initialization

After importing the nodify-logger package, create a logging factory by using the createInstance() method. The first parameter to this call is an options object containing the following items:

  • [ facility ] - a facility name (required)
  • [ emitter ] - a function to use to write the log message (optional, defaults to util.debug)
  • [ messages ] - a map of severities / messages (optional)
  • [ messagesPath ] - the name of a file containing the map of severities / messages (optional)

The second parameter is a callback with three parameters:

  • [ log_function ] - a function used to log a message
  • [ messages ] - an object whose keys are message abbreviations derived from the messages object from the options
  • [ log_instance ] - the logger instance, in case you want to call it directly

Here is a simple example for a facility called PROXY:

    var logger = require( 'nodify-logger' );
    var logger_options = {
      facility: 'PROXY',
      messages: {
        S: {
          init: "Initialization Successful",
          new: "New Route Established"
        },
        I: {
          fast: "Can't Keep Up with Requests"
        },
        W: {
          authreq: "Authentication Required for Route %s"
        },
        E: {
          unknown: "Unknown Route",
          authfail: "Authentication Failure"
        }
      }
    };
    
    logger.createInstance( logger_options, function ( l, PROXY, log ) {
      l( PROXY.S_INIT );
    } );

Logging a message

Use the log_function parameter passed into the createInstance() callback to log messages. Parameters can be added to messages that require them:

var logger = require( 'nodify-logger' );
var logger_options = {
  facility: 'TEST',
  messages: {
    I: {
      simple: "I am a simple message",
      formats: "I am a message that takes a parameter: %s",
      formatd: "I am a message that takes a numeric parameter: %d"
    }
  }
};

logger.createInstance( logger_options, function ( l, TEST, log ) {
  l( TEST.I_SIMPLE );
  l( TEST.I_FORMATS, "w00t!" );
  l( TEST.I_FORMATD, 1337 );
} );</pre>