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
- Construct the logger first and pass it: `new winston.ExceptionHandler(logger)`.
- Prefer the public API `logger.exceptions.handle(transport)` which creates the handler for you with the correct logger.
- Ensure the logger is created before any code that builds the exception handler (module initialization order).
- 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
- Create the Logger at app bootstrap before wiring exception handling
- Use `logger.exceptions.handle(...)` instead of instantiating ExceptionHandler directly
- Check initialization order when splitting logger setup across modules
- Add an integration test that enables exception handling at startup
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
- ExceptionStream requires a TransportStream instance.
- Logger is required for profiling
- Logger is required to handle rejections
- Transports must WritableStreams in objectMode. Set { objectM
- RejectionStream requires a TransportStream instance.
AI-assisted analysis of winstonjs/winston@ff0b79de85 (2026-08-31).
Data as JSON: /api/errors/14ba4379a787758f.
Report an issue: GitHub.