如何解决使用 Google Cloud Winston Logging (Nodejs) 记录日志前后的有趣字符
我正在使用 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}`));
它按预期记录,但在 Google 云控制台上查看时,日志消息前后都有有趣的字符。以下是日志的一些示例:
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”,但它们让我很难阅读我的日志。
解决方法
这些字符称为 ANSI 转义码或转义序列。它们用于修改终端显示文本的方式。例如更改颜色或使文本加粗。这可能会使消息在终端上看起来更好,但会使处理日志文件更加困难。
这些字符不是由 Google Cloud 日志记录添加的,而是由您系统上的应用程序或记录器软件添加的。
如果您检查问题中的示例代码,请注意 label
和 colorize
。这些函数正在生成转义码。
const customFormats = (category) => combine(
label({label: category}),colorize({all: true}),
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。