# express-logging-json

> A JSON logging helper for Express

Latest version **0.0.4** (published 2026-10-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install express-logging-json
pnpm add express-logging-json
yarn add express-logging-json
bun add express-logging-json
```

## Health

**Score 65/100 (B)** — status: active.

Positive: has types; no vulnerabilities; recently updated; high maintenance score; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

## Facts

| | |
|---|---|
| Version | 0.0.4 |
| Published | 2026-10-21 |
| First published | 2022-11-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >= 12 |
| Dependencies | 0 |
| Unpacked size | 50.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Open Room Inc. |
| Maintainers | openrm |
| Keywords | express, logger, logging, json |

## Links

- npm: https://www.npmjs.com/package/express-logging-json
- Repository: https://github.com/openrm/express-logging-json
- Homepage: https://github.com/openrm/express-logging-json#readme
- Issues: https://github.com/openrm/express-logging-json/issues
- npm.io page: https://npm.io/package/express-logging-json

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 0.0.4 (latest) — 2026-10-21
- 0.0.3 — 2026-10-16
- 0.0.2 — 2026-10-16
- 0.0.1 — 2022-11-29

## README

# express-logging-json
A JSON logging helper for Express

## Installation
```bash
npm install express-logging-json
```

## Usage
A simple logging middleware example, with [on-finished](https://github.com/jshttp/on-finished) module:
```js
const onFinished = require('on-finished');
const format = require('express-logging-json');

const options = {
    mask: {
        fields: ['headers.authorization']
    }
};

const app = express();

app.use((req, res, next) => {
    onFinished(res, err => {
        const log = format(req, res, options);
        log.err = err;
        console.info(log);
    });
    next();
});
```

## Leaving parts out
A log refers to a lot of what a request carries, and an access log is written for every request. To log less, leave out what is not needed with `omit`: paths, or regular expressions tested against them.

```js
const options = {
    omit: [
        'trailers', // a top-level key
        'headers.accept-language', // a path
        /^headers\.sec-/ // everything a regular expression matches
    ]
};
```

Paths are dotted, as in `mask.fields`. The items of an array share the path of the array, so `items.note` leaves out `note` in every item. Like masking, it works on a copy and never changes the request. It can be combined with `mask`.

## Masking secrets
A request log refers to everything a request carries: headers, cookies, session, query, body. Secrets in there are easy to miss, so masking can be done by path, by name or by what a value looks like. Masking works on a copy: the request, session and response are never changed.

```js
const options = {
    mask: {
        fields: ['body.card.number'], // an exact path
        keys: ['pin', /^x-internal-/i], // names, at any depth
        values: [/\b\d{16}\b/], // matches within any string
        auto: true, // the names and shapes that most secrets have, see below
        except: [/^x-token-/i] // names that only sound secret, see below
    }
};
```

`auto` masks:
- keys named like secrets, as whole words: `password`, `secret`, `token`, `credentials`, `authorization`, `signature`, `jwt`, `csrf`, `api key`, `session id` and the like, in any style (`x-auth-token`, `csrfSecret`, `API_KEY`), and the `Cookie` and `Set-Cookie` headers. Words that merely contain these letters, such as `passport` or `author`, are left alone
- the values of those keys, unless there is nothing to hide: flags, nulls, and `cookie` objects that are only settings
- recognisable secrets in any string, whatever the key is called: JSON web tokens, the credentials of `Bearer` and `Basic` values, well known vendor keys, PEM private keys, and secret-looking query parameters such as `token` or `signature`, as in a `url`

Only the secret part is replaced, so `Bearer [Filtered]` and `?signature=[Filtered]&page=2` stay readable. Long hex or base64 strings are not treated as secrets: they are at least as often ids.

A name can sound secret and hold something harmless, such as an id in a `token-user` header. List those in `except` and `auto` leaves them alone by name. What is inside is still checked, so a real token in an excepted header is masked all the same. `keys` and `fields` take precedence over `except`.

Masking never throws. A part that cannot be copied (a throwing getter or `toJSON`) is replaced by `[Unreadable]` or `[Unserializable]`, and if a log cannot be formatted at all, `format()` returns just the method, the url (masked), the status and a `formatError`, never what it could not mask. A log is written from callbacks where nobody would catch an error.

Masking is a safety net, not a guarantee. A secret under an innocent name, in an unrecognisable shape, is not found.

The same helpers can be used on other parts of a log, such as a text line:

```js
const { redact, redactString, secrets } = require('express-logging-json');

redactString('GET /file?signature=abc123 HTTP/1.1', { auto: true });
// 'GET /file?signature=[Filtered] HTTP/1.1'

redact(anyObject, { auto: true, except: ['x-token-user'] }); // a masked copy
secrets.isSecretKey('x-api-key'); // true
```

---
_Source: https://npm.io/package/express-logging-json · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
