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

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

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

问题

以下是旋转日志的代码:

package main

import (
	"os"

	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
	lumberjack "gopkg.in/natefinch/lumberjack.v2"
)

func main() {
	logPath, _ := os.Getwd()
	log := NewLoggerFp(logPath, 1, 2, 2)

	log.Infof("sjkshfjsdf\n")
	log.Infof("sjkshfjsdf\n")
	log.Infof("sjkshfjsdf\n")
}

func NewLoggerFp(logPath string, maxSize, maxBackUp, maxAge int) *zap.SugaredLogger {

	w := zapcore.AddSync(&lumberjack.Logger{

		Filename:   logPath + "/dump.log",
		MaxSize:    maxSize, // megabytes
		MaxBackups: maxBackUp,
		MaxAge:     maxAge, // days
	})

	core := zapcore.NewCore(
		zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()),
		w,
		zap.InfoLevel,
	)

	return zap.New(core).Sugar()
}

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

英文:

Below code for rotation logs:

package main

import (
	"os"

	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
	lumberjack "gopkg.in/natefinch/lumberjack.v2"
)

func main() {
	logPath, _ := os.Getwd()
	log := NewLoggerFp(logPath, 1, 2, 2)

	log.Infof("sjkshfjsdf\n")
	log.Infof("sjkshfjsdf\n")
	log.Infof("sjkshfjsdf\n")
}

func NewLoggerFp(logPath string, maxSize, maxBackUp, maxAge int) *zap.SugaredLogger {

	w := zapcore.AddSync(&lumberjack.Logger{

		Filename:   logPath + "/dump.log",
		MaxSize:    maxSize, // megabytes
		MaxBackups: maxBackUp,
		MaxAge:     maxAge, // days
	})

	core := zapcore.NewCore(
		zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()),
		w,
		zap.InfoLevel,
	)

	return zap.New(core).Sugar()
}

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

答案1

得分: 2

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

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

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

w := zapcore.AddSync(&lumberjack.Logger{

	Filename:   logPath + fmt.Sprintf("/dump-%v.log", time.Now().Format(time.RFC822)),
	MaxSize:    maxSize, // megabytes
	MaxBackups: maxBackUp,
	MaxAge:     maxAge, // days
})

答案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:

确定