fixup! Standard logger middleware for loopback

This commit is contained in:
David Cheung 2016-06-09 15:58:03 -04:00
parent 73d78e25d9
commit 0189cdac06
1 changed files with 21 additions and 5 deletions

View File

@ -6,22 +6,33 @@
var bunyan = require('bunyan');
var loopback = require('../../lib/loopback');
var util = require('util');
var path = require('path');
module.exports = loggerMiddleware;
function loggerMiddleware(options) {
//console.log('typeof this mdidleware', this.get('restApiRoot'));
console.log('log-middleware options: ', options);
var logPath = options.path || path.resolve('logs', 'info.log');
var defaultLogLevel = process.env.LOG_LEVEL || 'info';
var defaultOptions = {
name: 'Loopback',
// decides what to log
log: {
request: true,
},
streams: [],
streams: [
{
level: defaultLogLevel,
stream: process.stdout,
},
{
level: defaultLogLevel,
type: 'file',
path: logPath,
},
],
};
var options = util._extend(defaultOptions, options);
var logger = bunyan.createLogger({name: 'app'});
var logger = bunyan.createLogger(options);
return function(req, res, next) {
@ -53,6 +64,11 @@ function loggerMiddleware(options) {
remoteMethod: remoteMethod,
};
//log data
logger.info(data);
if (res.statusCode >= 200 && res.statusCode < 300)
logger.info(data);
else if (res.statusCode < 400)
logger.warn(data);
else
logger.error(data);
}
};