Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.5k views
in Technique[技术] by (71.8m points)

node.js - Funny Characters Before and at End of Logs with Google Cloud Winston Logging (Nodejs)

I am using the google-cloud/logging-winston nodejs package for logging, and I created my custom formatter for output this way:

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}`));

It logs as expected, but there are funny characters before and after the log messages when viewed on Google cloud console. Below are some examples of the log:

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

I don't know what these mean: "[32m", "[31m" or "[39m" but they make it hard to read my logs.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Those characters are called ANSI Escape Codes or Escape Sequences. They are used to modify how terminals display text. For example to change the color or make text bold. This might make messages look better on a terminal but make processing logfiles more difficult.

Those characters are not added by Google Cloud logging but by either the application or the logger software on your system.

ANSI escape code

If you examine the example code in your question, notice label and colorize. Those functions are generating the escape codes.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...