Skip to content

fix: preserve request start time across multiple morgan instances#345

Open
SAY-5 wants to merge 1 commit into
expressjs:masterfrom
SAY-5:fix/multiple-instances-start-time
Open

fix: preserve request start time across multiple morgan instances#345
SAY-5 wants to merge 1 commit into
expressjs:masterfrom
SAY-5:fix/multiple-instances-start-time

Conversation

@SAY-5
Copy link
Copy Markdown

@SAY-5 SAY-5 commented Apr 15, 2026

When multiple morgan middleware instances are chained together, the second
one resets req._startAt and res._startAt before recording new values. This
means all instances end up measuring response time from when the last morgan
middleware ran, not from the actual request arrival.

The fix only records start timestamps once per request/response pair. If
_startAt is already set (by an earlier morgan instance), it is left alone.

Before (two morgan instances with a 100ms delay between them):

0.998
0.998

After:

106.552
106.552

Both loggers now correctly report the full response time from request start.

Includes a test that chains two morgan instances with a 50ms delay and
verifies both report times >= the delay.

Fixes #141

When multiple morgan instances are used as middleware, each one was
resetting req._startAt and res._startAt to undefined before recording
a new start time. This caused all instances to measure response time
from the point the last instance ran, not from the actual request start.

Now the start time is only recorded once per request/response pair.
Subsequent morgan instances reuse the original timestamps, so all
loggers report accurate response times.

Fixes expressjs#141
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Multiple morgan instances stubs out original start time

1 participant