简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM