node package manager


Customisable logger where most things are optional.

Log2 - Customisable logger where most things are optional.


Unfortunately loggers are very personal and people like what they like. I like this one. You might too.

var log2 = require('log2');
# options
var log = log2();
var logWithFilename = log2({ filename : __filename });
var logWithProcessInfo = log2({ title : true, pid : true, hostname : true });
var logToStream = log2({ stream : fs.createWriteStream('filename.log') });
# log() is the same as
log('This is a log message, which defaults to info');
# levels
log.debug('Used for Debugging');'Regularly info messages');
log.warn('Let the user know that something needs looking at');
log.error('Interestingly, somewhere went wrong');
log.fatal('Something went badly wrong');
# simple line, just
2013-03-29T03:15:32.458Z -  INFO: This is a log message, which defaults to info
# this line has an ID
2013-03-29T03:15:32.458Z - (259)  INFO: This is a log message, which defaults to info
# this line shows the program title, pid and hostname
2013-03-29T03:45:23.811Z -  INFO: {test-log2/16982@cheetah} Same level as the line above
# this shows the filename and method
2013-03-29T03:45:23.812Z -  INFO: [/home/chilts/src/chilts-log2/examples/t2.js::start] Same level as the line above
# this shows an id, program title, pid, hostname, filename and method
2013-03-29T03:45:23.812Z - (804)  INFO: {test-log2/20795@cheetah} [/home/chilts/src/chilts-log2/examples/t2.js::start] Same level as the line above
timestamp - (id) LEVEL: {title/pid@hostname} [filename::method] Message

If anything in the group is not wanted, then the rest of the group still shows. e.g. {title@hostname} even if the pid isn't required. However, if none of title, pid and hostname is required, the braces also get left out.

Default: true

Whether to show or not show the timestamp of the log.

Default: process.stdout

The stream to output messages to. Must have a .write() method.

Pass in the current filename so you can log it too. e.g.

var log = log2({ filename : __filename });

Pass in the current method so you can log it too. e.g.

var log = log2({ method : 'makeServer' });

Pass in an 'id' so we can see which log lines relate to others. e.g. usually used with things like connect-flake or connect-uuid to attach an ID to a request.

app.use(function(req, res, next) {
    req.log = log2({ id : req.flake });
    req.log('This is an info message with a unique ID for this request');

Default: false

Shows the process.title.

Default: false

Shows the

Default: false

Shows the hostname the program is running on.


Written by Andrew Chilton - Blog - Twitter.