sawmill
v0.2.2
Published
Where logs go to be cut up and sent elsewhere.
Downloads
16
Readme
Sawmill
On top of being a delicious variety of gravy, a sawmill is the destination for manly men and giant logs to float to, cutting the logs down to size and sending them elsewhere on arrival. Whether you're a manly man or a giant log, it is your destiny.
Goals
- Predictability - The way this works should be tested and documented to the point that all side effects are predictable to users.
- Simplicity - Both the code and its use should be self-explanatory for the times I fail at #1 (of course I will).
- Streams - Where would we be without log driving? Streams are definitely the best method of moving logs around.
- Fun - Let's face it: we spend a lot of time working on these applications. Shouldn't we have a little fun with it?
Installation
npm install --save sawmill
Usage
In general, usage follows that of console
. The major differences are around
our differing need: where console
is always to stdout
and stderr
, we want
to support any stream, and need to provide resources for that.
Inspiration is also paid to debug for its simplicity.
Creating a Mill
First things first: create a "Mill" to collect logs. I recommend (much like
debug) creating and naming the Mill on require
:
var mill = require('sawmill').createMill('app:Server')
// Shortcut
var mill = require('sawmill')('app:Server')
Logging
Once you have a Mill, it's time to send it logs. There are only two kinds of logs Sawmill understands: Objects and Messages. You may only send one at a time, and the delimiter is thus: anything that is not of type "object" is a Message.
mill.log('Some message') // Send a Message to log level "info".
mill.info({ key: 'value' }) // Send an Object to log level "info".
mill.debug('This goes to log level "debug"')
mill.warn('Same for "warn"')
mill.error('Same for "error"')
mill.trace('This describes stuff') // Send a Message with a stack trace.
Output Format
Both Objects and Messages are sent out as JSON. Messages are added as a "message" key, while Objects are used wholesale. Additional values are always added:
mill
: The original name you gave the Mill. (Feel free to usename
for your own dastardly purposes!)time
: The Date this log was received.level
: The level at which this log was received.
Output Locations
Any Mill can have as many or few destinations as you like, configured with the
add
and remove
methods:
mill.add('stdout', process.stdout) // Name required!
mill.remove('stdout') // Peace and quiet.
Removing is always by name, and adding is by name and a WritableStream. If
a third argument is provided to add
, it is used to determine the minimum
log level for sending, inclusive:
mill.add('stderr', process.stderr, "error")
The current streams are accessible at mill.streams
as an Array of Objects.
Each Object has three keys, name
, level
, and stream
. Should be pretty
self-explanatory. Change at will.
Helpful Streams
These streams are provided to make working with Mills' output easier. They are
available on the root module (e.g. require('sawmill').human
).
human
- A batteries-includedconsole
logger. By default, "info" through "warn" are sent toprocess.stdout
, while "error" is sent toprocess.stderr
. If DEBUG is set, it is used as a filter and "debug" is sent as well. If NODE_ENV is set to "development", "debug" is sent, whereas setting NODE_ENV to "production" silences all log messages.
Alternatives
There are plenty of alternatives when it comes to logging, but the two best alternatives are:
- Winston - Lots of available destinations. I've spent more time fighting with Winston than I have using it (although I've used it for major projects), but that's probably because of an integration problem with either me or some other library I chose. YMMV.
- Bunyan - A commonly-offered alternative, but too complicated for my taste. Comes with Dtrace support and an drool-worthy CLI tool for reading the JSON output.