Why extra string in AWS Cloud watch logs generated by Lambda function?

Viewed 45

I have golang based lambda function which does some work and logs the information during execution.

I'm using zapcore Golang logger as below

func NewLogger(logLevel string) (*zap.SugaredLogger, error) {

    encoderConfig := zapcore.EncoderConfig{
        TimeKey:        "Time",
        LevelKey:       "Level",
        NameKey:        "Name",
        CallerKey:      "Caller",
        MessageKey:     "Msg",
        StacktraceKey:  "St",
        EncodeLevel:    zapcore.CapitalColorLevelEncoder,
        EncodeTime:     zapcore.ISO8601TimeEncoder,
        EncodeDuration: zapcore.StringDurationEncoder,
        EncodeCaller:   zapcore.ShortCallerEncoder,
    }

    consoleEncoder := zapcore.NewConsoleEncoder(encoderConfig)

    consoleOut := zapcore.Lock(os.Stdout)

    var level zap.AtomicLevel
    err := level.UnmarshalText(([]byte(logLevel)))

    if err != nil {
        level.UnmarshalText([]byte(defaultLogLevel))
    }

    core := zapcore.NewTee(zapcore.NewCore(
        consoleEncoder,
        consoleOut,
        level,
    ))

    logger := zap.New(core)
    Logger = logger.Sugar()

    return Logger, nil
}

However when it generates the logs in CloudWatch, I see an extra character in the logs.

2022-06-30T21:52:43.310-07:00   2022-07-01T04:52:43.310Z [34mINFO[0m Process my event
  1. Why the logs is getting generated with [34m and [0m string in it?
  2. Do I really need to generate logs with timestamp because I see cloudwatch already adds timestamp to logs.
1 Answers

To disable colors you can remove ASCII Escape Codes:

- EncodeLevel:    zapcore.CapitalColorLevelEncoder
+ EncodeLevel:    zapcore.CapitalLevelEncoder

I don't think you need to repeat time information, if you need more metrics later you'll probably log as JSONs and there will be already time information when querying from CloudWatch.

Related