Documentation
¶
Overview ¶
package rotatelogs is a port of File-RotateLogs from Perl (https://metacpan.org/release/File-RotateLogs), and it allows you to automatically rotate output files when you write to them according to the filename pattern that you can specify.
Index ¶
- Variables
- type AgingFunc
- type Clock
- type Event
- type EventType
- type FileRotatedEvent
- type Handler
- type HandlerFunc
- type LogFileInfo
- type NamingFunc
- type Option
- func ForceNewFile() Option
- func WithAgingFunc(f AgingFunc) Option
- func WithClock(c Clock) Option
- func WithHandler(h Handler) Option
- func WithLinkName(s string) Option
- func WithLocation(loc *time.Location) Option
- func WithMaxAge(d time.Duration) Option
- func WithNamingFunc(f NamingFunc) Option
- func WithRotationCount(n uint) Option
- func WithRotationSize(s int64) Option
- func WithRotationTime(d time.Duration) Option
- type RotateLogs
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var Local = clockFn(time.Now)
Local is an object satisfying the Clock interface, which returns the current time in the local timezone
var UTC = clockFn(func() time.Time { return time.Now().UTC() })
UTC is an object satisfying the Clock interface, which returns the current time in UTC
Functions ¶
This section is empty.
Types ¶
type AgingFunc ¶
type AgingFunc func(files []LogFileInfo) []string
AgingFunc 自定义日志老化清理回调,决定哪些旧日志文件应被删除
- files: 所有匹配的日志文件(已过滤 _lock/_symlink)
返回值为要删除的文件路径列表 设置后将完全替代内置的 maxAge / rotationCount 清理逻辑
type FileRotatedEvent ¶
type FileRotatedEvent struct {
// contains filtered or unexported fields
}
func (*FileRotatedEvent) CurrentFile ¶
func (e *FileRotatedEvent) CurrentFile() string
func (*FileRotatedEvent) PreviousFile ¶
func (e *FileRotatedEvent) PreviousFile() string
func (*FileRotatedEvent) Type ¶
func (e *FileRotatedEvent) Type() EventType
type HandlerFunc ¶
type HandlerFunc func(Event)
func (HandlerFunc) Handle ¶
func (h HandlerFunc) Handle(e Event)
type LogFileInfo ¶
LogFileInfo 记录匹配到的日志文件路径及其文件信息, 供 AgingFunc 自定义老化清理逻辑使用
type NamingFunc ¶
NamingFunc 自定义滚动日志文件命名规则
- baseFilename: strftime 模式生成的基础文件名(如 "/var/log/app.log")
- generation: 代际编号,从 1 开始递增
返回值为该代际实际使用的完整文件名 若不设置,默认规则为 "文件名.代际.扩展名"(如 app.1.log)
type Option ¶
type Option interface {
Name() string
Value() interface{}
}
Option is used to pass optional arguments to the RotateLogs constructor
func ForceNewFile ¶
func ForceNewFile() Option
ForceNewFile ensures a new file is created every time New() is called. If the base file name already exists, an implicit rotation is performed
Example ¶
package main
import (
"fmt"
"os"
rotatelogs "github.com/khan-lau/file-rotatelogs"
)
func main() {
logDir, err := os.MkdirTemp("", "rotatelogs_test")
if err != nil {
fmt.Println("could not create log directory ", err)
return
}
logPath := fmt.Sprintf("%s/test.log", logDir)
for i := 0; i < 2; i++ {
writer, innerErr := rotatelogs.New(logPath, rotatelogs.ForceNewFile())
if innerErr != nil {
fmt.Println("Could not open log file ", innerErr)
return
}
n, writeErr := writer.Write([]byte("test"))
if writeErr != nil || n != 4 {
fmt.Println("Write failed ", writeErr, " number written ", n)
return
}
err = writer.Close()
if err != nil {
fmt.Println("Close failed ", err)
return
}
}
files, err := os.ReadDir(logDir)
if err != nil {
fmt.Println("ReadDir failed ", err)
return
}
for _, file := range files {
info, infoErr := file.Info()
if infoErr != nil {
fmt.Println("Info failed ", infoErr)
continue
}
fmt.Println(file.Name(), info.Size())
}
err = os.RemoveAll(logDir)
if err != nil {
fmt.Println("RemoveAll failed ", err)
return
}
}
Output: test.1.log 4 test.log 4
func WithAgingFunc ¶
WithAgingFunc 设置自定义日志老化清理回调,决定哪些旧日志文件应被删除。 设置后将完全替代内置的 maxAge / rotationCount 清理逻辑。
回调接收所有匹配的日志文件(含 os.FileInfo), 返回要删除的文件路径列表。
func WithClock ¶
WithClock creates a new Option that sets a clock that the RotateLogs object will use to determine the current time.
By default rotatelogs.Local, which returns the current time in the local time zone, is used. If you would rather use UTC, use rotatelogs.UTC as the argument to this option, and pass it to the constructor.
func WithHandler ¶
WithHandler creates a new Option that specifies the Handler object that gets invoked when an event occurs. Currently `FileRotated` event is supported
func WithLinkName ¶
WithLinkName creates a new Option that sets the symbolic link name that gets linked to the current file name being used.
func WithLocation ¶
WithLocation creates a new Option that sets up a "Clock" interface that the RotateLogs object will use to determine the current time.
This optin works by always returning the in the given location.
func WithMaxAge ¶
WithMaxAge creates a new Option that sets the max age of a log file before it gets purged from the file system.
func WithNamingFunc ¶
func WithNamingFunc(f NamingFunc) Option
WithNamingFunc 设置自定义滚动日志文件命名规则。 当需要滚动日志且新文件名与已有文件冲突时,回调此函数。 参数为 strftime 模式生成的基础文件名和代际编号,返回代际文件名。
不设置时默认规则为:"文件名.代际编号.扩展名"(如 app.log → app.1.log)。
func WithRotationCount ¶
WithRotationCount creates a new Option that sets the number of files should be kept before it gets purged from the file system.
func WithRotationSize ¶
WithRotationSize creates a new Option that sets the log file size between rotation.
func WithRotationTime ¶
WithRotationTime creates a new Option that sets the time between rotation.
type RotateLogs ¶
type RotateLogs struct {
// contains filtered or unexported fields
}
RotateLogs represents a log file that gets automatically rotated as you write to it.
func New ¶
func New(p string, options ...Option) (*RotateLogs, error)
New creates a new RotateLogs object. A log filename pattern must be passed. Optional `Option` parameters may be passed
func (*RotateLogs) Close ¶
func (rl *RotateLogs) Close() error
Close satisfies the io.Closer interface. You must call this method if you performed any writes to the object.
func (*RotateLogs) CurrentFileName ¶
func (rl *RotateLogs) CurrentFileName() string
CurrentFileName returns the current file name that the RotateLogs object is writing to
func (*RotateLogs) Rotate ¶
func (rl *RotateLogs) Rotate() error
Rotate forcefully rotates the log files. If the generated file name clash because file already exists, a numeric suffix of the form ".1", ".2", ".3" and so forth are appended to the end of the log file
Thie method can be used in conjunction with a signal handler so to emulate servers that generate new log files when they receive a SIGHUP
func (*RotateLogs) Write ¶
func (rl *RotateLogs) Write(p []byte) (n int, err error)
Write satisfies the io.Writer interface. It writes to the appropriate file handle that is currently being used. If we have reached rotation time, the target file gets automatically rotated, and also purged if necessary.
