【问题标题】:Funny Characters Before and at End of Logs with Google Cloud Winston Logging (Nodejs)使用 Google Cloud Winston Logging (Nodejs) 的日志前后有趣的字符
【发布时间】:2021-01-16 11:51:13
【问题描述】:

我正在使用 google-cloud/logging-winston nodejs 包进行日志记录,并以这种方式创建了用于输出的自定义格式化程序:

const winston = require('winston');
const { LoggingWinston } = require('@google-cloud/logging-winston');
const { format } = winston;
const { combine, label, json, timestamp, printf, colorize, simple } = format;
const path = require('path');

const customFormats = (category) => combine(
    label({label: category}),
    colorize({all: true}),
    // simple()
    timestamp(),
    json(),
    printf((info) => `${info.timestamp} - [${info.label?`${info.label}`:"NO_LABEL"}] - [${info.level}] : ${info.message}`));

它按预期记录,但在谷歌云控制台上查看时,日志消息前后有有趣的字符。以下是一些日志示例:

2021-01-16T10:58:00.836Z - [DEFAULT] - [[32minfo[39m] : [32mValidating route @/bills/airtime/send[39m
2021-01-16T10:58:00.841Z - [AIRTIME] - [[31merror[39m] : [31mAirtime recharge error Low account balance[39m

我不知道这些是什么意思:“[32m”、“[31m”或“[39m”,但它们让我很难阅读我的日志。

【问题讨论】:

    标签: node.js google-cloud-platform winston google-cloud-logging


    【解决方案1】:

    这些字符称为 ANSI 转义码或转义序列。它们用于修改终端显示文本的方式。例如更改颜色或使文本加粗。这可能会使消息在终端上看起来更好,但会使处理日志文件更加困难。

    这些字符不是由 Google Cloud 日志记录添加的,而是由您系统上的应用程序或记录器软件添加的。

    ANSI escape code

    如果您检查问题中的示例代码,请注意 labelcolorize。这些函数正在生成转义码。

    const customFormats = (category) => combine(
        label({label: category}),
        colorize({all: true}),
    

    【讨论】:

    • 谢谢。我评论了 colorize({all: true}) 行,这些字符消失了。
    猜你喜欢
    • 1970-01-01
    • 2020-07-07
    • 2020-01-03
    • 1970-01-01
    • 2019-07-22
    • 2017-08-17
    • 2021-08-06
    • 2020-09-12
    • 1970-01-01
    相关资源
    最近更新 更多