question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How can I use "config.colorize" in my custom formatter?

See original GitHub issue

I want to be able to add color in my custom formatter, how can I call the “colorize” method to add color to me logging message?

in common.js

var util = require('util'),
  ...
  config = require('./config');

  ...

  output = timestamp ? timestamp + ' - ' : '';
  if (showLevel) {
    output += options.colorize === 'all' || options.colorize === 'level' || options.colorize === true
      ? config.colorize(options.level)
      : options.level;
  }

but within the formatter we don’t have access to it ? Am I right ?

var transport = new (winston.transports.Console)(
    colorize: true
    prettyPrint: true
    formatter: (options) ->
      # Return string will be passed to logger.
      formatted = '## [' + options.timestamp().format() + ']' +
          '[' +config.colorize(options.level.toUpperCase()) + ']' + # <--------

           ...

      return formatted
  )

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

17reactions
somnathpanjacommented, Aug 4, 2015

Instead of var config = require(winston/lib/winston/config);

Try this it works well for me

var formatterFunc = function (options) { return winston.config.colorize(options.level,options.level.toUpperCase()) + “: [” + this.timestamp() +"| " + options.meta.codePath + "] " + options.message + " " + JSON.stringify(options.meta); };

0reactions
broker-okositlakcommented, Mar 17, 2017

Using @somnathpanja ‘s solution and still make it possible to use colorize option: `var myLogFormatter = function (options) { var formatted = options.timestamp() + ’ [’ + options.level.toUpperCase() + '] ’ + (options.message ? options.message : ‘’) + (options.meta && Object.keys(options.meta).length ? ‘\n\t’ + JSON.stringify(options.meta) : ‘’); return options.colorize ? winston.config.colorize(options.level, formatted) : formatted; }

const myLogger = new (winston.Logger)({ transports: [ // colorize the output to the console new (winston.transports.Console)({ timestamp: tsFormat, formatter: myLogFormatter, colorize: true, level: ‘debug’ }), new (winston.transports.File)({ filename: ${logDir}/results.log, timestamp: tsFormat, formatter: myLogFormatter, level: env === ‘development’ ? ‘debug’ : ‘info’ }) ] });`

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use the winston.format.colorize function in winston - Snyk
To help you get started, we've selected a few winston.format.colorize examples, ... set the custom colors const colorizeFormat = format.colorize({ colors, }); ...
Read more >
Make your own custom color formatter with Python logging
A custom color formatter. For building our own custom formatter, we will extend the logging.Formatter class, give it the log format we want,...
Read more >
How To Color Python Logging Output? | Better Stack Community
Finally, make your formatter return this custom format: FORMATS = { logging. ... For this, you can use an external module named colorlog...
Read more >
Configurable colors - Formatters - RSpec Core - Relish
RSpec allows you to configure the terminal colors used in the text formatters. failure_color : Color used when tests fail (default: :red );...
Read more >
winston saves color formatter in text, how to remove this but ...
Basically you just need to drop colorzie() from general format that you have defined then use it in the specify transports you want...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found