From 3316c2ded32b145c37dfc81c0dd6e5c4ab39db6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Tue, 3 Apr 2018 12:26:35 +0200 Subject: [PATCH] Create logger middleware to encapsulate its configuration from app --- lib/cartodb/middleware/logger.js | 22 ++++++++++++++++++++++ lib/cartodb/server.js | 6 +++--- 2 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 lib/cartodb/middleware/logger.js diff --git a/lib/cartodb/middleware/logger.js b/lib/cartodb/middleware/logger.js new file mode 100644 index 00000000..ccd51d45 --- /dev/null +++ b/lib/cartodb/middleware/logger.js @@ -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); +}; diff --git a/lib/cartodb/server.js b/lib/cartodb/server.js index 3e860d12..bcb53dc1 100644 --- a/lib/cartodb/server.js +++ b/lib/cartodb/server.js @@ -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());