为什么Lambda函数生成的AWS CloudWatch日志中有额外的字符串?

huangapple go评论103阅读模式
英文:

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

问题

我有一个基于Golang的Lambda函数,在执行过程中记录一些信息。

我使用的是以下的zapcore Golang日志记录器:

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
}

然而,当它在CloudWatch中生成日志时,我看到日志中有一个额外的字符。

2022-06-30T21:52:43.310-07:00 2022-07-01T04:52:43.310Z INFO Process my event
  1. 为什么日志中会生成包含**[34m[0m**字符串的内容?
  2. 我是否真的需要生成带有时间戳的日志,因为我看到CloudWatch已经为日志添加了时间戳。
英文:

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 INFO 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

得分: 0

要禁用颜色,您可以删除ASCII转义代码

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

我认为您不需要重复时间信息,如果以后需要更多的指标,您可能会记录为JSON,并且在从CloudWatch查询时已经有时间信息了。

英文:

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.

huangapple
  • 本文由 发表于 2022年7月2日 00:19:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/72832111.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定