Winston: 控制台日志记录未着色。

创建于 2017-11-10  ·  22评论  ·  资料来源: winstonjs/winston

我正在尝试为控制台输出着色,但它不起作用。 输出都是相同的(白色)颜色。 我在 3.0.0-rc1 上。

const winston = require('winston');
const logLevels = {
  levels: {
    error: 0,
    warn: 1,
    info: 2,
    http: 3,
    sql: 4,
    debug: 5
  },
  colors: {
    error: "red",
    warn: "darkred",
    info: "black",
    http: "green",
    sql: "blue",
    debug: "gray"
  }
};
winston.addColors(logLevels);
const logger = winston.createLogger({...});
logger.add(new winston.transports.Console({colorize: true}));

我也在这里尝试了这个建议,但这也不起作用:

logger.add(new winston.transports.Console({format: winston.format.combine(formatter, winston.format.colorize())}));

谢谢,
阿尔瓦罗

最有用的评论

我能够使用logform的文档使其部分工作。

  const alignedWithColorsAndTime = winston.format.combine(
    winston.format.colorize(),
    winston.format.timestamp(),
    winston.format.align(),
    winston.format.printf(info => `${info.timestamp} [${info.level}]: ${info.message}`),
  );

不确定如何处理转储 JSON 参数。

这里的更新是我的工作:

  const alignedWithColorsAndTime = winston.format.combine(
    winston.format.colorize(),
    winston.format.timestamp(),
    winston.format.align(),
    winston.format.printf((info) => {
      const {
        timestamp, level, message, ...args
      } = info;

      const ts = timestamp.slice(0, 19).replace('T', ' ');
      return `${ts} [${level}]: ${message} ${Object.keys(args).length ? JSON.stringify(args, null, 2) : ''}`;
    }),
  );

screen shot 2017-11-13 at 10 55 05 am

所有22条评论

我能够使用logform的文档使其部分工作。

  const alignedWithColorsAndTime = winston.format.combine(
    winston.format.colorize(),
    winston.format.timestamp(),
    winston.format.align(),
    winston.format.printf(info => `${info.timestamp} [${info.level}]: ${info.message}`),
  );

不确定如何处理转储 JSON 参数。

这里的更新是我的工作:

  const alignedWithColorsAndTime = winston.format.combine(
    winston.format.colorize(),
    winston.format.timestamp(),
    winston.format.align(),
    winston.format.printf((info) => {
      const {
        timestamp, level, message, ...args
      } = info;

      const ts = timestamp.slice(0, 19).replace('T', ' ');
      return `${ts} [${level}]: ${message} ${Object.keys(args).length ? JSON.stringify(args, null, 2) : ''}`;
    }),
  );

screen shot 2017-11-13 at 10 55 05 am

两个小时后,我设法让颜色正常工作,谢谢@Xeoncross

开始了温斯顿,也得到了没有颜色的控制台......
我正在使用下一个传输定义代码:

new winston.transports.Console({
  format: winston.format.simple()
})

读完这个帖子后,我这样做了:

new winston.transports.Console({
  format: winston.format.combine(
            winston.format.simple(),
            winston.format.colorize()
          )
})

并没有什么区别。
所以我将 colorize 格式的位置更改为 1st 并让颜色正常工作

new winston.transports.Console({
  format: winston.format.combine(
            winston.format.colorize(),
            winston.format.simple()
          )
})

谢谢@abrakadobr ! 那成功了。

这里没有任何东西对我有用,仍然没有颜色,并且将 colorize 移到顶部导致信息字段记录编码字符串

我创建了一个要点,展示了如何在一个简单的 expressjs 应用程序中使用 winston & morgan 登录。

谢谢@Xeoncross! 将 winston.format.colorize() 添加到我的格式中有效。 有趣的是,这不在文档中。 或者至少我在文档中找不到它。

我喜欢新的灵活性,但我希望它能得到更好的文档。 我只是花了太长时间才让它工作。 对于我们这些使用winston.cli()的人来说,一个即插即用的例子会有很长的路要走,类似于@Xeoncross 的例子。

任何人都知道如何使用当前发布版本来做到这一点? 发布候选版本不是一个选项。

在阅读代码时,您还可以将all发送到colorize作为对整个日志进行着色的选项。

colorize({ all: true })

@markhealey我已经从我的 package.json 中删除了 winston 依赖项,然后我阅读了您的消息。 谢谢 :+1:

