How to use the addColors function from winston

Find comprehensive JavaScript winston.addColors code examples handpicked from public code repositorys.

winston.addColors is a method used to add custom logging levels with corresponding colors to the Winston logging library.

24
25
26
27
28
29
30
31
32
33
      timestamp: true,
      prettyPrint: true
    })
  ]
});
winston.addColors({
  trace: 'magenta',
  input: 'grey',
  verbose: 'cyan',
  prompt: 'grey',
fork icon35
star icon222
watch icon9

+ 7 other calls in file

14
15
16
17
18
19
20
21
22
23
function createLogger() {
    if (rawLogFormat) {
        colorize = winston.format.colorize().colorize;
        // if colors updated ensure you update native colors also
        // black,red,green,yellow,blue,magenta,cyan,white,gray,grey,brightRed,brightGreen,brightYellow,brightBlue,brightMagenta,brightCyan,brightWhite
        winston.addColors({ error: 'brightRed', warn: 'brightYellow', info: 'brightBlue', debug: 'brightGreen' });
    }
    const logger = winston.createLogger({
        level: LOG_LEVEL,
        format: format.combine(
fork icon4
star icon4
watch icon4

+ 18 other calls in file

How does winston.addColors work?

The winston.addColors method is used to define custom color levels for logging in the winston logging library, by providing a mapping of log levels to their corresponding color codes. The method takes an object as input, where the keys represent the log levels and the values are color codes. The color codes can be specified using the chalk library or as raw ANSI color codes. Once defined, these color levels can be used to format log output.

51
52
53
54
55
56
57
58
59
60
 * }}
 */
exports.loggerFactory = function loggerFactory(logFileDirectory, type) {
    const consoleLogLevel = getLogLevel()
    const filePathParts = logFileDirectory.split('/')
    addColors(customLevels.colors)
    return createLogger({
        levels: customLevels.levels,
        format: combine(
            splat(),
fork icon0
star icon1
watch icon1

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const winston = require("winston");

// Define custom log colors
const customColors = {
  error: "red",
  warn: "yellow",
  info: "green",
  debug: "blue",
};

// Add custom log colors to Winston
winston.addColors(customColors);

// Create a logger with a custom color scheme
const logger = winston.createLogger({
  level: "info",
  format: winston.format.simple(),
  transports: [
    new winston.transports.Console({
      format: winston.format.combine(
        winston.format.colorize({ all: true }),
        winston.format.simple()
      ),
    }),
  ],
});

// Test the logger
logger.info("This is an info message with custom colors.");
logger.warn("This is a warning message with custom colors.");
logger.error("This is an error message with custom colors.");
logger.debug("This is a debug message with custom colors.");

In this example, we first define a custom set of log colors using an object with color names as keys and color values as values. We then add these colors to the default Winston colors using winston.addColors(customColors). Next, we create a logger with a custom color scheme by setting up a console transport with a format that includes both colorization and a simple log format. Finally, we use the logger to test the custom color scheme by logging messages at different levels. Each log message will be colorized based on its level using the custom colors we defined earlier.