Create logger middleware to encapsulate its configuration from app

This commit is contained in:
Daniel García Aubert 2018-04-03 12:26:35 +02:00
parent b6989ac82a
commit 3316c2ded3
2 changed files with 25 additions and 3 deletions

View File

@ -0,0 +1,22 @@
module.exports = function logger (options) {
if (!global.log4js || !options.log_format) {
return function dummyLoggerMiddleware (req, res, next) {
next();
};
}
const opts = {
level: 'info',
// Allowing for unbuffered logging is mainly
// used to avoid hanging during unit testing.
// TODO: provide an explicit teardown function instead,
// releasing any event handler or timer set by
// this component.
buffer: !options.unbuffered_logging,
// optional log format
format: options.log_format
};
const logger = global.log4js.getLogger();
return global.log4js.connectLogger(logger, opts);
};

View File

@ -13,6 +13,7 @@ const lzmaMiddleware = require('./middleware/lzma');
const errorMiddleware = require('./middleware/error-middleware');
const syntaxError = require('./middleware/syntax-error');
const servedByHostHeader = require('./middleware/served-by-host-header');
const logger = require('./middleware/logger');
const ControllersFactory = require('./controllers/factory');
@ -28,6 +29,8 @@ module.exports = function createServer (serverOptions) {
const app = bootstrap(serverOptions);
app.use(logger(serverOptions));
app.use(bodyParser.json());
app.use(servedByHostHeader());
@ -39,9 +42,6 @@ module.exports = function createServer (serverOptions) {
app.use(lzmaMiddleware());
setupLogger(app, serverOptions);
app.use(cors());
app.use(user());