Skip to content

Latest commit

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Log Ride

Simple stream-based logging with an API similar to Node's console. Log Ride exposes a stream property that can be piped into Transform streams for filtering or output formatting. Log Ride is nothing fancy, just merely a no-hassle approach for logging with streams.

Installation

npm install log-ride

Usage

Use default (singleton) logger piped to JSON.stringify()

constlog=require('log-ride').defaultLogger();constjson=newrequire('stream').Transform({objectMode: true});json._transform=(chunk,encoding,callback)=>{json.push(JSON.stringify(chunk)+'\r\n');callback();}log.stream.pipe(json).pipe(process.stdout);log.info('Logging %s message to stdout','information');log.error('Logging %s message to stdout','error');log.error('Logging %s to stdout',newError('Fabulous'));// Produces:// {"info":{"message":"Logging information message to stdout"}}// {"error":{"message":"Logging error message to stdout"}}// {"error":{"message":"Logging Error: Fabulous to stdout"}}

Define a custom log method

// Shorthand way to obtain the defaultLogger()constlog=require('log-ride')();log.verbose=(lineNumber,longMessage)=>{log.custom('verbose',{lineNumber: lineNumber,longMessage: longMessage});}log.verbose(123,'A very verbose message');// Produces no output since you still need to pipe it!

API

Module interface

Assuming:

constlogRide=require('log-ride');
  • logRide.defaultLogger([options]) — Factory method to obtain a default (singleton) Logger. Options to configure the singleton are only recognized on the first call. Note that logRide([options]) is a shorthand way to obtain the default Logger.

  • logRide.createLogger([options]) — Factory method to create a new Logger instance with options.

  • logRide.Logger — Exposes the Logger class that can be instantiated via new Logger([options]).

Examples:

constlogRide=require('log-ride');constlog=logRide.defaultLogger();// Obtain the default Loggerconstlog=logRide();// Obtain the default Loggerconstlog=logRide.createLogger();// New Logger instance// New Logger instanceconstLogger=require('log-ride').Logger;constlog=newLogger();

Class: Logger

A Logger object provides the method interface for queuing log entries to an underlying Readable stream that can be piped for output.

new Logger([options])

Constructor. Creates a new Logger with options. The options object is forwarded to the underlying HookedReadable stream.

options (Object)

  • beforePush (Function) — A callback to invoke every time a log entry is pushed into the stream. Courtesy of HookedReadable.

    Example:

    // Add a 'timestamp' property
    beforePush: function(data){data.timestamp=newDate();returndata;}
  • highWaterMark (Number) — The maximum number of log entries to store in the internal stream buffer. Default is 16.

logger.stream

Returns a Readable stream that can be piped. Streamed log entries are JavaScript objects, and must undergo a transformation for output to the console.

log.stream.pipe(json).pipe(process.stdout);

logger.custom(key, object)

Queues a custom log object {key: object} which can be transformed for output downstream. This method is a thin wrapper around the stream's push() method. To implement a custom log method, decorate a Logger instance with a function that calls logger.custom().

Example:

log.verbose=(lineNumber,longMessage)=>{log.custom('verbose',{lineNumber: lineNumber,longMessage: longMessage});}// Now use your methodlog.verbose(76,'A very verbose message');

logger.error([data | error][, ...])

Queues an 'error' log entry. Similar to Node's console.error(), multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values (the arguments are all passed to util.format()).

As a convenience, you can also pass an Error object as the first (and only) argument; in which case the error's message becomes the primary message.

logger.info([data][, ...])

Queues an 'info' log entry. Similar to Node's console.info(), multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values (the arguments are all passed to util.format()).

logger.log([data][, ...])

This is an alias for logger.info().

logger.warn([data | error][, ...])

This is an alias for logger.error().

License

MIT

About

Simple stream-based logging with a console-like interface.

Resources

Stars

0 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages