最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - winston saves color formatter in text, how to remove this but still show color? - Stack Overflow

programmeradmin0浏览0评论

I am trying to create a logging module using winston as the logging framework. It allows me to specify colors, which is quite nice if the transport is Console, but if I were to define two transports, one Console, one File, it will actually save the terminal formatter string in the text, which will be saved in the file.

const { addColors, createLogger, format, transports } = require('winston');

const { combine, colorize, printf, timestamp } = format;

const logFormat = printf((info) => {
  return `[${info.timestamp}] ${info.level}: ${info.message}`;
});

const rawFormat = printf((info) => {
  return `[${info.timestamp}] ${info.level}: ${info.message}`;
});

const config = require('../config');

const logger = createLogger({
  level: config.DEBUG,
  format: combine(
    colorize(),
    timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
    logFormat,
  ),
  transports: [
    new transports.File({
      filename: 'combined.log',
    }),
    new transports.Console({ format: combine(timestamp(), rawFormat) }),
  ],
});

addColors({
  debug: 'white',
  error: 'red',
  info: 'green',
  warn: 'yellow',
});

module.exports = logger;

Console will look like this:

[2018-06-12 15:54:14] info: Listening on port 9000

Where info is colorized to green, but if i look at the combine.log file, it will show as:

[2018-06-12 15:54:14] [32minfo[39m: Listening on port 9000

Is it possible to log raw text to a file, but still show colors in terminal?

If not, is there another library that allows for this? Say bunyan?

I am trying to create a logging module using winston as the logging framework. It allows me to specify colors, which is quite nice if the transport is Console, but if I were to define two transports, one Console, one File, it will actually save the terminal formatter string in the text, which will be saved in the file.

const { addColors, createLogger, format, transports } = require('winston');

const { combine, colorize, printf, timestamp } = format;

const logFormat = printf((info) => {
  return `[${info.timestamp}] ${info.level}: ${info.message}`;
});

const rawFormat = printf((info) => {
  return `[${info.timestamp}] ${info.level}: ${info.message}`;
});

const config = require('../config');

const logger = createLogger({
  level: config.DEBUG,
  format: combine(
    colorize(),
    timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
    logFormat,
  ),
  transports: [
    new transports.File({
      filename: 'combined.log',
    }),
    new transports.Console({ format: combine(timestamp(), rawFormat) }),
  ],
});

addColors({
  debug: 'white',
  error: 'red',
  info: 'green',
  warn: 'yellow',
});

module.exports = logger;

Console will look like this:

[2018-06-12 15:54:14] info: Listening on port 9000

Where info is colorized to green, but if i look at the combine.log file, it will show as:

[2018-06-12 15:54:14] [32minfo[39m: Listening on port 9000

Is it possible to log raw text to a file, but still show colors in terminal?

If not, is there another library that allows for this? Say bunyan?

Share Improve this question asked Jun 12, 2018 at 20:10 PGTPGT 2,0292 gold badges26 silver badges49 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 15

I don't know if u need this anymore but late is better than never.

Basically you just need to drop colorzie()from general format that you have defined then use it in the specify transports you want to show color.

var options = {
    console: {
        handleExceptions: true,
        level: 'debug',
        format: combine(colorize(), myFormat)
    },
    verbose: {
        filename: './logs/debug/mobile_api-%DATE%.log',
        level: 'debug',
        format: combine(myFormat)
    },
}
const logger = winston.createLogger({
levels: myCustomLevels.levels,
transports: [
    new transports.Console(options.console),
    new transports.file(options.verbose),
]
});

With this, when the log get printed in the console, you can see color and when write to file, it won't show any color code at all.

You can use the uncolorized method from winston.format. Read about it in logform's documentation.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论