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

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

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

问题

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

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

  1. func NewLogger(logLevel string) (*zap.SugaredLogger, error) {
  2. encoderConfig := zapcore.EncoderConfig{
  3. TimeKey: "Time",
  4. LevelKey: "Level",
  5. NameKey: "Name",
  6. CallerKey: "Caller",
  7. MessageKey: "Msg",
  8. StacktraceKey: "St",
  9. EncodeLevel: zapcore.CapitalColorLevelEncoder,
  10. EncodeTime: zapcore.ISO8601TimeEncoder,
  11. EncodeDuration: zapcore.StringDurationEncoder,
  12. EncodeCaller: zapcore.ShortCallerEncoder,
  13. }
  14. consoleEncoder := zapcore.NewConsoleEncoder(encoderConfig)
  15. consoleOut := zapcore.Lock(os.Stdout)
  16. var level zap.AtomicLevel
  17. err := level.UnmarshalText(([]byte(logLevel)))
  18. if err != nil {
  19. level.UnmarshalText([]byte(defaultLogLevel))
  20. }
  21. core := zapcore.NewTee(zapcore.NewCore(
  22. consoleEncoder,
  23. consoleOut,
  24. level,
  25. ))
  26. logger := zap.New(core)
  27. Logger = logger.Sugar()
  28. return Logger, nil
  29. }

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

  1. 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

  1. func NewLogger(logLevel string) (*zap.SugaredLogger, error) {
  2. encoderConfig := zapcore.EncoderConfig{
  3. TimeKey: "Time",
  4. LevelKey: "Level",
  5. NameKey: "Name",
  6. CallerKey: "Caller",
  7. MessageKey: "Msg",
  8. StacktraceKey: "St",
  9. EncodeLevel: zapcore.CapitalColorLevelEncoder,
  10. EncodeTime: zapcore.ISO8601TimeEncoder,
  11. EncodeDuration: zapcore.StringDurationEncoder,
  12. EncodeCaller: zapcore.ShortCallerEncoder,
  13. }
  14. consoleEncoder := zapcore.NewConsoleEncoder(encoderConfig)
  15. consoleOut := zapcore.Lock(os.Stdout)
  16. var level zap.AtomicLevel
  17. err := level.UnmarshalText(([]byte(logLevel)))
  18. if err != nil {
  19. level.UnmarshalText([]byte(defaultLogLevel))
  20. }
  21. core := zapcore.NewTee(zapcore.NewCore(
  22. consoleEncoder,
  23. consoleOut,
  24. level,
  25. ))
  26. logger := zap.New(core)
  27. Logger = logger.Sugar()
  28. return Logger, nil
  29. }

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

  1. 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转义代码

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

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

英文:

To disable colors you can remove ASCII Escape Codes:

  1. - EncodeLevel: zapcore.CapitalColorLevelEncoder
  2. + 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:

确定