Skip to content

Commit

Permalink
up: update some config for create log handler
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jun 9, 2022
1 parent d2af535 commit e775d38
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 53 deletions.
79 changes: 51 additions & 28 deletions handler/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,66 @@ type ConfigFn func(c *Config)
type Config struct {
// Logfile for write logs
Logfile string `json:"logfile" yaml:"logfile"`

// Levels for log record
Levels []slog.Level `json:"levels" yaml:"levels"`

// UseJSON for format logs
UseJSON bool `json:"use_json" yaml:"use_json"`

// BuffMode type name. allow: line, bite
BuffMode string `json:"buff_mode" yaml:"buff_mode"`
// BuffSize for enable buffer. set 0 to disable buffer

// BuffSize for enable buffer, unit is bytes. set 0 to disable buffer
BuffSize int `json:"buff_size" yaml:"buff_size"`
// RotateTime for rotate file

// RotateTime for rotate file, unit is seconds.
RotateTime rotatefile.RotateTime `json:"rotate_time" yaml:"rotate_time"`
// MaxSize on rotate file by size.

// MaxSize on rotate file by size, unit is bytes.
MaxSize uint64 `json:"max_size" yaml:"max_size"`

// Compress determines if the rotated log files should be compressed using gzip.
// The default is not to perform compression.
Compress bool `json:"compress" yaml:"compress"`

// BackupNum max number for keep old files.
//
// 0 is not limit, default is 20.
BackupNum uint `json:"backup_num" yaml:"backup_num"`

// BackupTime max time for keep old files, unit is hours.
//
// 0 is not limit, default is a week.
BackupTime uint `json:"backup_time" yaml:"backup_time"`

// RenameFunc build filename for rotate file
RenameFunc func(filepath string, rotateNum uint) string
}

// NewEmptyConfig new config instance
func NewEmptyConfig(fns ...ConfigFn) *Config {
c := &Config{Levels: slog.AllLevels}
return c.With(fns...)
}

// NewConfig new config instance with some default settings.
func NewConfig(fns ...ConfigFn) *Config {
c := &Config{
Levels: slog.AllLevels,
BuffMode: BuffModeLine,
BuffSize: DefaultBufferSize,
// rotate file settings
MaxSize: rotatefile.DefaultMaxSize,
RotateTime: rotatefile.EveryHour,
// old files clean settings
BackupNum: rotatefile.DefaultBackNum,
BackupTime: rotatefile.DefaultBackTime,
}

return c.With(fns...)
}

