Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE #828] Support rlog rotate #829

Merged
merged 1 commit into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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