Rotation logs – How to add timestamp to log file name?

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

Rotation logs - How to add timestamp to log file name?

问题

以下是旋转日志的代码:

  1. package main
  2. import (
  3. "os"
  4. "go.uber.org/zap"
  5. "go.uber.org/zap/zapcore"
  6. lumberjack "gopkg.in/natefinch/lumberjack.v2"
  7. )
  8. func main() {
  9. logPath, _ := os.Getwd()
  10. log := NewLoggerFp(logPath, 1, 2, 2)
  11. log.Infof("sjkshfjsdf\n")
  12. log.Infof("sjkshfjsdf\n")
  13. log.Infof("sjkshfjsdf\n")
  14. }
  15. func NewLoggerFp(logPath string, maxSize, maxBackUp, maxAge int) *zap.SugaredLogger {
  16. w := zapcore.AddSync(&lumberjack.Logger{
  17. Filename: logPath + "/dump.log",
  18. MaxSize: maxSize, // megabytes
  19. MaxBackups: maxBackUp,
  20. MaxAge: maxAge, // days
  21. })
  22. core := zapcore.NewCore(
  23. zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()),
  24. w,
  25. zap.InfoLevel,
  26. )
  27. return zap.New(core).Sugar()
  28. }

日志会被创建。
如何在 dump.log 中添加时间戳?

英文:

Below code for rotation logs:

  1. package main
  2. import (
  3. "os"
  4. "go.uber.org/zap"
  5. "go.uber.org/zap/zapcore"
  6. lumberjack "gopkg.in/natefinch/lumberjack.v2"
  7. )
  8. func main() {
  9. logPath, _ := os.Getwd()
  10. log := NewLoggerFp(logPath, 1, 2, 2)
  11. log.Infof("sjkshfjsdf\n")
  12. log.Infof("sjkshfjsdf\n")
  13. log.Infof("sjkshfjsdf\n")
  14. }
  15. func NewLoggerFp(logPath string, maxSize, maxBackUp, maxAge int) *zap.SugaredLogger {
  16. w := zapcore.AddSync(&lumberjack.Logger{
  17. Filename: logPath + "/dump.log",
  18. MaxSize: maxSize, // megabytes
  19. MaxBackups: maxBackUp,
  20. MaxAge: maxAge, // days
  21. })
  22. core := zapcore.NewCore(
  23. zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()),
  24. w,
  25. zap.InfoLevel,
  26. )
  27. return zap.New(core).Sugar()
  28. }

Logs get created
How to add timestamp to dump.log?

答案1

得分: 2

我试着理解Christian的建议。也许你可以尝试这样做。

  1. w := zapcore.AddSync(&lumberjack.Logger{
  2. Filename: logPath + fmt.Sprintf("/dump-%v.log", time.Now().Format(time.RFC822)),
  3. MaxSize: maxSize, // 兆字节
  4. MaxBackups: maxBackUp,
  5. MaxAge: maxAge, // 天数
  6. })
英文:

I try to understand what Christian suggests. Maybe you can try this.

  1. w := zapcore.AddSync(&lumberjack.Logger{
  2. Filename: logPath + fmt.Sprintf("/dump-%v.log", time.Now().Format(time.RFC822)),
  3. MaxSize: maxSize, // megabytes
  4. MaxBackups: maxBackUp,
  5. MaxAge: maxAge, // days
  6. })

答案2

得分: 1

你是否尝试过使用较小的MaxSize(或MaxAge)参数进行测试?

根据https://pkg.go.dev/gopkg.in/natefinch/lumberjack.v2#example-Logger.Rotate的说明,如果文件存在且小于MaxSize兆字节,Lumberjack将追加到第一个文件。当达到此限制时,会自动添加时间戳:

每当写入导致当前日志文件超过MaxSize兆字节时,当前文件将被关闭、重命名,并创建一个具有原始名称的新日志文件。

备份使用Logger给定的日志文件名,格式为name-timestamp.ext,其中name是没有扩展名的文件名,timestamp是日志轮换的时间,格式为2006-01-02T15-04-05.000,扩展名是原始扩展名。

例如,如果你的Logger.Filename是/var/log/foo/server.log,在2016年11月11日下午6:30创建的备份将使用文件名/var/log/foo/server-2016-11-04T18-30-00.000.log

如果文件存在且其大小>= MaxSize兆字节,则在文件的扩展名之前(或文件名末尾,如果没有扩展名)插入当前时间的时间戳来重命名文件。然后,使用原始文件名创建一个新的日志文件。

有一个开放的拉取请求可以自定义时间戳格式:https://github.com/natefinch/lumberjack/pull/118

英文:

Have you tested it with a smaller MaxSize (or MaxAge) parameter?

According to https://pkg.go.dev/gopkg.in/natefinch/lumberjack.v2#example-Logger.Rotate, Lumberjack will append to the first file if the file exists and is less than MaxSize megabytes. When this limit is reached, timestamps are automatically added:

> Whenever a write would cause the current log file exceed MaxSize megabytes, the current file is closed, renamed, and a new log file created with the original name.
>
> Backups use the log file name given to Logger, in the form name-timestamp.ext where name is the filename without the extension, timestamp is the time at which the log was rotated formatted with the time.Time format of 2006-01-02T15-04-05.000 and the extension is the original extension.
>
> For example, if your Logger.Filename is /var/log/foo/server.log, a backup created at 6:30pm on Nov 11 2016 would use the filename /var/log/foo/server-2016-11-04T18-30-00.000.log
>
> If the file exists and its size is >= MaxSize megabytes, the file is renamed by putting the current time in a timestamp in the name immediately before the file's extension (or the end of the filename if there's no extension). A new log file is then created using original filename.

There is an open pull request to be able to customize the time stamp format: https://github.com/natefinch/lumberjack/pull/118

huangapple
  • 本文由 发表于 2022年8月28日 09:24:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/73515403.html
匿名

发表评论

匿名网友

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

确定