Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Winston Colorizer throwing error

Tags:

winston

Updated winston to 3.0.0 Got this error when running my nodejs application the following error appears

  TypeError: colors[Colorizer.allColors[lookup]] is not a function
like image 687
Asif Saeed Avatar asked Jul 02 '18 11:07

Asif Saeed


3 Answers

If you are adding custom colors to your Winston configuration you can only use colors defined by the colors package. This was the issue I had.

like image 148
gavs Avatar answered Nov 15 '22 22:11

gavs


If you are using format.colorize, you must take care to always indicate the log level.

You can indicate it as the initial parameter, in this case 'error':

logger.log('error', 'hello', { message: 'world' });

You can call its specific method:

logger.error('hello', { message: 'world' });

The problem that you encountered occurs if at any time you omit the level; for example, if you write something like this:

logger.log('hello', { message: 'world' });
like image 33
David Rengifo Avatar answered Nov 15 '22 22:11

David Rengifo


Used winstone package: version 3.8.0

If you are using colorize() from Winstone package, and you created a custom level, you must define a color for this level.

Winston is using logform package for supplying default colors. The crash will occur in colorize.js file inside logform package:

colorize.js:

    if (!Array.isArray(Colorizer.allColors[lookup])) {
      return colors[Colorizer.allColors[lookup]](message);
    }

'lookup' variable will be undefined if you won't define a color for your custom level.

Notice how I define a color for 'request' level

import { config, addColors } from 'winston';


// Adding a custom level named 'request'
const levels = { ...config.syslog.levels, request: 6.5 };

// Configuring 'request' color
addColors({ ...config.syslog.colors, request: config.syslog.colors.debug });

Image

(Notice there is a "request" color - it got added)

like image 2
Eden Avital Avatar answered Nov 15 '22 22:11

Eden Avital