Skip to content

[Bug]: Message property is undefined when throwing an error #2531

@Wazbat

Description

@Wazbat

🔎 Search Terms

uncaughtException,message undefined, message,undefined

The problem

When logging an uncaught exception with winston, the message property is always undefined when using the console transport

What version of Winston presents the issue?

v3.8.1

What version of Node are you using?

v22.3.0

If this worked in a previous version of Winston, which was it?

No response

Minimum Working Example

const { createLogger, transports, format } = require('winston');

const logger = createLogger({
    transports: [new transports.Console({
        format: format.printf((info) => `[${info.timestamp}] ${info.level}: ${info.message}`)
    })],
    level: 'debug'
});

process.on('uncaughtException', (err) => {
    logger.error(err);
});

throw new Error('this text is lost and not shown anywhere');

Additional information

This is my full logger file, which hopefully shows why I'm handling uncaughtExceptions this way

import { createLogger, transports, format } from 'winston';
import { LoggingWinston } from '@google-cloud/logging-winston';

export const getLogger = (application: string, options?: { silent?: boolean}) => {
    const transportsToUse: NonNullable<NonNullable<Parameters<typeof createLogger>[0]>['transports']> = [];
    if (process.env.NODE_ENV === 'production') {
        const loggingWinston = new LoggingWinston({
            redirectToStdout: true,
            useMessageField: false,
            level: 'info',
            serviceContext: {
                service: application,
                version: process.env.VERSION || 'v0.0.0'
            },
            labels: {
                namespace: process.env.NAMESPACE || 'unknown',
                version: process.env.VERSION || 'unknown'
            }
        });
        transportsToUse.push(loggingWinston);
    } else {
        transportsToUse.push(new transports.Console({
            level: 'debug',
            format: format.combine(
                format.colorize(),
                format.timestamp({
                    format: 'YYYY-MM-DD HH:mm:ss'
                }),
                format.errors({ stack: true }),
                format.printf((info) => `[${info.timestamp}] ${info.level}: ${info.message}`)
            )
        }));
    }
    const logger = createLogger({
        transports: transportsToUse,
        silent: options?.silent ?? false
    });
    process.on('uncaughtException', (err) => {
        logger.error(err);
        logger.on('finish', () => {
            process.exit(1);
        });
        logger.on('error', (e) => {
            // eslint-disable-next-line no-console
            console.error('Winston error while logging uncaughtException');
            // eslint-disable-next-line no-console
            console.error(e);
            process.exit(1);
        });
        // https://github.com/googleapis/nodejs-logging-winston/issues/502
        logger.end();
    });
    process.on('unhandledRejection', (reason: Error, promise) => {
        logger.error(`unhandledRejection: ${reason?.message || reason}`, {
            error: reason,
            promise
        });
        logger.error(reason);
    });

    return logger;
};

Perhaps I'm just going about this the wrong way?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions