Home

Awesome

bole

A tiny JSON logger, optimised for speed and simplicity

Travis Status

NPM

Log JSON from within Node.js applications. The log format is obviously inspired by the excellent Bunyan and is likely to be output-compatible in most cases. The difference is that bole aims for even more simplicity, supporting only the common-case basics.

bole is designed for global singleton use. Your application has many log sources, but they all aggregate to the same sources. You configure output in one place for an application, regardless of how many modules and dependencies are also using bole for logging.

Example

mymodule.js

const log = require('bole')('mymodule')

module.exports.derp = () => {
  log.debug('W00t!')
  log.info('Starting mymodule#derp()')
}

main.js

const bole = require('bole')
const mod  = require('./mymodule')

bole.output({
  level: 'info',
  stream: process.stdout
})

mod.derp()
$ node main
{"time":"2014-05-18T23:47:06.545Z","hostname":"tweedy","pid":27374,"level":"info","name":"mymodule","message":"Starting mymodule#derp()"}

Features

API

bole(name)

Create a new logger with the supplied name to be attached to each output. If you keep a logger-per module you don't need to pass loggers around, keep your concerns separated.

logger#debug(), logger#info(), logger#warn(), logger#error()

Loggers have 4 roughly identical log methods, one for each of the supports log-levels. Log levels are recorded on the output and can be used to determine the level of detail passed to the output.

Log methods support the following types of input:

If you require more sophisticated serialisation of your objects, then write a utility function to convert those objects to loggable objects.

logger()

The logger object returned by bole(name) is also a function that accepts a name argument. It returns a new logger whose name is the parent logger with the new name appended after a ':' character. This is useful for splitting a logger up for grouping events. Consider the HTTP server case where you may want to group all events from a particular request together:

const log = bole('server')

http.createServer((req, res) => {
  req.log = log(uuid.v4()) // make a new sub-logger
  req.log.info(req)

  //...

  // log an error against this sub-logger
  req.log.error(err)
})

In this case, your events would be listed as something like "name":"server:93f57a1a-ae59-46da-a625-8d084a77028a" and each event for a particular request would have the same "name" property, distinct from the rest.

Sub-loggers can even be split in to sub-sub loggers, the rabbit hole is ~bottomless.

bole.output()

Add outputs for application-wide logging, accepts either an object for defining a single output or an array of objects defining multiple outputs. Each output requires only a 'level' and a 'stream', where the level defines the minimum debug level to print to this stream and the stream is any WritableStream that accepts a .write() method.

If you pass in a stream with objectMode set to true then you will receive the raw log objects rather than their stringified versions.

bole.output([
  { level: 'debug', stream: fs.createWriteStream('app.log') },
  { level: 'info', stream: process.stdout }
])

bole.reset()

Clears all output streams from the application

bole.setFastTime()

If speed is something you care about and you can handle time in milliseconds since epoch (Date.now()) rather than the full ISO string (new Date().toISOString()) in your logs then use bole.setFastTime(true) to shave off some precious microseconds.

Note that this will reset to the default of false when you use bole.reset()

Additional features

If you need to serialise specific types of objects then write a utility function to convert to a loggable object.

If you need a special kind of output then write a stream to accept output data.

If you need to filter a present output data in a special way, write a package to do it and publish it in npm.

License

bole is Copyright (c) 2014 Rod Vagg @rvagg and licensed under the MIT License. All rights not explicitly granted in the MIT License are reserved. See the included LICENSE.md file for more details.