Registry / observability / bunyan-middleware

bunyan-middleware

JSON →
library1.0.2jsnpmunverified

bunyan-middleware is a Node.js middleware for logging HTTP requests and responses using the Bunyan logger. Currently at version 1.0.2, it appears to be a stable project, though its release cadence is not explicitly stated. This library offers robust features for integrating structured logging into Express, Connect, or pure HTTP servers. Key differentiators include automatic management of the `X-Request-Id` header (generating a UUID if not present), logging request-response duration, providing a request-scoped logger (`req.log`) for tracing individual requests throughout an application, and custom serializers for `req` and `res` objects. It also provides functionality to obscure sensitive headers in log outputs, enhancing security and compliance. It is a direct dependency of an existing Bunyan logger instance, allowing for consistent logging configuration.

npm install bunyan-middleware
INSTALL
IMPORT
SIG · BUNYAN-MIDDLEWARE
B
bunyan-middleware
observabilityjavascriptv1.0.2
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

bunyanMiddleware
const bunyanMiddleware = require('bunyan-middleware')
import bunyanMiddleware from 'bunyan-middleware'
This package is distributed as a CommonJS module. For TypeScript, use `import bunyanMiddleware = require('bunyan-middleware')`. For ESM projects, direct `import` syntax might require bundler configuration or a CommonJS wrapper.
BunyanMiddlewareOptions
import bunyanMiddleware = require('bunyan-middleware'); type Options = bunyanMiddleware.BunyanMiddlewareOptions;
TypeScript types are nested under the main `bunyanMiddleware` export.

This example demonstrates setting up bunyan-middleware with Express, configuring request ID handling, obscuring headers, adding custom finish data, and utilizing the request-scoped `req.log`.

const bunyan = require('bunyan') const bunyanMiddleware = require('bunyan-middleware') const express = require('express') const app = express() const logger = bunyan.createLogger({ name: 'My App', level: process.env.LOG_LEVEL || 'info' }) app.use(bunyanMiddleware( { headerName: 'X-Request-Id' , propertyName: 'reqId' , logName: 'req_id' , obscureHeaders: ['authorization', 'cookie'] // Example: obscure sensitive headers , logger: logger , additionalRequestFinishData: function(req, res) { // Add custom data to the 'request finish' log message return { exampleCustomData: 'value' } } } ) ) app.get('/', function (req, res) { // Use `req.log` for request-specific logging, automatically including the request ID. req.log.info({ user: 'guest' }, 'Accessed home route') res.send('Hello, Bunyan Logger!') }) const port = process.env.PORT || 3000; app.listen(port, () => { logger.info(`Server listening on port ${port}`); });
Debug
Known issues
gotchaThis package is distributed as a CommonJS module. In modern TypeScript or pure ESM projects, a direct `import bunyanMiddleware from 'bunyan-middleware'` will likely fail or require specific bundler configurations. The recommended TypeScript import is `import bunyanMiddleware = require('bunyan-middleware')`.
fix
For TypeScript, use `import bunyanMiddleware = require('bunyan-middleware')`. For ESM-only JavaScript, consider using a CommonJS wrapper or a bundler that correctly handles CommonJS modules.
affects: >=1.0.0
gotchaThe `logger` option is a required configuration. Failing to provide a valid Bunyan logger instance will result in runtime errors as the middleware attempts to use it.
fix
Always pass an initialized Bunyan logger instance to the `bunyanMiddleware` options, e.g., `logger: bunyan.createLogger({ name: 'my-app' })`.
affects: >=1.0.0
gotchaWhen using `bunyan-middleware` with Express mounted apps that rewrite `req.url`, the default Bunyan `req` serializer might log the rewritten URL instead of the original. `bunyan-middleware` provides its own serializer using `req.originalUrl` to mitigate this, but custom serializer overrides could reintroduce this behavior.
fix
Ensure that if you provide custom `req` serializers, they correctly reference `req.originalUrl` or the desired URL property for accurate logging in mounted Express applications.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: bunyanMiddleware is not a function
Attempting to use `import bunyanMiddleware from 'bunyan-middleware'` in an ESM context without proper CommonJS interoperability configuration, leading to the module object being imported instead of the default export function.
fix
In TypeScript or CommonJS, use `const bunyanMiddleware = require('bunyan-middleware')`. If forced to use ESM `import` in a modern Node.js environment, ensure your `tsconfig.json` or bundler settings handle CommonJS imports correctly, or wrap `require` in a utility function.
Sensitive headers (e.g., Authorization, Cookie) are appearing in logs despite `obscureHeaders` being configured.
The `obscureHeaders` option is case-sensitive or not configured correctly, or the headers are being logged by other means before `bunyan-middleware` processes them.
fix
Verify that the header names in the `obscureHeaders` array exactly match the casing of the incoming HTTP headers. Double-check that `bunyan-middleware` is applied early in your middleware stack to obscure headers before other logging mechanisms capture them.
Upgrade
Version history
1.0.2latest on npm
Audit
Dependencies
bunyanrequiredThis middleware requires an instantiated Bunyan logger to function, passed via the `logger` option.
Agent activity
21 hits · last 30 days
node
18
OpenAI (training)
1
Resources
bunyan-middleware — npm install bunyan-middleware · libregistry