mirror of
https://github.com/eosswedenorg/thalos
synced 2026-06-16 04:24:56 +02:00
internal/log/RotatingFile.go: use option functions.
This commit is contained in:
parent
bcdaa9cea4
commit
90b10f39ae
1 changed files with 19 additions and 10 deletions
|
|
@ -24,7 +24,7 @@ func open(filename string) (*os.File, error) {
|
|||
}
|
||||
|
||||
// Open a new rotating file.
|
||||
func NewRotatingFile(filename string, maxSize int64, maxAge time.Duration) (*RotatingFile, error) {
|
||||
func NewRotatingFile(filename string, opts ...RotatingFileOption) (*RotatingFile, error) {
|
||||
if err := os.MkdirAll(path.Dir(filename), 0o766); err != nil && !os.IsExist(err) {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -39,14 +39,18 @@ func NewRotatingFile(filename string, maxSize int64, maxAge time.Duration) (*Rot
|
|||
return nil, err
|
||||
}
|
||||
|
||||
return &RotatingFile{
|
||||
fd: fd,
|
||||
size: stat.Size(),
|
||||
maxSize: maxSize,
|
||||
ts: time.Now(),
|
||||
maxAge: maxAge,
|
||||
format: "2006-01-02_150405",
|
||||
}, nil
|
||||
file := &RotatingFile{
|
||||
fd: fd,
|
||||
size: stat.Size(),
|
||||
ts: time.Now(),
|
||||
format: "2006-01-02_150405",
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(file)
|
||||
}
|
||||
|
||||
return file, nil
|
||||
}
|
||||
|
||||
// Open a new rotating file using a config struct.
|
||||
|
|
@ -55,7 +59,12 @@ func NewRotatingFileFromConfig(config Config, suffix string) (*RotatingFile, err
|
|||
suffix = "_" + suffix
|
||||
}
|
||||
|
||||
return NewRotatingFile(config.GetFilePath()+suffix+".log", int64(config.MaxFileSize), config.MaxTime)
|
||||
filename := config.GetFilePath() + suffix + ".log"
|
||||
|
||||
return NewRotatingFile(filename, func(f *RotatingFile) {
|
||||
f.maxAge = config.MaxTime
|
||||
f.maxSize = int64(config.MaxFileSize)
|
||||
})
|
||||
}
|
||||
|
||||
func (w *RotatingFile) newFilename(name string) string {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue