You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@rocketmq.apache.org by du...@apache.org on 2022/05/26 05:46:36 UTC

[rocketmq-client-go] branch master updated: 1. support rlog rotate

This is an automated email from the ASF dual-hosted git repository.

duhengforever pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/rocketmq-client-go.git


The following commit(s) were added to refs/heads/master by this push:
     new 7bc7af5  1. support rlog rotate
     new 2d7d781  Merge pull request #829 from WJL3333/support_log_rotate
7bc7af5 is described below

commit 7bc7af5848b840f1608ca4500fc588f32f81f16c
Author: wangjinlong.1048576 <wa...@bytedance.com>
AuthorDate: Fri May 13 18:55:14 2022 +0800

    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 0aac6ba..34e8d8d 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 157f415..888e9a8 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 a179a40..b387ed7 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