Skip to content

Commit

Permalink
Morgan options can be passed via applyMiddlewareAccessLog api
Browse files Browse the repository at this point in the history
  • Loading branch information
panalbish committed Nov 14, 2019
1 parent f7e17ac commit a2f8e29
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 12 deletions.
30 changes: 30 additions & 0 deletions docs/more.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
});
```
16 changes: 12 additions & 4 deletions example/express_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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);
});
});
Expand Down
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
10 changes: 5 additions & 5 deletions src/ExpressMiddlewares.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/ServiceLogger.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit a2f8e29

Please sign in to comment.