Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit f8578a0

Browse files
committed
Merge branch 'feature/ignore-http-methods' into 'master'
Add option to ignore certain HTTP methods See merge request Backend/ackee-node-logger!4
2 parents dca046f + 4c286f0 commit f8578a0

6 files changed

Lines changed: 92 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## [0.2.4] - 2018-09-06
2+
3+
### Added
4+
- `options.ignoredHttpMethods` to ignore certain HTTP requests in express middleware
5+
16
## [0.2.3] - 2018-09-06
27

38
### Fixed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ Options override both default logger configuration and environment-specific conf
9797
- `defaultLevel` - set logger's minimal loglevel (default is `debug`)
9898
- `disableFields` - list of paths which will be omitted from the objects being logged (if any)
9999
- `enableFields` - list of paths which will not be omitted by default serializers from objects being logged
100+
- `ignoredHttpMethods` - list of HTTP methods which will not be logged by express logging middleware at all. Defaults to `['OPTIONS']`
100101
- `streams` - list of stream objects, which will be passed directly to [pino-multistream's multistream function](https://github.com/pinojs/pino-multi-stream#pinomsmultistreamstreams) instead of default `ackee-node-logger` stream
101102
- `pretty` - if set to `true`, logger will use [pino pretty human-readable logs](https://github.com/pinojs/pino/blob/master/docs/API.md#pretty). This option can be overriden by `streams`
102103
- `config` - object, which will be passed to underlying logger object. Right now, underlying logger is [pino](https://github.com/pinojs/pino), so for available options see [pino API docs](https://github.com/pinojs/pino/blob/master/docs/API.md#pinooptions-stream)

express.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ const expressMiddleware = function(req, response, next) {
1414
});
1515
onFinished(response, (err, res) => {
1616
const error = res[Symbol.for('error')];
17+
if (this.options.ignoredHttpMethods.includes(req.method)) {
18+
return;
19+
}
1720
if (error) {
1821
this.error({ error, req, res, ackId: req.ackId }, `${reqInfo} - Error handler at the end of app`);
1922
} else if (res.out) {

index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ const defaultLogger = (options = {}) => {
6969
];
7070
}
7171

72+
if (!options.ignoredHttpMethods) {
73+
options.ignoredHttpMethods = ['OPTIONS'];
74+
}
75+
7276
const logger = pino(
7377
_.merge(
7478
{
@@ -83,6 +87,7 @@ const defaultLogger = (options = {}) => {
8387
multistream(streams)
8488
);
8589
logger.warning = logger.warn;
90+
logger.options = options;
8691

8792
// Add maxLevel support to pino-multi-stream
8893
// This could be replaced with custom pass-through stream being passed to multistream, which would filter the messages

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ackee-node-logger",
3-
"version": "0.2.3",
3+
"version": "0.2.4",
44
"description": "Ackee Node Logger",
55
"main": "index.js",
66
"scripts": {

tests/index.test.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ test('can create named logger', () => {
2121
});
2222

2323
test('can use custom stream', () => {
24+
const loggerWrites = jest.fn();
2425
const logger = loggerFactory({
2526
streams: [
2627
{
2728
stream: new stream.Writable({
2829
write: (chunk, encoding, next) => {
2930
const json = JSON.parse(chunk);
3031
expect(json.message).toBe('Hello');
32+
loggerWrites();
3133
next();
3234
},
3335
}),
@@ -36,9 +38,11 @@ test('can use custom stream', () => {
3638
});
3739

3840
logger.info('Hello');
41+
expect(loggerWrites).toBeCalled();
3942
});
4043

4144
test('can use warning level', () => {
45+
const loggerWrites = jest.fn();
4246
const logger = loggerFactory({
4347
streams: [
4448
{
@@ -47,6 +51,7 @@ test('can use warning level', () => {
4751
const json = JSON.parse(chunk);
4852
expect(json.message).toBe('Hello');
4953
expect(json.level).toBe(levels.warn);
54+
loggerWrites();
5055
next();
5156
},
5257
}),
@@ -55,6 +60,7 @@ test('can use warning level', () => {
5560
});
5661

5762
logger.warning('Hello');
63+
expect(loggerWrites).toBeCalled();
5864
});
5965

6066
test('express binds', () => {
@@ -64,3 +70,74 @@ test('express binds', () => {
6470
app.use(logger.express);
6571
return request.get('/');
6672
});
73+
74+
test('GET requests are logged by default', () => {
75+
const loggerWrites = jest.fn();
76+
const logger = loggerFactory({
77+
streams: [
78+
{
79+
stream: new stream.Writable({
80+
write: (chunk, encoding, next) => {
81+
const json = JSON.parse(chunk);
82+
expect(json.req.method).toBe('GET');
83+
loggerWrites();
84+
next();
85+
},
86+
}),
87+
},
88+
],
89+
});
90+
const app = express();
91+
const request = supertest(app);
92+
app.use(logger.express);
93+
return request.get('/').then(() => {
94+
expect(loggerWrites).toBeCalled();
95+
});
96+
});
97+
98+
test('OPTIONS requests are ignored by default', () => {
99+
const loggerWrites = jest.fn();
100+
const logger = loggerFactory({
101+
streams: [
102+
{
103+
stream: new stream.Writable({
104+
write: (chunk, encoding, next) => {
105+
loggerWrites();
106+
next();
107+
},
108+
}),
109+
},
110+
],
111+
});
112+
const app = express();
113+
const request = supertest(app);
114+
app.use(logger.express);
115+
return request.options('/').then(() => {
116+
expect(loggerWrites).not.toBeCalled();
117+
});
118+
});
119+
120+
['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'HEAD'].forEach(method => {
121+
test(`${method} HTTP method can be ignored by options`, () => {
122+
const loggerWrites = jest.fn();
123+
const logger = loggerFactory({
124+
streams: [
125+
{
126+
stream: new stream.Writable({
127+
write: (chunk, encoding, next) => {
128+
loggerWrites();
129+
next();
130+
},
131+
}),
132+
},
133+
],
134+
ignoredHttpMethods: [method],
135+
});
136+
const app = express();
137+
const request = supertest(app);
138+
app.use(logger.express);
139+
return request[method.toLowerCase()]('/').then(() => {
140+
expect(loggerWrites).not.toBeCalled();
141+
});
142+
});
143+
});

0 commit comments

Comments
 (0)