Skip to content

Commit

Permalink
Merge pull request #5656 from flexion/devex-fix-logger-response-conte…
Browse files Browse the repository at this point in the history
…xt-to-staging

Fix format of response object in our logs
  • Loading branch information
jimlerza authored Dec 18, 2024
2 parents df3f8d1 + f4d5498 commit 49803be
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
5 changes: 3 additions & 2 deletions web-api/src/logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,12 @@ describe('logger', () => {
const instance = req.locals.logger;

instance.info = jest.fn();
jest.spyOn(instance, 'addContext');

res.end();

expect(instance.info).toHaveBeenCalledWith(
expect.any(String),
expect(instance.info).toHaveBeenCalledWith(expect.any(String));
expect(instance.addContext).toHaveBeenCalledWith(
expect.objectContaining({
response: expect.objectContaining({
statusCode: 200,
Expand Down
4 changes: 3 additions & 1 deletion web-api/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,15 @@ export const expressLogger = (req, res, next) => {
end.apply(this, arguments);
const responseTimeMs = new Date() - req.locals.startTime;

logger.info(`Request ended: ${req.method} ${req.url}`, {
logger.addContext({
response: {
responseSize: parseInt(res.get('content-length') ?? '0'),
responseTimeMs,
statusCode: res.statusCode,
},
});

logger.info(`Request ended: ${req.method} ${req.url}`);
logger.clearContext();
};

Expand Down

0 comments on commit 49803be

Please sign in to comment.