// With more config settings func
func (c *Config) With(fns ...ConfigFn) *Config {
for _, fn := range fns {
Expand Down Expand Up @@ -86,13 +127,17 @@ func (c *Config) CreateWriter() (output SyncCloseWriter, err error) {

// create a rotate config.
if c.MaxSize > 0 || c.RotateTime > 0 {
rc := rotatefile.NewConfig(c.Logfile)
rc.MaxSize = c.MaxSize
rc := rotatefile.EmptyConfigWith()

// has locked on logger.write()
rc.CloseLock = true
rc.Compress = c.Compress
rc.Filepath = c.Logfile
// copy settings
rc.MaxSize = c.MaxSize
rc.RotateTime = c.RotateTime
rc.BackupNum = c.BackupNum
rc.BackupTime = c.BackupTime
rc.Compress = c.Compress

if c.RenameFunc != nil {
rc.RenameFunc = c.RenameFunc
Expand Down Expand Up @@ -134,28 +179,6 @@ func (c *Config) wrapBuffer(w io.Writer) (bw flushSyncCloseWriter) {
return bw
}

// NewEmptyConfig new config instance
func NewEmptyConfig(fns ...ConfigFn) *Config {
c := &Config{
Levels: slog.AllLevels,
}
return c.With(fns...)
}

// NewConfig new config instance with some default settings.
func NewConfig(fns ...ConfigFn) *Config {
c := &Config{
Levels: slog.AllLevels,
MaxSize: rotatefile.DefaultMaxSize,
BuffMode: BuffModeLine,
BuffSize: DefaultBufferSize,
// time rotate settings
RotateTime: rotatefile.EveryHour,
}

return c.With(fns...)
}

// WithLogfile setting
func WithLogfile(logfile string) ConfigFn {
return func(c *Config) {
Expand Down
49 changes: 38 additions & 11 deletions rotatefile/clean_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,43 @@ import (
"time"
)

// CConfig struct for clean
// CConfig struct for clean files
type CConfig struct {
// BackupNum max number for keep old files.
// 0 is not limit, default is 20.
BackupNum uint `json:"backup_num" yaml:"backup_num"`

// BackupTime max time for keep old files.
// 0 is not limit, default is a week.
// BackupTime max time for keep old files, unit is hours.
//
// unit is hours
// 0 is not limit, default is a week.
BackupTime uint `json:"backup_time" yaml:"backup_time"`

// Compress determines if the rotated log files should be compressed
// using gzip. The default is not to perform compression.
Compress bool `json:"compress" yaml:"compress"`

// TimeClock for rotate
// FileDirs list
FileDirs []string `json:"file_dirs" yaml:"file_dirs"`

// TimeClock for clean files
TimeClock Clocker

// skip error
// TODO skipError bool
}

// AddFileDir for clean
func (c *CConfig) AddFileDir(dirs ...string) *CConfig {
c.FileDirs = append(c.FileDirs, dirs...)
return c
}

// NewCConfig instance
func NewCConfig() *CConfig {
return &CConfig{
BackupNum: DefaultBackNum,
BackupTime: DefaultBackTime,
}
}

// FilesClear multi files by time. TODO
Expand All @@ -35,26 +54,34 @@ type FilesClear struct {
filepathDirs []string
// full file path patterns
filePatterns []string
// file max backup time. equals Config.BackupTime * time.Hour
// file max backup time. equals CConfig.BackupTime * time.Hour
backupDur time.Duration
// skip error
// TODO skipError bool
}

// NewCleanFiles instance
func NewCleanFiles(cfg *CConfig) *FilesClear {
// NewFilesClear instance
func NewFilesClear(cfg *CConfig) *FilesClear {
if cfg == nil {
cfg = NewCConfig()
}

return &FilesClear{
cfg: cfg,
}
}

// WithConfigFn for custom settings
func (r *FilesClear) WithConfigFn(fn func(c *CConfig)) *FilesClear {
fn(r.cfg)
return r
}

//
// ---------------------------------------------------------------------------
// clean backup files
// ---------------------------------------------------------------------------
//

const flushInterval = 30 * time.Second
const flushInterval = 60 * time.Second

// DaemonClean daemon clean old files by config
func (r *FilesClear) DaemonClean() {
Expand Down
14 changes: 14 additions & 0 deletions rotatefile/clean_files_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package rotatefile_test

import (
"testing"

"github.com/gookit/slog/rotatefile"
)

func TestNewFilesClear(t *testing.T) {
fc := rotatefile.NewFilesClear(nil)
fc.WithConfigFn(func(c *rotatefile.CConfig) {
c.AddFileDir("./testdata")
})
}
20 changes: 6 additions & 14 deletions rotatefile/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,13 @@ type Config struct {
CloseLock bool `json:"close_lock" yaml:"close_lock"`

// BackupNum max number for keep old files.
// 0 is not limit, default is 20.
//
// 0 is not limit, default is DefaultBackNum
BackupNum uint `json:"backup_num" yaml:"backup_num"`

// BackupTime max time for keep old files.
// 0 is not limit, default is a week.
// BackupTime max time for keep old files, unit is hours.
//
// unit is hours
// 0 is not limit, default is DefaultBackTime
BackupTime uint `json:"backup_time" yaml:"backup_time"`

// Compress determines if the rotated log files should be compressed using gzip.
Expand All @@ -180,12 +180,6 @@ type Config struct {
// default see DefaultFilenameFn
RenameFunc func(filePath string, rotateNum uint) string

// AfterFunc hook func. It will fire when rotating the file is done.
//
// TIP: Please do not do time-consuming operations in it.
// It is recommended to use the go keyword to call logic in it.
// AfterFunc func(bakFilePath string)

// TimeClock for rotate
TimeClock Clocker
}
Expand All @@ -208,9 +202,7 @@ func (c *Config) With(fns ...ConfigFn) *Config {
}

// Create new Writer by config
func (c *Config) Create() (*Writer, error) {
return NewWriter(c)
}
func (c *Config) Create() (*Writer, error) { return NewWriter(c) }

const (
// OneMByte size
Expand Down Expand Up @@ -248,9 +240,9 @@ var (
func NewDefaultConfig() *Config {
return &Config{
MaxSize: DefaultMaxSize,
RotateTime: EveryHour,
BackupNum: DefaultBackNum,
BackupTime: DefaultBackTime,
RotateTime: EveryHour,
RenameFunc: DefaultFilenameFn,
TimeClock: DefaultTimeClockFn,
}
Expand Down
7 changes: 7 additions & 0 deletions rotatefile/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ func TestNewConfig(t *testing.T) {
assert.Equal(t, rotatefile.DefaultMaxSize, cfg.MaxSize)

dump.P(cfg)

cfg = rotatefile.EmptyConfigWith(func(c *rotatefile.Config) {
c.Compress = true
})
assert.True(t, cfg.Compress)
assert.Equal(t, 0, cfg.BackupNum)
assert.Equal(t, 0, cfg.BackupTime)
}

func TestRotateTime_String(t *testing.T) {
Expand Down

0 comments on commit e775d38

Please sign in to comment.