No description, website, or topics provided.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib
README.md
package.json

README.md

Express Request Logger

Logs express pending, slow, error and completed requests to redis. The redis server is fixed at the Metrological redis test server for easy inclusion into Metrological projects. Notice that logging is only performed when NODE_ENV is either production or test. In case of NODE_ENV dev or backendDev, logs are written to redis at localhost:6379.

Usage:

  1. Add the logger to the express application, preferably before any other middleware:
require('express-request-logger').configure(
	app,
	{projectName: "provisioningBackend"}
);

// Express config..
app.get('/', handleGet);
app.all('*',function(req,res){
	res.status(404).end();
});

This will log all pending, slow and completed requests, but will not detect errors. Error requests will be logged as completed requests.

  1. To log error requests separately, set the .error property explicitly in the express response:
function errorResponse(request, response, message, debug) {
    if (response.rLog) {
        response.rLog.info.error = {message: message, debug: debug};
    }

    // Express stuff.
    config.getLogger().error('error', message, JSON.stringify(request.query), debug);
    response.header('Cache-Control', 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0');
    response.status(500).end(message);
}
  1. To attach extra meta-information to the log record:
if (res.rLog) {
	// Write additional meta info to log.
	if (req.user && req.user.id) {
		res.rLog.info.user = {id: req.user.id, name: req.user.username};
	}
	res.rLog.update();
}
  1. To ignore some request:
app.use(function(req, res, next) {
	if (res.rLog) {
		if (req.path == '/') {
			// Ignore nagios calls.
			res.rLog.ignore();
		}
	}
});

Notice that if the request has already been logged at the moment of calling .ignore, the key is not deleted from redis. Preferably call ignore as soon as possible, for example in middleware appended directly after the request logger itself.