Colorize doesn't work on transports.Console
See original GitHub issueI created a shared logger successfully, but it prints all white, even though I’ve set colorize to true. And as a test, My Label successfully prints out in red.
// Logging with Winston
const { red } = require('colors')
const os = require('os')
const fs = require('fs')
const path = require('path')
const config = require('../data/config/config')
const winston = require('winston')
const { createLogger, format, transports } = winston
const { combine, timestamp, label, printf } = format
const tsFormat = () => (new Date()).toLocaleTimeString()
const myFormat = printf(info => {
return `${info.timestamp} [${info.label}] ${info.level}: ${info.message}`
})
const logDir = path.resolve(os.homedir(), '.curator-server')
if (!fs.existsSync(logDir)) {
fs.mkdirSync(logDir)
}
const logger = createLogger({
format: combine(
label({ label: red('My Label') }),
timestamp(),
myFormat
),
transports: [
new transports.Console({ level: config.logLevel, humanReadableUnhandledException: true, colorize: true }),
new transports.File({ filename: path.resolve(logDir, 'info.log'), level: 'info' }),
new transports.File({ filename: path.resolve(logDir, 'error.log'), level: 'error' })
]
})
module.exports = logger
Issue Analytics
- State:
- Created 6 years ago
- Reactions:4
- Comments:12 (6 by maintainers)
Top Results From Across the Web
Winston 3.0 colorize whole output on console - Stack Overflow
I have already tried different NPM Packages like winston color and winston-console-formatter, but they don't seem to work.
Read more >How to use the winston.format.colorize function in winston - Snyk
Use Snyk Code to scan source code in minutes - no build needed - and fix issues ... function createConsoleTransport (args, logLvl) {...
Read more >A Complete Guide to Winston Logging in Node.js - Better Stack
The following transport options are available in Winston by default: Console: output logs to the Node.js console. File: store log messages to one...
Read more >logform.colorize JavaScript and Node.js code examples
createLogger({ level: appConfig.logs.level || 'silly', format: combine(colorize(), timestamp(), myFormat), transports: [ new transports.Console({ colorize: ...
Read more >winston - npm
Note that the default logger doesn't have any transports by default. ... to be written in winston; Working with multiple Loggers in winston....
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
Right, so I’ve looked more into this. You can’t “format.colorize()” after you use “myFormat” since the modifications colorize does would not be seen in the final message.
You would need to add colorize and myFormat to the format specifiers of each transport instead of for the whole logger.
Try moving the colorize to a format property inside your Console constructor: new transports.Console({ level: config.logLevel, format: colorize() })