Standard logger middleware for loopback

This commit is contained in:
David Cheung 2016-06-09 12:10:44 -04:00
parent ed45358be8
commit 80e7fa6fef
2 changed files with 56 additions and 0 deletions

View File

@ -39,6 +39,7 @@
"bcryptjs": "^2.1.0",
"bluebird": "^3.1.1",
"body-parser": "^1.12.0",
"bunyan": "^1.8.1",
"canonical-json": "0.0.4",
"continuation-local-storage": "^3.1.3",
"debug": "^2.1.2",

View File

@ -0,0 +1,55 @@
// Copyright IBM Corp. 2014,2015. All Rights Reserved.
// Node module: loopback
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
var bunyan = require('bunyan');
var loopback = require('../../lib/loopback');
var util = require('util');
module.exports = logger;
function logger(options) {
//console.log('typeof this mdidleware', this.get('restApiRoot'));
console.log('log-middleware options: ', options);
var defaultOptions = {
name: 'Loopback',
// decides what to log
log: {
request: true,
},
streams: [],
};
var options = util._extend(defaultOptions, options);
var logger = bunyan.createLogger({name: 'app'});
return function(req, res, next) {
var app = req.app;
// use singleton instance of logger?
if (!app.get('logger')) {
app.set('logger', logger);
}
if (options.log.request) {
bindMethod(req, res, logRequest);
}
next();
};
function bindMethod(req, res, method) {
res.on('finish', function() { method(req, res); });
}
function logRequest(req, res) {
var data = {
action: options.log.request.action || 'request',
verb: req.method,
path: req.url,
statusCode: res.statusCode,
};
logger.info(data);
}
};