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

Commit

Permalink
Do not log empty user agent
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal Vlasák committed Aug 2, 2019
1 parent c26d3c2 commit 76f02e9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- stream write function type
- pretty streams created only when needed
- logger name in pretty loggers
- empty user-agent logged no more


### Added
Expand Down
6 changes: 4 additions & 2 deletions src/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ const expressOnHeaders = (req: AckeeRequest, res: AckeeResponse) => () => {

const expressOnFinished = (logger: AckeeLogger, req: AckeeRequest) => (_err: Error | null, res: AckeeResponse) => {
const error = res[errorSymbol];
const reqOut = `${res.statusCode} ${req.method} ${req.originalUrl} ${res.time} ms ${req.headers['user-agent']}`;
const userAgent = req.headers['user-agent'];
const reqOut = `${res.statusCode} ${req.method} ${req.originalUrl} ${res.time} ms ${userAgent ? userAgent : ''}`;
if (logger.options.ignoredHttpMethods && logger.options.ignoredHttpMethods.includes(req.method)) {
// left here for BC
return;
Expand Down Expand Up @@ -57,7 +58,8 @@ const expressMiddleware: RequestHandler = function(
response: AckeeResponse,
next: any
) {
const reqIn = `--- ${req.method} ${req.originalUrl} ${req.headers['user-agent']}`;
const userAgent = req.headers['user-agent'];
const reqIn = `--- ${req.method} ${req.originalUrl} ${userAgent ? userAgent : ''}`;
this.debug({ req, ackId: req.ackId }, `${reqIn} - Request accepted`);
req._startAt = process.hrtime();
onHeaders(response, expressOnHeaders(req, response));
Expand Down
30 changes: 30 additions & 0 deletions src/tests/express.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,33 @@ test('route can be ignored using regexp helper', () => {
expect(loggerWrites).not.toBeCalled();
});
});

test('user-agent is logged', () =>
new Promise((resolve, reject) => {
const logger = loggerFactory({
streams: [testWriteStream(resolve, json => expect(json.message).toMatch('dummy agent'))],
});

const app = express();
const request = supertest(app);
app.use(logger.express);
request
.get('/test')
.set('User-Agent', 'dummy agent')
.then(() => null);
}));

test('missing user-agent is not logged', () =>
new Promise((resolve, reject) => {
const logger = loggerFactory({
streams: [testWriteStream(resolve, json => expect(json.message).not.toMatch('undefined'))],
});

const app = express();
const request = supertest(app);
app.use(logger.express);
request
.get('/test')
.unset('User-Agent')
.then(() => null);
}));

0 comments on commit 76f02e9

Please sign in to comment.