Skip to content

Commit

Permalink
feat: 实现日志文件按文件大小切割
Browse files Browse the repository at this point in the history
1. xxx
2. xxx

close: zeromicro#1

MRTB: master
  • Loading branch information
one-the committed Jun 9, 2021
1 parent 5d86cc2 commit 1cd5072
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
1 change: 1 addition & 0 deletions core/logx/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ type LogConf struct {
Compress bool `json:",optional"`
KeepDays int `json:",optional"`
StackCooldownMillis int `json:",default=100"`
Size int64 `json:",optional"`
}
14 changes: 12 additions & 2 deletions core/logx/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ type (
gzipEnabled bool
logStackCooldownMills int
keepDays int
Size int64
}

// LogOption defines the method to customize the logging.
Expand Down Expand Up @@ -296,6 +297,13 @@ func WithKeepDays(days int) LogOption {
}
}

//WithSize customizes logging to size logs file size limit.
func WithSize(size int64) LogOption {
return func(options *logOptions) {
options.Size = size
}
}

// WithGzip customizes logging to automatically gzip the log files.
func WithGzip() LogOption {
return func(opts *logOptions) {
Expand All @@ -308,7 +316,7 @@ func createOutput(path string) (io.WriteCloser, error) {
return nil, ErrLogPathNotSet
}

return NewLogger(path, DefaultRotateRule(path, backupFileDelimiter, options.keepDays,
return NewLogger(path, DefaultRotateRule(path, backupFileDelimiter, options.keepDays, options.Size,
options.gzipEnabled), options.gzipEnabled)
}

Expand Down Expand Up @@ -433,7 +441,9 @@ func setupWithFiles(c LogConf) error {
if c.KeepDays > 0 {
opts = append(opts, WithKeepDays(c.KeepDays))
}

if c.Size > 0 {
opts = append(opts, WithSize(c.Size))
}
accessFile := path.Join(c.Path, accessFilename)
errorFile := path.Join(c.Path, errorFilename)
severeFile := path.Join(c.Path, severeFilename)
Expand Down
44 changes: 40 additions & 4 deletions core/logx/rotatelogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,26 @@ type (
delimiter string
days int
gzip bool
size int64
}
)

// DefaultRotateRule is a default log rotating rule, currently DailyRotateRule.
func DefaultRotateRule(filename, delimiter string, days int, gzip bool) RotateRule {
func DefaultRotateRule(filename, delimiter string, days int, size int64, gzip bool) RotateRule {
return &DailyRotateRule{
rotatedTime: getNowDate(),
filename: filename,
delimiter: delimiter,
days: days,
gzip: gzip,
size: size,
}
}

// BackupFileName returns the backup filename on rotating.
func (r *DailyRotateRule) BackupFileName() string {
return fmt.Sprintf("%s%s%s", r.filename, r.delimiter, getNowDate())
//return fmt.Sprintf("%s%s%s", r.filename, r.delimiter, getNowDate())
return fmt.Sprintf("%s%s%s%s%s%s%s", r.filename, r.delimiter, getNowDate(), r.delimiter, getNowHour(), getNowMinute(), getNowSecond())
}

// MarkRotated marks the rotated time of r to be the current time.
Expand Down Expand Up @@ -105,7 +108,8 @@ func (r *DailyRotateRule) OutdatedFiles() []string {

var buf strings.Builder
boundary := time.Now().Add(-time.Hour * time.Duration(hoursPerDay*r.days)).Format(dateFormat)
fmt.Fprintf(&buf, "%s%s%s", r.filename, r.delimiter, boundary)
//fmt.Fprintf(&buf, "%s%s%s", r.filename, r.delimiter, boundary)
fmt.Fprintf(&buf, "%s%s%s%s%s", r.filename, r.delimiter, boundary, r.delimiter, "000000")
if r.gzip {
buf.WriteString(".gz")
}
Expand All @@ -123,7 +127,15 @@ func (r *DailyRotateRule) OutdatedFiles() []string {

// ShallRotate checks if the file should be rotated.
func (r *DailyRotateRule) ShallRotate() bool {
return len(r.rotatedTime) > 0 && getNowDate() != r.rotatedTime
return (len(r.rotatedTime) > 0 && getNowDate() != r.rotatedTime) || (r.size > 0 && genFileSize(r.filename) >= r.size*1024*1024)
}

func genFileSize(fName string) int64 {
fi, err := os.Stat(fName)
if err != nil || fi == nil {
return 0
}
return fi.Size()
}

// NewLogger returns a RotateLogger with given filename and rule, etc.
Expand Down Expand Up @@ -304,6 +316,30 @@ func getNowDate() string {
return time.Now().Format(dateFormat)
}

func getNowHour() (h string) {
hi, _, _ := time.Now().Clock()
if hi < 10 {
return fmt.Sprintf("0%d", hi)
}
return fmt.Sprintf("%d", hi)
}

func getNowMinute() (m string) {
_, mi, _ := time.Now().Clock()
if mi < 10 {
return fmt.Sprintf("0%d", mi)
}
return fmt.Sprintf("%d", mi)
}

func getNowSecond() (s string) {
_, _, si := time.Now().Clock()
if si < 10 {
return fmt.Sprintf("0%d", si)
}
return fmt.Sprintf("%d", si)
}

func gzipFile(file string) error {
in, err := os.Open(file)
if err != nil {
Expand Down

0 comments on commit 1cd5072

Please sign in to comment.