Winstonjs - different formats for different levels?

Viewed 204

I'm trying to dress up my log messages with winstonjs for easier debugging. I am working within a single transport (namely transports.Console), and I want to use different formats for different levels. For example, I have custom levels info, warn, error, and header. For header, I use bold black:

import Winston, { createLogger, format, info, transports } from 'winston';
 
const levels = {
    levels: {
        info: 0,
        header: 1,
        warn: 2,
        error: 3,
    },
    colors: {
        info: 'green',
        header: 'bold black',
        warn: 'yellowBG',
        error: 'red',
    },
};

const logger = createLogger({ 
    levels: levels.levels,
    transports: [
        new transports.Console({
            level: 'error',
            format: format.combine(
                format.colorize(),
                format.align(),
            ),
        })
    ]
});

Winston.addColors(levels.colors);

By default, the colors are applied to the level of the log, so we get this:

enter image description here

But what if I want the entire message to be bold for just the header level?

I know we can add options to format.colorize(), like format.colorize({ all: true }), which makes the whole message takes the color formatting. However, this happens to all levels. So it looks like this:

enter image description here

But I only want to apply the all: true option to the header level, and none of the other levels.

Is this possible? I know its possible to create different transports with different formats, or different loggers, but can I use different formats for different levels within the same transport? Or even customize the format of a single message one at a time (i.e. logger.log(message, someCustomFormatJustForThisMessage)` - Is anything like this possible? (If this would be better asked as an issue on the winston repo, let me know)

0 Answers
Related