morgan
semver
>=1.0.0 <2.0.0postconditions6functions4last verified2026-06-24coverage 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 - format · format-non-string-non-function-delayed-throwwarningWhenmorgan.format() is called with fmt argument that is neither a string nor a function (e.g., number, plain object, undefined, null)Throws
No error thrown at format() call site. Later, when morgan(name) is invoked for that format, getFormatFunction passes the stored value to compile(), which throws TypeError 'argument format must be a string' for non-string values. For object/array values, compile() may silently produce a format function that returns the JSON-encoded representation, causing garbled log lines instead of an exception.Required handlingCaller MUST ensure the second argument to morgan.format() is a non-empty format string (using :token-name syntax) or a FormatFn ((tokens, req, res) => string|undefined|null). The TypeScript declarations in @types/morgan enforce this at compile time, but plain JavaScript callers, dynamic-typed wrappers, and config-loaded format values can defeat the type check. Validate format values at the integration boundary where they enter the morgan registration call. Do not register a format from untrusted config without verifying its type.costlowin proddelayed failureusers seeservice unavailablevisibilitysilent
Sources
Every postcondition cites at least one of these. Grouped by source type; numbered to match the footnotes above.
Official documentation
- [1]expressjs.com/en/resources/middlewareMorgan
- [2]nodejs.org/api/stream.htmlStream
- [3]expressjs.com/en/guide/error-handling.htmlError Handling
Source code
- [4]raw.githubusercontent.com/expressjs/morgan/masterexpressjs/morgan · index.js
- [5]github.com/expressjs/morganexpressjs/morgan
Research notes
Curator notes from SOURCES.md captured when the profile was written so you can verify the reasoning, not just the rules.
Sources: morgan
Official Documentation
Research Date: 2026-02-26
Need a different package?
Request a profile