lugg

3.0.0 • Public • Published

lugg

Build Status

A simple logging module that uses pino and draws inspiration from TJ Hollowaychuk's debug.

Logging is a universal concern in most programs, and lugg aims to make the common usage pattern as simple as possible.

Manifesto: Server logs should be structured. JSON's a good format. Let's do that. A log record is one line of JSON.stringify'd output.

Based on pino

At first glance, logging appears to be an isolated concern, but on closer inspection you can see that it intersects with analytics, error handling, debugging and disaster recovery. The pino module provides a great solution to address all of these concerns.

lugg simplifies the common use case, and aims to be really simple to use.

Inspired by debug

lugg also provides the ability to control debug output using a DEBUG environment variable.

Example Usage

// call init once in your program
require('lugg').init();
 
// then in foo.js
var log = require('lugg')('foo');
log.info('doing stuff');
log.warn({foo: 'bar'}, 'something %s', 'interesting');
log.error(new Error('blah'), 'something %s', 'bad');
log.debug('this will not be output'); // set DEBUG=app:foo to see debug output from this logger

Arguments

Each argument you pass is logged as-is, up to the first string argument, which is formatted using util.format() to provide string interpolation of any subsequent arguments.

Controlling Log Output

Read the source (it's tiny) and refer the pino docs for more info.

You can control the output of lugg using the level option:

require('lugg').init({level: 'warn'}); // show only warnings and higher

The logging level you provide in the call to .init() would typically come from your local configuration (eg. warn in production, info in development).

You can also manipulate the logging level for specific loggers at runtime, without having to modify your configuration, using an environment variable (see Controlling Debug Output below).

The call to lugg.init() takes an option hash, which is passed to pino.child() to create a "root logger". All loggers returned from lugg are children of this root logger, so they inherit whatever settings you provide to init().

See the docs for pino for more info about the supported options. lugg will provide a name of "app" if no name is provided.

Controlling Debug Output

The log level can be manipulated using the DEBUG environment variable, using the same approach as the debug module:

$ DEBUG=* node app.js # print all debug output 
$ DEBUG=app:* node app.js # print debug output from your app 
$ DEBUG=foo,express:* node app.js # print debug output from foo and express 
$ DEBUG=*,-foo node app.js # print all debug output except foo 

As loggers are created, if they have a name that matches this environment variable then they will have their level set to debug. You can also manipulate this programmatically using lugg.debug():

lugg.debug('app:foo'); // debug messages from app:foo
lugg.debug('app:foo:'); // debug messages from app:foo

Be aware this doesn't change any loggers that have already been created.

Output

pino writes logs to stdout in JSON format, so pipe the output through the pino-pretty CLI to get logs in a more human readable format:

node app.js | pino-pretty

Package Sidebar

Install

npm i lugg

Weekly Downloads

1,058

Version

3.0.0

License

MIT

Unpacked Size

10.9 kB

Total Files

8

Last publish

Collaborators

  • aexmachina