Lori
Lori is a very lightweight logger for use in Node.js applications, with a middleware option for Express access logs. Lori is a colourful way to output quick logging, and is extremely easy to set up. It's not designed to take on the functionality of other libraries such as morgan, but to get something in place quickly which can provide insight into your application.
Compatibility
This module is built on each commit with TravisCI on the latest stable version of Node.js, although the main module is compatible all the way back to 0.8.
Usage
Lori is available on npm, so simply install it.
$ npm install lori
There are a couple of ways you can use Lori, either as a generic logger or as a piece of Middleware for Express.
Middleware
As middleware, Lori will generate access logs for your Express application in the following format:
[Thu, 13 Aug 2015 19:14:09] [INFO] GET /welcome 544.98ms
To set up Lori as a piece of Middleware, simply drop it into your app as early as possible in your setup:
var lori = require('lori');
app.use(lori.express());
Logging
There are several default methods written into Lori, for the common use cases with logging:
lori.debug('test');
lori.error('test');
lori.info('test');
lori.warn('test');
These methods typically take String
arguments, however if a non-String
argument is provided, Lori will JSON.stringify
whatever is passed.
You can also call the access
function the Express Middleware uses, as below:
lori.access(req);
lori.access('GET', '/welcome', process.hrtime(startTime));
Each level has a colour associated with it (which can be changed, see below). By default all logs go to stdout
, but this can also be modified.
Configuration
Lori is extremely easy to use, with a few little utilities to allow for customisation.
Constructor
By default, the lori
module provides a singleton logger for use throughout your application (it doesn't care about state), however if you wish to make many loggers, you can access the constructor as below:
var lori = require('lori');
var Logger = new lori.Logger();
This allows you to have different loggers with different configurations throughout your application.
Configure
The configure
function is typically called as soon as you start your application - it allows you to specify colours, logging locations, formats, etc. This function takes an object argument of the following options:
lori.configure({
dateFormat: 'ddd, DD MMM YYYY HH:mm:ss',
localTime: false,
logColors: true,
log: function(str, msg, lvl){
console.log(str);
},
theme: {
debug: 'yellow',
error: 'red',
info: 'green',
warn: 'yellow',
verb: 'white',
path: 'cyan',
message: 'white',
duration: 'white'
}
});
Modification
This is not the only time you can control Lori though, it's dynamic - you can modify it at any time, although naturally it will only effect the instance you're working with (instead of all of them):
lori.reset();
lori.setLogger(function(str, msg, lvl){
if(app.get('env') !== 'production'){
console.log(str);
} else {
fs.appendFileSync('./' + lvl + '-logs.txt', msg);
}
});
lori.setDateFormat('DD-MM-YYYY');
lori.setTheme({
info: 'blue'
});
lori.updateTheme({
info: 'blue'
});
lori.useColors(false);
lori.useLocalTime(true);
Tests
Tests are written in Mocha, and can be run via npm scripts.
$ npm test
You can also generate coverage reports in HTML and lcov formats using:
$ npm run coverage
$ npm run lcov
Sadly, couldn't use Grunt because it swallows the colours.
Issues
If you find any issues inside this module, feel free to open an issue here.