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