Skip to content

Commit

Permalink
feat(middleware): add support for Express
Browse files Browse the repository at this point in the history
  • Loading branch information
sonicoder86 committed Nov 22, 2017
1 parent b8b7b95 commit feb7083
Show file tree
Hide file tree
Showing 10 changed files with 399 additions and 21 deletions.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.spec.js
node_modules
.idea
examples
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Simple JSON logger middleware that combines the namespaces of [debug] and the

It has the same logging levels as [bunyan].

## Example
## Example logging

```javascript
process.env.DEBUG = 'redis';
Expand All @@ -29,7 +29,23 @@ will output
{"name":"redis","action":"query","level":50,"time":"2016-08-15T08:50:23.569Z","error_name":"Error","error_stack":"Error: Unauthorized\n at Object.<anonymous> (/home/blacksonic/workspace/bunyan-debug/example.js:15:32)\n at Module._compile (module.js:541:32)\n at Object.Module._extensions..js (module.js:550:10)\n at Module.load (module.js:458:32)\n at tryModuleLoad (module.js:417:12)\n at Function.Module._load (module.js:409:3)\n at Module.runMain (module.js:575:10)\n at run (bootstrap_node.js:352:7)\n at startup (bootstrap_node.js:144:9)\n at bootstrap_node.js:467:3","error_message":"Unauthorized","problem":"missmatch"}
```

Examples can be found in ```example.js```.
Examples can be found in ```examples/index.js```.

### Logging request identifier automatically

The library provides middlewares for both Koa and Express applications.
These middlewares add the request identifiers coming from the header X-Request-Id to every log
(in the log: `request_id`).

```javascript
const Koa = require('koa');
const logFactory = require('@emartech/json-logger');

app.use(logFactory.getMiddleware());
```

The method `getMiddleware` creates a Koa middleware (alias for `getKoaMiddleware`).
The `getExpressMiddleware` method does the same, but returns an Express middleware.

## Development

Expand Down
22 changes: 22 additions & 0 deletions examples/express.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

const express = require('express');
const logFactory = require('../index');
const logger = logFactory('example');
const port = 3000;

const app = express();

app.use(logFactory.getExpressMiddleware());

app.get('/', (req, res) => {
logger.info('before');

logFactory.setOnContext('customer_id', Math.round(Math.random() * 1000));

logger.info('after');
res.send('It works')
});

app.listen(port);
console.log('listening on port: ' + port);
2 changes: 1 addition & 1 deletion example.js → examples/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

process.env.DEBUG = 'redis';
const logger = require('./index');
const logger = require('../index');

const mongoLogger = logger('mongo');
const redisLogger = logger('redis');
Expand Down
8 changes: 1 addition & 7 deletions koa-example.js → examples/koa.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
'use strict';

const Koa = require('koa');
const uuid = require('uuid');
const logFactory = require('./index');
const logFactory = require('../index');
const logger = logFactory('example');
const port = 3000;

const app = new Koa();

app.use(async (ctx, next) => {
ctx.request.header['x-request-id'] = uuid.v4();
await next();
});

app.use(logFactory.getMiddleware());

app.use(async (ctx) => {
Expand Down
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ logFactory.Logger = Logger;
logFactory.getNamespaces = function() {
return process.env.DEBUG || '';
};
logFactory.getMiddleware = contextMiddlewareFactory.getMiddleware.bind(contextMiddlewareFactory);
logFactory.getKoaMiddleware = contextMiddlewareFactory.getKoaMiddleware.bind(contextMiddlewareFactory);
logFactory.getExpressMiddleware = contextMiddlewareFactory.getExpressMiddleware.bind(contextMiddlewareFactory);
logFactory.getMiddleware = logFactory.getKoaMiddleware;
logFactory.setOnContext = contextMiddlewareFactory.setOnContext.bind(contextMiddlewareFactory);

module.exports = logFactory;
Loading

0 comments on commit feb7083

Please sign in to comment.