Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
4.2k views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...