morgan
semver
>=1.0.0 <2.0.0postconditions5functions4last verified2026-06-12coverage score100%Postconditions — what we check
- morgan · stream-write-errorerrorWhenThe output stream (default process.stdout or custom stream) throws or emits an error during log line writeThrows
Error from the underlying stream write operationRequired handlingCaller MUST attach an error handler to custom streams before passing them to morgan. Unhandled stream errors will propagate and crash the process. Use stream.on('error', handler) before passing stream to morgan options.costmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisible - morgan · next-called-with-errorerrorWhenAn error occurs within morgan middleware execution (e.g., token function throws)Throws
Error passed to next(err) in Express middleware chainRequired handlingCaller MUST have an Express error-handling middleware registered (4-argument middleware). Morgan calls next(err) on internal errors; without error middleware the default Express handler will respond with 500 and the error stack trace.costmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisible - compile · compile-invalid-token-syntax-silentwarningWhenFormat string contains valid syntax but references undefined token names (e.g., ':nonexistent-token')Throws
No error thrown — undefined tokens silently resolve to '-' in log outputRequired handlingCaller SHOULD verify that all token names used in format strings are registered via morgan.token() before the format is compiled. Unregistered tokens resolve to '-' silently, producing misleading log output without any runtime error. Validate token names during integration testing.costlowin proddegraded serviceusers seedegraded performancevisibilitysilent - token · token-name-overwrite-silentwarningWhenmorgan.token() is called with a name that matches a built-in token (e.g., 'status', 'method', 'url', 'response-time') or a previously registered custom tokenThrows
No error thrown — existing token definition is silently replacedRequired handlingCaller MUST avoid reusing built-in token names unless intentionally overriding built-in behavior. Built-in tokens: 'date', 'http-version', 'method', 'referrer', 'remote-addr', 'remote-user', 'req', 'res', 'response-time', 'status', 'total-time', 'url', 'user-agent'. Overwriting 'status' or 'response-time' silently changes log output for ALL morgan middleware instances in the same process, including third-party middleware. Use namespaced token names (e.g., 'x-request-id', 'app-user-id') to avoid collisions.costlowin proddegraded serviceusers seedegraded performancevisibilitysilent - format · format-name-overwrite-silentwarningWhenmorgan.format() is called with a name that matches a built-in format ('combined', 'common', 'default', 'dev', 'short', 'tiny') or a previously registered custom formatThrows
No error thrown — existing format definition is silently replacedRequired handlingCaller MUST avoid reusing built-in format names unless intentionally overriding built-in behavior. Built-in formats registered by morgan itself: 'combined', 'common', 'default', 'dev', 'short', 'tiny'. Overwriting 'combined' (Apache combined log shape, used by the most common morgan('combined') invocation) silently changes log line structure for ALL morgan middleware instances in the same process, including third-party middleware. Downstream log parsers (Logstash, Datadog, Fluentd) break when field positions shift. Use namespaced format names (e.g., 'app-access', 'audit-log') to avoid collisions with the built-in set.costlowin proddegraded serviceusers seedegraded performancevisibilitysilent
Sources
Every postcondition cites at least one of these. Numbered to match the footnotes above.
- [1]expressjs.com/en/resourceshttps://expressjs.com/en/resources/middleware/morgan.html
- [2]nodejs.org/api/stream.htmlhttps://nodejs.org/api/stream.html#stream_writable_write_chunk_encoding_callback
- [3]expressjs.com/en/guidehttps://expressjs.com/en/guide/error-handling.html
- [4]raw.githubusercontent.com/expressjs/morganhttps://raw.githubusercontent.com/expressjs/morgan/master/index.js
- [5]github.com/expressjs/morganhttps://github.com/expressjs/morgan#readme
Need a different package?
Request a profile