From 7bc7af5848b840f1608ca4500fc588f32f81f16c Mon Sep 17 00:00:00 2001 From: "wangjinlong.1048576" Date: Fri, 13 May 2022 18:55:14 +0800 Subject: [PATCH] 1. support rlog rotate --- go.mod | 1 + go.sum | 2 ++ rlog/log.go | 51 +++++++++++++++++++++++++++++++++++++++++++++------ 3 files changed, 48 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index 0aac6ba3..34e8d8d0 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 157f4152..888e9a8e 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/rlog/log.go b/rlog/log.go index a179a407..b387ed78 100644 --- a/rlog/log.go +++ b/rlog/log.go @@ -19,8 +19,11 @@ package rlog import ( "os" + "path/filepath" "strings" + "gopkg.in/natefinch/lumberjack.v2" + "github.com/sirupsen/logrus" ) @@ -123,14 +126,49 @@ 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 } @@ -138,6 +176,7 @@ func (l *defaultLogger) OutputPath(path string) (err error) { func SetLogger(logger Logger) { rLog = logger } + func SetLogLevel(level string) { if level == "" { return