谢谢@Xeoncross。 看看: https ://github.com/winstonjs/logform/blob/master/timestamp.js。 您可以使用format opt 参数格式化时间戳。 但要小心时区

format.combine(
    format.colorize(),
    format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
    format.align(),
    format.printf(info => {
        const { timestamp, level, message, ...extra } = info;

        return `${timestamp} [${level}]: ${message} ${
            Object.keys(extra).length ? JSON.stringify(extra, null, 2) : ''
        }`;
    }),
)

如果包含时间戳, format.colorize({all:true}就不会真正起作用,这是一个为整行着色的工作示例:

const colorizer = winston.format.colorize();

const logger = winston.createLogger({
  level: 'debug',
  format: combine(
    winston.format.timestamp(),
    winston.format.simple(),
    winston.format.printf(msg => 
      colorizer.colorize(msg.level, `${msg.timestamp} - ${msg.level}: ${msg.message}`)
    )
  ),
  transports: [
    new transports.Console(),
  ]

});

检查我的全功能记录器

谢谢@abrakadobr ,您的解决方案对我有用。

@tommuhm我现在看到了缺陷。 如果确实想为整个printf着色,则colorize没有内置格式选项。 你能理解这个吗?

const { createLogger, format, transports } = require('../');

const logger = createLogger({
  level: 'debug',
  format: format.combine(
    format.timestamp(),
    format.simple(),
    format.printf(info => `${info.timestamp} - ${info.level}: ${info.message}`),
    format.colorize({ all: true })
  ),
  transports: [
    new transports.Console(),
  ]
});

logger.error('wowza');

棘手的一点是在colorizer info已“序列化”(即当info[MESSAGE]已设置为字符串时)时知道 colorizer。 不久后对logform的后续 PR - 感谢您的深入研究!

@Xeoncross我们也可以以更简单的方式实现相同的目标

const { createLogger, format, transports } = require('winston');
const { combine, timestamp, colorize, printf } = format;

const level = process.env.LOG_LEVEL || 'debug';

const myFormat = printf(({ level, message, label, timestamp }) => {
    return `${timestamp} ${level}: ${message}`;
});

const logger = createLogger({
    format: combine(colorize(), timestamp(), myFormat),
    transports: [new transports.Console()]
});

参考: https: //www.npmjs.com/package/winston#using -custom-logging-levels

我正在使用 winston 登录 cloudwatch 并尝试添加颜色,但没有出现颜色符号。我尝试了上面提到的所有解决方案。 下面是我的代码

const winston = require('winston');
const {transports, format, createLogger  } = winston;
const { combine,errors,timestamp} = format;

const colorizer = winston.format.colorize();


const logger = createLogger({
    level:  process.env.LOG_LEVEL,
    prettyPrint : true,
    format: combine(
        winston.format.timestamp(),
        winston.format.simple(),
        winston.format.printf(msg =>
            colorizer.colorize(msg.level, `${msg.timestamp} - ${msg.level}: ${msg.message}`)
        )
    ),
    transports: [
        new transports.Console()
    ]
});

module.exports = {logger};

在 cloudwatch 控制台输出是

[34m2019-08-22T13:00:03.325Z - debug: res by id: [
{
    "isActive": true,
    "value": "fNUMheWiwXayKsaYCbUeA7Gg7BtEPIUbakB56XH1",
    "id": "oehlwqlcve",
    "userId": "6b347b41-ddef-4842-83a0-1cd5ca358482"
}
][39m

[39m出现而不是颜色。 有什么建议吗?

-22T13:00:03.325Z - 调试:通过 id 解析:[

我也有同样的问题。 你设法解决了吗?

-22T13:00:03.325Z - 调试:通过 id 解析:[

我也有同样的问题。 你设法解决了吗?

有什么更新吗? 你有没有以某种方式解决它?

@Xeoncross我们也可以以更简单的方式实现相同的目标

const { createLogger, format, transports } = require('winston');
const { combine, timestamp, colorize, printf } = format;

const level = process.env.LOG_LEVEL || 'debug';

const myFormat = printf(({ level, message, label, timestamp }) => {
  return `${timestamp} ${level}: ${message}`;
});

const logger = createLogger({
  format: combine(colorize(), timestamp(), myFormat),
  transports: [new transports.Console()]
});

参考: https: //www.npmjs.com/package/winston#using -custom-logging-levels

这对我在 MacOS 上的 iTerm 中起到了作用

此页面是否有帮助?
0 / 5 - 0 等级