Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.1k 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 Answer

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}),

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...