{"id":17605,"library":"express-bunyan-logger","title":"Express Bunyan Logger Middleware","description":"express-bunyan-logger is an Express middleware that integrates the Bunyan structured logger into web applications. It automatically logs incoming requests and outgoing responses, enriching log entries with detailed metadata such as HTTP method, URL, response status, and response time. It supports flexible configuration for log formats, custom levels based on response status or time, and dynamic inclusion/exclusion of fields. The current stable version is 1.3.3, last published 8 years ago. However, the project's original maintainer has openly stated a lack of time for ongoing maintenance, indicating an effectively abandoned status. While functional, new feature development and proactive security updates are unlikely. Its primary differentiator is deep integration with Bunyan's serialization capabilities, providing rich, machine-readable logs suitable for analysis and integration with log management tools.","status":"abandoned","version":"1.3.3","language":"javascript","source_language":"en","source_url":"https://github.com/villadora/express-bunyan-logger","tags":["javascript","express","logger","bunyan"],"install":[{"cmd":"npm install express-bunyan-logger","lang":"bash","label":"npm"},{"cmd":"yarn add express-bunyan-logger","lang":"bash","label":"yarn"},{"cmd":"pnpm add express-bunyan-logger","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Core logging library that express-bunyan-logger wraps. Essential for all logging functionality.","package":"bunyan","optional":false},{"reason":"The web framework this middleware is designed to integrate with. Not a direct npm dependency but a fundamental runtime requirement.","package":"express","optional":false}],"imports":[{"note":"This library is CommonJS-first. Use `require()` for Node.js environments. Direct ESM import is not officially supported and may require a wrapper.","wrong":"import expressBunyanLogger from 'express-bunyan-logger';","symbol":"expressBunyanLogger","correct":"const expressBunyanLogger = require('express-bunyan-logger');"},{"note":"The main logger is instantiated by calling the imported function, typically without arguments or with an options object.","wrong":"app.use(expressBunyanLogger);","symbol":"mainLoggerMiddleware","correct":"app.use(require('express-bunyan-logger')());"},{"note":"The error logger is a separate middleware exposed as a property, also instantiated by calling it.","wrong":"app.use(expressBunyanLogger.errorLogger);","symbol":"errorLoggerMiddleware","correct":"app.use(require('express-bunyan-logger').errorLogger());"},{"note":"A child logger is automatically attached to each `req` object as `req.log` after the middleware runs. This logger inherits context and can be used for request-specific logging.","symbol":"requestChildLogger","correct":"req.log.debug('this is debug in middleware');"}],"quickstart":{"code":"const express = require('express');\nconst expressBunyanLogger = require('express-bunyan-logger');\nconst bunyan = require('bunyan');\n\nconst app = express();\nconst PORT = process.env.PORT || 3000;\n\n// Create a Bunyan logger instance if you want custom streams or serializers\nconst customBunyanLogger = bunyan.createLogger({\n  name: 'my-express-app',\n  streams: [\n    { level: 'info', stream: process.stdout },\n    { level: 'error', path: './app-errors.log' }\n  ],\n  serializers: bunyan.stdSerializers\n});\n\n// Use the request logger middleware with custom options\napp.use(expressBunyanLogger({\n  name: 'request-logger',\n  logger: customBunyanLogger, // Use the custom bunyan logger\n  format: \":remote-address - :user-agent[major] custom logger - :method :url\",\n  levelFn: function(status) {\n    if (status >= 500) return 'error';\n    if (status >= 400) return 'warn';\n    return 'info';\n  },\n  obfuscate: ['req.body.password', 'req.headers.authorization'] // Obfuscate sensitive fields\n}));\n\n// Middleware to add custom data to the request log\napp.use(function(req, res, next) {\n  // Access the child logger attached to req\n  req.log.info({ customField: 'someValue', userId: 'user123' }, 'Request processed by custom middleware');\n  next();\n});\n\napp.get('/', (req, res) => {\n  req.log.info('Handling GET / request');\n  res.send('Hello World!');\n});\n\napp.post('/data', express.json(), (req, res) => {\n  req.log.info({ body: req.body }, 'Received data post');\n  if (req.body.password) {\n    req.log.warn('Password received in request body, should be obfuscated.');\n  }\n  res.status(200).json({ message: 'Data received', received: req.body });\n});\n\n// Use the error logger middleware after all routes and other middleware\napp.use(expressBunyanLogger.errorLogger({\n  name: 'error-logger',\n  logger: customBunyanLogger,\n  level: 'error'\n}));\n\n// Basic error handling middleware\napp.use((err, req, res, next) => {\n  customBunyanLogger.error(err, 'Unhandled error caught by final error handler');\n  res.status(500).send('Something broke!');\n});\n\napp.listen(PORT, () => {\n  customBunyanLogger.info(`Server listening on port ${PORT}`);\n});","lang":"javascript","description":"This quickstart demonstrates setting up `express-bunyan-logger` for both request and error logging within an Express application. It shows how to pass a custom Bunyan logger instance, configure a custom format string, implement a `levelFn` for dynamic log levels, and utilize the `obfuscate` option for sensitive data. It also highlights how to access the request-specific child logger (`req.log`) within subsequent middleware."},"warnings":[{"fix":"Consider migrating to a more actively maintained logging middleware for Express (e.g., `pino-http`, `morgan` with `winston`/`pino`), or be prepared to fork and maintain the library yourself.","message":"The `express-bunyan-logger` package is no longer actively maintained by its original author. The maintainer explicitly stated a lack of spare time for module maintenance, inviting others to take over. This implies that new features, bug fixes, and security updates are unlikely to be released.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Upgrade to version 1.3.3 or later immediately to address known security vulnerabilities. It's also critical to regularly audit your dependency tree.","message":"Versions prior to 1.3.3 had known security vulnerabilities related to the `useragent` dependency (#51). While 1.3.3 bumps the dependencies to fix this, using older versions exposes your application.","severity":"gotcha","affected_versions":"<1.3.3"},{"fix":"Use `require()` syntax for imports. For modern ESM-only projects, consider an alternative logging solution that offers native ESM support.","message":"This library is primarily designed for CommonJS (CJS) environments and the README examples exclusively use `require()`. Direct ES Modules (ESM) import is not officially supported and may lead to issues or require CJS interop.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"If you need to obfuscate sensitive data in your logs, upgrade to version 1.3.0 or later and configure the `obfuscate` option in the middleware.","message":"The `options.obfuscate` feature, which allows obfuscating nested properties of logged JSON (e.g., passwords in `req.body`), was introduced in version 1.3.0.","severity":"gotcha","affected_versions":"<1.3.0"},{"fix":"If you rely on `immediate: true` to log at the precise request reception time, ensure you are on version 1.2.0 or higher. For older versions, the log might still be delayed until response is sent, potentially affecting response time calculations.","message":"The `options.immediate` behavior was fixed in v1.2.0. Before this version, when `immediate` was true, it would still log when the response was sent. After v1.2.0, it logs strictly immediately upon request reception.","severity":"gotcha","affected_versions":"<1.2.0"}],"env_vars":null,"last_verified":"2026-04-23T00:00:00.000Z","next_check":"2026-07-22T00:00:00.000Z","problems":[{"fix":"Install Bunyan: `npm install bunyan` or `npm install express-bunyan-logger bunyan`.","cause":"The `bunyan` package, which is a core dependency, has not been installed.","error":"Error: Cannot find module 'bunyan'"},{"fix":"Ensure `app` is correctly initialized as `const app = express();` and that `express` is imported: `const express = require('express');`.","cause":"You are attempting to use `app.use()` on an object that is not an Express application instance, or `app` is undefined.","error":"TypeError: app.use is not a function"},{"fix":"Ensure `app.use(require('express-bunyan-logger')());` is placed early in your Express middleware stack, before any routes or other middleware that rely on `req.log`.","cause":"The `express-bunyan-logger` middleware has not been applied to the request pipeline before the code attempting to access `req.log`.","error":"TypeError: req.log is undefined"},{"fix":"When initializing `express-bunyan-logger` (or the underlying Bunyan logger), ensure `streams` are configured, e.g., `streams: [{ level: 'info', stream: process.stdout }]`, and that the `level` matches or is lower than the messages you expect to see.","cause":"The underlying Bunyan logger might not have correctly configured streams, or its log level is too high for the messages being emitted.","error":"No logs appear in console/file despite middleware being active."}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}