skip to content

As the lead setting an access-log standard for a team's Express services, would you standardise on morgan's combined format or a shared format function emitting structured entries, and which fields must every entry carry?

level: principalimportance: should knowfreq 15%

answer

  1. who parses the access log
  2. combined lacks latency and ids
  3. object mode needs 1.12
  4. the query string is a leak

basics

~20 s

Keep combined only where an existing parser expects it. Otherwise ship one shared morgan 1.12+ format function writing one structured entry per request to the app logger: request id, method, path, status, both timers, size; never credentials or query strings.

solid answer

~40 s

It depends on who reads the logs. `combined` costs nothing to adopt and every web-log parser understands it, but it has no latency, no request id, and its `:url` writes the full `req.originalUrl`, query string and any tokens in it included. A shared format function, written to the application logger through a stream in object mode (morgan 1.12.0 or later), gives fields the backend can index, the same request id the rest of the logs carry, and a deliberate field list. My default for new services is the shared function, packaged once: `requestId`, `method`, the path without the query, numeric `status`, `responseTimeMs` and `totalTimeMs`, response size and user agent, with health probes skipped only when they succeed. I would keep `combined` where a downstream system already depends on it, and append latency there.

go deeper

for a junior

Know that combined is a standard text layout and that a format function can build your own entry instead.

for a middle

Explain which fields combined lacks, how a shared format function and stream adapter fill the gap, and the 1.12 object-mode requirement.

for a senior

Spot leaks in a format, such as query-string tokens or Basic-auth user names, and keep exactly one finish-time entry per request.

for a principal

Weigh consumer compatibility against structured fields and ownership cost, set the field list, and plan migration and exceptions.

## The decision An **access log** records one entry per HTTP request: who asked for what, what came back and how long it took. In an Express estate that uses morgan, a lead has to choose what every service emits, because dashboards, alerts and incident searches are written against that shape. The realistic options are morgan's predefined `combined` format, or a **shared format function** that every service imports and that produces structured entries for the application logger. There is no universally right answer; it turns on who consumes the log and what they need from it. ## What combined offers and lacks | Aspect | `combined` | Shared format function | |---|---|---| | Adoption cost | one word per service | a small internal package to own | | Downstream parsing | understood by existing web-log tooling | fields arrive already structured | | Latency | absent unless appended | `:response-time` and `:total-time` as numbers | | Request id | absent | carried in every entry | | Query strings | logged in full through `:url` | chosen deliberately | | Version floor | any current 1.x release | 1.12.0 for object-mode entries | `combined` is the right call when a system already ingests it: a security tool, a log pipeline with fixed patterns, an analytics job. Changing the shape there breaks a consumer the team may not own. ## What the structured option costs - **An owned artefact.** The format function, its tests and its upgrades live in one package that every service depends on. That is a real maintenance line item, but a small one. - **A morgan version floor.** Passing objects to the stream requires morgan 1.12.0 or later, with `writableObjectMode: true` on the stream. Services pinned below that must upgrade or emit a string. - **Type discipline.** Token functions return escaped strings, so the shared function converts status and timers to numbers and reads raw values from `req` where the logger does its own encoding. ## Fields every entry should carry - **`requestId`**: the same id the rest of the request's logs carry, generated or accepted at the edge. - **`method`** and the **path without the query string**. - **`status`** as a number, absent when the client aborted before a response. - **`responseTimeMs`** and **`totalTimeMs`**: time to headers and time to finish, so slow bodies are visible. - **Response size**, from the `Content-Length` response header when present. - **Client address**, which morgan reads from `req.ip`, so it is only meaningful if the Express proxy settings are correct for the deployment. - **User agent**, useful for separating clients and spotting automated traffic. ## Fields that must never appear 1. **The raw query string.** `:url` prints `req.originalUrl`, so `combined` writes `/reset?token=...` verbatim. Log the path, and allow-list any query parameters you need. 2. **Credentials.** No `:req[authorization]`, no cookies. `:remote-user`, part of `short`, `common` and `combined`, writes the Basic-auth user name. 3. **Bodies.** morgan never logs them by default; keep it that way in the shared function. ## One entry per request, and where it goes - **One logger instance, without `immediate`**, so the entry carries the final status and both timers. A second, immediate instance doubles volume and needs a join; adopt it only for services where in-flight visibility matters. - **Into the application logger** through the `stream` option, not `process.stdout` beside it, so access entries share the logger's timestamps, levels and destinations. - **Health probes skipped when they succeed**, through a shared `skip` predicate on `req.originalUrl`, so a failing probe still leaves a trace. - **No duplicate request logging** in the application logger or framework hooks; pick one owner of the access entry. ## Keeping the standard enforced A standard that lives only in a wiki drifts within a release or two. Three cheap controls keep it real: - A **contract test** in the shared package that formats a sample request and compares the entry's keys with the agreed list. - A **lint rule or review checklist** item that flags direct `morgan('dev')` or ad-hoc format strings in service code. - A **dashboard query** per service that counts entries missing `requestId` or `status`, so gaps show up as numbers rather than as surprises during an incident. ## How I would roll it out 1. Publish the shared format function, stream adapter and `skip` predicate as one internal package, with a test that asserts the field list. 2. Migrate new services first; leave `combined` where a consumer depends on it, appending ` :response-time ms` if that consumer tolerates an extra field. 3. Review the field list when a new consumer appears or a field turns out to carry personal data.

  • What would make you keep combined for a service despite its missing fields?
    A consumer that already parses it and that the team cannot change cheaply: a security tool, a fixed-pattern pipeline, a report built on the layout. Breaking that contract costs more than the missing latency and id. I would append ` :response-time ms` only if the parser tolerates trailing fields, and add the request id the same way.
  • Why skip only successful health probes rather than all of them?
    A failing probe is often the first sign an instance is unhealthy, and its line is the one you search for during an incident. Skipping probes whose status is below 400 removes the noise while keeping failures. The predicate should match the path on `req.originalUrl`, since `req.url` may be relative to a mounted router.

  • combined is always the right production standard because every tool reads it.
  • morgan's :url token prints only the path, never the query string.
  • Two morgan instances, one immediate, are the safe default for every service.
  • Passing objects from a format function to the stream works on any morgan 1.x release.
  • Leaving format choice to each service is harmless because lines look alike.

Assumes morgan 1.12.0 or later for structured entries: HISTORY.md lists object pass-through for object-mode streams in 1.12.0; on earlier 1.x the shared format must return a string line.