How can I use "config.colorize" in my custom formatter?
See original GitHub issueI 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:
- Created 9 years ago
- Comments:5 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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); };
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’ }) ] });`