From a2f8e29bca5ca08b8703ae05253e2b4be7c4d472 Mon Sep 17 00:00:00 2001 From: Bishal Pantha Date: Thu, 14 Nov 2019 14:13:26 +0100 Subject: [PATCH] Morgan options can be passed via applyMiddlewareAccessLog api --- docs/more.md | 30 ++++++++++++++++++++++++++++++ example/express_example.js | 16 ++++++++++++---- index.d.ts | 2 +- src/ExpressMiddlewares.js | 10 +++++----- src/ServiceLogger.js | 4 ++-- 5 files changed, 50 insertions(+), 12 deletions(-) diff --git a/docs/more.md b/docs/more.md index 2a74d48..7bb8514 100644 --- a/docs/more.md +++ b/docs/more.md @@ -84,6 +84,36 @@ var app = express(); // log an elk formatted access log to cout logger.applyMiddlewareAccessLog(app); +// pass morgan options +// for example [skip option](https://github.com/expressjs/morgan#skip): only log error responses +logger.applyMiddlewareAccessLog(app, undefined, { + skip: function(req, res) { + return res.statusCode < 400; + } +}); + + //log an elk formatted access log to a file logger.applyMiddlewareAccessLogFile(app, "./access_log.json"); +``` + +## [Create Custom log fields](https://github.com/expressjs/morgan#creating-new-tokens) +```javascript +logger.applyMiddlewareAccessLog(app, { + new_fancy_field: (req, res) => { + return 'i-am-your-new-field-in-access-log'; + } +}); + +``` + +## [skip logging](https://github.com/expressjs/morgan#skip) based on matching condition + +```javascript +// only log error responses +logger.applyMiddlewareAccessLog(app, undefined, { + skip: function(req, res) { + return res.statusCode < 400; + } +}); ``` \ No newline at end of file diff --git a/example/express_example.js b/example/express_example.js index 7bb5c47..7a272c8 100644 --- a/example/express_example.js +++ b/example/express_example.js @@ -20,11 +20,19 @@ var logger = new log4bro(options); var app = express(); //#log an elk formatted access log to cout -logger.applyMiddlewareAccessLog(app, { +logger.applyMiddlewareAccessLog( + app, + { rjm: (req, res) => { - return "hi-test"; + return "hi-test"; } -}); + }, + { + skip: (req, res) => { + return req.url === "/dont-log-this-url"; + } + } +); //#log an elk formatted access log to a file //log4bro.applyMiddlewareAccessLogFile(app, "./access_log.json"); @@ -55,7 +63,7 @@ app.listen(port, function(){ MLOG.changeLogLevel("DEBUG"); - request({ "url": "http://localhost:" + port + "/" }, function(err, response, body){ + request({ "url": "http://localhost:" + port + "/dont-log-this-url" }, function(err, response, body){ process.exit(0); }); }); diff --git a/index.d.ts b/index.d.ts index 11614de..1674a21 100644 --- a/index.d.ts +++ b/index.d.ts @@ -57,7 +57,7 @@ declare class ServiceLogger { createChild(defaultAdditionalFields?: any): ServiceLogger; changeLogLevel(level: string): void; createLoggingDir(): void; - applyMiddlewareAccessLog(expressApp: Application, opts?: any): Application; + applyMiddlewareAccessLog(expressApp: Application, customTokens?: any, accessLogOptions?: any): Application; applyMiddlewareAccessLogFile(expressApp: Application, logFilePath: string): Application; setGlobal(): void; diff --git a/src/ExpressMiddlewares.js b/src/ExpressMiddlewares.js index 937118f..50ad9d8 100644 --- a/src/ExpressMiddlewares.js +++ b/src/ExpressMiddlewares.js @@ -9,7 +9,7 @@ const AUTH_INFO_USER_ID = "auth-info-user-id"; class ExpressMiddlewares { - static accessLogMiddleware(serviceName, dockerMode, opts) { + static accessLogMiddleware(serviceName, dockerMode, customTokens = {}, accessLogOptions = {}) { serviceName = serviceName || "unknown"; const hostName = os.hostname(); @@ -18,10 +18,10 @@ class ExpressMiddlewares { const optKeys = []; // Check for additional access logs - if (opts && typeof opts === "object") { - for (const key in opts) { + if (customTokens && typeof customTokens === "object") { + for (const key in customTokens) { try { - morgan.token(key, typeof opts[key] === "function" ? opts[key] : errorHandler); + morgan.token(key, typeof customTokens[key] === "function" ? customTokens[key] : errorHandler); } catch(err) { morgan.token(key, errorHandler); @@ -138,7 +138,7 @@ class ExpressMiddlewares { " \"current_color\": \":service_color\", \"remote_client_id\": \":remote_client_id\"," + " \"user_agent\": \":user_agent\", " + `${optKeys.length ? optKeys.join(", ") + "," : ""}` + " \"bytes_received\": \":bytes_received\" }", - {}); + accessLogOptions); } accessLogMiddlewareFile(filePath, dockerMode) { diff --git a/src/ServiceLogger.js b/src/ServiceLogger.js index 9dca24f..39b591b 100755 --- a/src/ServiceLogger.js +++ b/src/ServiceLogger.js @@ -212,13 +212,13 @@ class ServiceLogger { } } - applyMiddlewareAccessLog(expressApp, opts) { + applyMiddlewareAccessLog(expressApp, customTokens, accessLogOptions) { if(!expressApp || typeof expressApp !== "function"){ throw new Error("[log4bro] ExpressApp is null or not an object, make sure you pass an instance of express() to applyMiddleware."); } - expressApp.use(Middlewares.accessLogMiddleware(this.serviceName, this.dockerMode, opts)); + expressApp.use(Middlewares.accessLogMiddleware(this.serviceName, this.dockerMode, customTokens, accessLogOptions)); return expressApp; }