Skip to content

Commit

Permalink
Merge pull request #829 from WJL3333/support_log_rotate
Browse files Browse the repository at this point in the history
[ISSUE #828] Support rlog rotate
  • Loading branch information
duhenglucky authored May 26, 2022
2 parents 0189e44 + 7bc7af5 commit 2d7d781
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ require (
github.com/stretchr/testify v1.3.0
github.com/tidwall/gjson v1.13.0
go.uber.org/atomic v1.5.1
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
stathat.com/c/consistent v1.0.0 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,7 @@ golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBn
golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c h1:IGkKhmfzcztjm6gYkykvu/NiS8kaqbCWAEWWAyf8J5U=
golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8=
gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k=
stathat.com/c/consistent v1.0.0 h1:ezyc51EGcRPJUxfHGSgJjWzJdj3NiMU9pNfLNGiXV0c=
stathat.com/c/consistent v1.0.0/go.mod h1:QkzMWzcbB+yQBL2AttO6sgsQS/JSTapcDISJalmCDS0=
51 changes: 45 additions & 6 deletions rlog/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ package rlog

import (
"os"
"path/filepath"
"strings"

"gopkg.in/natefinch/lumberjack.v2"

"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -123,21 +126,57 @@ func (l *defaultLogger) Level(level string) {
}
}

func (l *defaultLogger) OutputPath(path string) (err error) {
var file *os.File
file, err = os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
return
type Config struct {
OutputPath string
MaxFileSizeMB int
MaxBackups int
MaxAges int
Compress bool
LocalTime bool
}

func (c *Config) Logger() *lumberjack.Logger {
return &lumberjack.Logger{
Filename: filepath.ToSlash(c.OutputPath),
MaxSize: c.MaxFileSizeMB, // MB
MaxBackups: c.MaxBackups,
MaxAge: c.MaxAges, // days
Compress: c.Compress, // disabled by default
LocalTime: c.LocalTime,
}
}

const defaultLogPath = "/tmp/rocketmq-client.log"

func defaultConfig() Config {
return Config{
OutputPath: defaultLogPath,
MaxFileSizeMB: 10,
MaxBackups: 5,
MaxAges: 3,
Compress: false,
LocalTime: true,
}
}

func (l *defaultLogger) Config(conf Config) (err error) {
l.logger.Out = conf.Logger()
return
}

func (l *defaultLogger) OutputPath(path string) (err error) {
config := defaultConfig()
config.OutputPath = path

l.logger.Out = file
l.logger.Out = config.Logger()
return
}

// SetLogger use specified logger user customized, in general, we suggest user to replace the default logger with specified
func SetLogger(logger Logger) {
rLog = logger
}

func SetLogLevel(level string) {
if level == "" {
return
Expand Down

0 comments on commit 2d7d781

Please sign in to comment.