Skip to content

Commit 6db94fd

Browse files
committed
feat(cls): remove cls with middlewares from logger, move it to @emartech/cls-adapter
BREAKING CHANGE: middlewares and automatic cls storage removed
1 parent c2fb05c commit 6db94fd

File tree

10 files changed

+52
-169
lines changed

10 files changed

+52
-169
lines changed

README.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,26 @@ Examples can be found in ```examples/index.js```.
3333

3434
### Logging request identifier automatically
3535

36-
The library provides middlewares for both Koa and Express applications.
37-
These middlewares add the request identifiers coming from the header X-Request-Id to every log
38-
(in the log: `request_id`).
36+
The continuation local storage handling has moved to the `@emartech/cls-adapter` package.
37+
You need to use the middlewares of `@emartech/cls-adapter` and add its transformer to the loggers configure method.
38+
This way it will log the request identifier coming from the header field (`X-Request-Id`).
39+
40+
For automatting
3941

4042
```javascript
4143
const Koa = require('koa');
4244
const logFactory = require('@emartech/json-logger');
45+
const clsAdapter = require('@emartech/cls-adapter');
4346

44-
app.use(logFactory.getMiddleware());
45-
```
47+
logFactory.configure({
48+
transformers: [
49+
clsAdapter.addContextStorageToInput()
50+
]
51+
});
4652

47-
The method `getMiddleware` creates a Koa middleware (alias for `getKoaMiddleware`).
48-
The `getExpressMiddleware` method does the same, but returns an Express middleware.
53+
const app = new Koa();
54+
app.use(clsAdapter.getKoaMiddleware());
55+
```
4956

5057
## Development
5158

examples/express.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,23 @@
22

33
const express = require('express');
44
const logFactory = require('../index');
5+
const clsAdapter = require('@emartech/cls-adapter');
56
const logger = logFactory('example');
67
const port = 3000;
78

9+
logFactory.configure({
10+
transformers: [
11+
clsAdapter.addContextStorageToInput()
12+
]
13+
});
814
const app = express();
915

10-
app.use(logFactory.getExpressMiddleware());
16+
app.use(clsAdapter.getExpressMiddleware());
1117

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

15-
logFactory.setOnContext('customer_id', Math.round(Math.random() * 1000));
21+
clsAdapter.setOnContext('customer_id', Math.round(Math.random() * 1000));
1622

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

examples/koa.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,23 @@
22

33
const Koa = require('koa');
44
const logFactory = require('../index');
5+
const clsAdapter = require('@emartech/cls-adapter');
56
const logger = logFactory('example');
67
const port = 3000;
78

9+
logFactory.configure({
10+
transformers: [
11+
clsAdapter.addContextStorageToInput()
12+
]
13+
});
814
const app = new Koa();
915

10-
app.use(logFactory.getMiddleware());
16+
app.use(clsAdapter.getKoaMiddleware());
1117

1218
app.use(async (ctx) => {
1319
logger.info('before');
1420

15-
logFactory.setOnContext('customer_id', Math.round(Math.random() * 1000));
21+
clsAdapter.setOnContext('customer_id', Math.round(Math.random() * 1000));
1622

1723
logger.info('after');
1824
ctx.body = 'It works';

index.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
const Logger = require('./src/logger/logger');
44
const isNamespaceEnabled = require('./src/enabled/enabled');
5-
const contextMiddlewareFactory = require('./src/context-middleware-factory/context-middleware-factory');
65
const formatter = require('./src/formatter');
76

87
/**
@@ -20,10 +19,6 @@ logFactory.Logger = Logger;
2019
logFactory.getNamespaces = function() {
2120
return process.env.DEBUG || '';
2221
};
23-
logFactory.getKoaMiddleware = contextMiddlewareFactory.getKoaMiddleware.bind(contextMiddlewareFactory);
24-
logFactory.getExpressMiddleware = contextMiddlewareFactory.getExpressMiddleware.bind(contextMiddlewareFactory);
25-
logFactory.getMiddleware = logFactory.getKoaMiddleware;
26-
logFactory.setOnContext = contextMiddlewareFactory.setOnContext.bind(contextMiddlewareFactory);
2722
logFactory.configure = function(options) {
2823
Logger.configure(options);
2924
};

package-lock.json

Lines changed: 20 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
}
1919
},
2020
"release": {
21-
"analyzeCommits": "@bubltechnology/customizable-commit-analyzer",
2221
"verifyConditions": {
2322
"path": "./node_modules/semantic-release/src/lib/plugin-noop.js"
2423
}
@@ -35,12 +34,10 @@
3534
"json"
3635
],
3736
"dependencies": {
38-
"chalk": "2.3.0",
39-
"cls-hooked": "4.2.2",
40-
"uuid": "3.1.0"
37+
"chalk": "2.3.0"
4138
},
4239
"devDependencies": {
43-
"@bubltechnology/customizable-commit-analyzer": "1.0.2-0",
40+
"@emartech/cls-adapter": "1.1.0",
4441
"chai": "4.1.2",
4542
"express": "4.16.2",
4643
"koa": "2.3.0",

src/context-middleware-factory/context-middleware-factory.js

Lines changed: 0 additions & 60 deletions
This file was deleted.

src/context-middleware-factory/context-middleware-factory.spec.js

Lines changed: 0 additions & 54 deletions
This file was deleted.

src/logger/logger.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,13 @@
11
'use strict';
22

33
const config = require('../config');
4-
const continuationLocalStorage = require('cls-hooked');
54
const STACK_TRACE_LIMIT = 3000;
65
const DATA_LIMIT = 3000;
76
const Timer = require('../timer/timer');
87
const jsonFormatter = require('../formatter/json');
98
const consoleOutput = require('../output/console');
109
const allowedKeys = ['output', 'formatter', 'transformers'];
1110

12-
const getContextStorage = function() {
13-
const contextNamespace = continuationLocalStorage.getNamespace('session');
14-
if (contextNamespace && contextNamespace.active) {
15-
const { id, _ns_name, ...contextData } = contextNamespace.active;
16-
return contextData;
17-
}
18-
19-
return {};
20-
};
21-
2211
class Logger {
2312
constructor(namespace, enabled) {
2413
this._namespace = namespace;
@@ -101,7 +90,6 @@ const logMethodFactory = function(level) {
10190
level: config.levels[level].number,
10291
time: new Date().toISOString()
10392
},
104-
getContextStorage(),
10593
data
10694
);
10795

src/logger/logger.spec.js

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const Logger = require('./logger');
4-
const continuationLocalStorage = require('cls-hooked');
54
const jsonFormatter = require('../formatter/json');
65
const consoleOutput = require('../output/console');
76

@@ -31,18 +30,6 @@ describe('Logger', function() {
3130
expect(logArguments.details).to.eql('forever');
3231
});
3332

34-
it('should log prequest id if there is namespace present', function() {
35-
const namespace = continuationLocalStorage
36-
.createNamespace('session');
37-
38-
namespace.run(function(){
39-
namespace.set('request_id', 'uid');
40-
logger.info('wedidit');
41-
const logArguments = JSON.parse(console.log.args[0]);
42-
expect(logArguments.request_id).to.eql('uid');
43-
});
44-
});
45-
4633
it('should not call log info method when disabled', function() {
4734
logger = new Logger('mongo', false);
4835

0 commit comments

Comments
 (0)