loggerware
v1.1.3
Published
An express middleware for application access and error logs
Downloads
3
Maintainers
Readme
#loggerware
An express middleware for application access and error logs
loggerware listens to every request, logging to STDOUT
and STDERR
for access and error logging respectively. It does
not manipulate any request or response.
##Quick Start
Add the package to your project:
$ npm install loggerware
Require loggerware and register your access and error loggers, like the following*:
var express = require('express'),
loggerware = require('loggerware')({ name: 'your-logger' }),
app = express();
app.use(loggerware.register('access', { // Register access loggerware
file: '/var/tmp/your-access.log'
}));
app.use('/', require('./routes/index'));
app.use('/healthcheck', require('./routes/healthcheck'));
app.use('/oauth', require('./routes/oauth'));
app.use(loggerware.register('error', { // Register error loggerware
file: '/var/tmp/your-error.log'
}));
app.use(function (err, req, res, next) { // Register error handler
res.status(err.status || 500);
if (req.xhr) res.json({ error: err.message });
else res.render('internal-error');
});
app.listen(port, function () {
loggerware.info('Server started on http://localhost:%s', port);
});
*Order matters here, for more information on middleware placement goto: http://expressjs.com/en/guide/using-middleware.html
##Guide
####Creating a logger Requiring loggerware will return a method that creates a bunyan logger and exposes extra functionality for creating and registering loggers.
So, executing the following:
var loggerware = require('loggerware')({ name: 'your-logger' });
will give you:
loggerware.create
create additional loggers for your applicationloggerware.register
register access and error loggers for your application
from here onwards, you have enough of an interface for registering and creating loggers for your application.
If there is a need to, you can create more that one logger by using the loggerware.create
method:
var loggerware = require('loggerware')({ 'name': 'your-logger'}),
loggerA = loggerware.create({ name: 'your-logger-a' }),
loggerB = loggerware.create({ name: 'your-logger-b' });;
The loggerware.create
signature is create([, configuration :: Object])
.
- The optional
configuration
parameter gets passed to thebunyan.createLogger
method. See the bunyan.createLogger api for more details.
####Registering access and error loggers
Once you have instantiated a logger, you can register access and error loggers for your application by using the loggerware.register
method.
The following registers two loggers to the default logger:
var loggerware = require('loggerware')({ name: 'your-logger' });
loggerware.register('access', {
name: 'your-access',
file: '/var/tmp/your-access.log'
});
loggerware.register('error');
In this example we:
- created a logger called
your-logger
- registered an access logger called
your-access
that streams toSTDOUT
and to the file/var/tmp/your-access.log
- registered an error logger streaming just to
STDERR
.
The loggerware.register
signature is register(type :: String, [, configuration :: Object])
.
The type
parameter value can either be access
or error
.
The optional configuration
object parameter can consist of format
and file
keys.
- The
format
key specifies the morgan logger format, by defaultcombined
is used. - The
file
key specifies the file to log to. If nofile
key is supplied, your logging will just be streamed toSTDOUT
andSTDERR
.
##License MIT