winstonjs/winston · error · Error

Logger is required to handle exceptions

Error message

Logger is required to handle exceptions

What it means

ExceptionHandler wraps a Logger to catch uncaught exceptions and route them to the logger's transports. Its constructor requires the logger argument; throwing early when it is missing/undefined prevents creating a broken handler that could not dispatch exception events.

Source

Thrown at lib/winston/exception-handler.js:28

const os = require('os');
const asyncForEach = require('async/forEach');
const debug = require('@dabh/diagnostics')('winston:exception');
const once = require('one-time');
const stackTrace = require('stack-trace');
const ExceptionStream = require('./exception-stream');

/**
 * Object for handling uncaughtException events.
 * @type {ExceptionHandler}
 */
module.exports = class ExceptionHandler {
  /**
   * TODO: add contructor description
   * @param {!Logger} logger - TODO: add param description
   */
  constructor(logger) {
    if (!logger) {
      throw new Error('Logger is required to handle exceptions');
    }

    this.logger = logger;
    this.handlers = new Map();
  }

  /**
   * Handles `uncaughtException` events for the current process by adding any
   * handlers passed in.
   * @returns {undefined}
   */
  handle(...args) {
    args.forEach(arg => {
      if (Array.isArray(arg)) {
        return arg.forEach(handler => this._addHandler(handler));
      }

      this._addHandler(arg);

View on GitHub (pinned to ff0b79de85)

Solutions

  1. Construct the logger first and pass it: `new winston.ExceptionHandler(logger)`.
  2. Prefer the public API `logger.exceptions.handle(transport)` which creates the handler for you with the correct logger.
  3. Ensure the logger is created before any code that builds the exception handler (module initialization order).
  4. Guard against undefined logger variables before constructing the handler.

Example fix

// before
const handler = new winston.ExceptionHandler(loggerMaybeUndefined);

// after
if (!logger) throw new Error('Create logger before ExceptionHandler');
const handler = new winston.ExceptionHandler(logger);
// or simply:
logger.exceptions.handle(new winston.transports.File({ filename: 'exceptions.log' }));
Defensive patterns

Strategy: type-guard

Validate before calling

if (!logger || typeof logger.log !== 'function') {
  throw new TypeError('ExceptionHandler requires a winston Logger instance');
}

Type guard

function isLogger(x) { return !!x && typeof x === 'object' && typeof x.log === 'function' && typeof x.exceptions === 'object'; }

Try / catch

try {
  handler = new winston.ExceptionHandler(logger);
} catch (err) {
  if (err.message === 'Logger is required to handle exceptions') {
    handler = logger.exceptions.handle(fileTransport);
  } else { throw err; }
}

Prevention

When it happens

Trigger: Calling `new winston.ExceptionHandler()` with no argument, or with `undefined`/`null`/falsy value — typically from hand-rolled exception plumbing instead of using `logger.exceptions.handle(...)`.

Common situations: Manually instantiating ExceptionHandler in custom logging setups; refactoring code where the logger variable is not yet initialized when the handler is constructed; typos or shadowed logger variables.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of winstonjs/winston@ff0b79de85 (2026-08-31). Data as JSON: /api/errors/14ba4379a787758f. Report an issue: GitHub.