This is an automated email from the ASF dual-hosted git repository.
dockerzhang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/inlong.git
The following commit(s) were added to refs/heads/master by this push:
new 05b92ddbed [INLONG-8791][TubeMQ] Add log level and log path
configuration API to Tubemq-client-go (#10040)
05b92ddbed is described below
commit 05b92ddbed99d33a050df5696d42ddd1b325dc7a
Author: XiaoYou201 <[email protected]>
AuthorDate: Mon Apr 22 20:54:55 2024 +0800
[INLONG-8791][TubeMQ] Add log level and log path configuration API to
Tubemq-client-go (#10040)
---
.../tubemq-client-go/config/config.go | 47 ++++++++++++++++++++++
.../tubemq-client-go/log/config.go | 43 ++++++++++++++++++++
2 files changed, 90 insertions(+)
diff --git
a/inlong-tubemq/tubemq-client-twins/tubemq-client-go/config/config.go
b/inlong-tubemq/tubemq-client-twins/tubemq-client-go/config/config.go
index 687990cd8d..27f636b4fd 100644
--- a/inlong-tubemq/tubemq-client-twins/tubemq-client-go/config/config.go
+++ b/inlong-tubemq/tubemq-client-twins/tubemq-client-go/config/config.go
@@ -22,6 +22,7 @@ import (
"encoding/json"
"errors"
"fmt"
+
"github.com/apache/inlong/inlong-tubemq/tubemq-client-twins/tubemq-client-go/log"
"net/url"
"strconv"
"strings"
@@ -143,6 +144,15 @@ type Config struct {
// AfterFail is the heartbeat timeout after a heartbeat failure.
AfterFail time.Duration
}
+
+ // Log is the namespace for configuration related to log messages,
+ // used by the logger
+ Log struct {
+ // LogPath represents the path where the log save in
+ LogPath string
+ // LogLevel represents the level of log
+ LogLevel string
+ }
}
// NewDefaultConfig returns a default config of the client.
@@ -175,6 +185,9 @@ func NewDefaultConfig() *Config {
c.Heartbeat.MaxRetryTimes = 5
c.Heartbeat.AfterFail = 60000 * time.Millisecond
+ c.Log.LogLevel = "warn"
+ c.Log.LogPath = "../log/tubemq.log"
+
return c
}
@@ -465,6 +478,16 @@ func getConfigFromToken(config *Config, values []string)
error {
config.Net.Auth.UserName = values[1]
case "authPassword":
config.Net.Auth.Password = values[1]
+ case "logPath":
+ err = log.SetLogPath(values[1])
+ if err == nil {
+ config.Log.LogPath = values[1]
+ }
+ case "logLevel":
+ err = log.SetLogLevel(values[1])
+ if err == nil {
+ config.Log.LogLevel = values[1]
+ }
default:
return fmt.Errorf("address format invalid, unknown keys: %v",
values[0])
}
@@ -620,3 +643,27 @@ func WithConsumePosition(consumePosition int) Option {
c.Consumer.ConsumePosition = consumePosition
}
}
+
+// WithLogLevel set log level
+func WithLogLevel(level string) Option {
+ return func(c *Config) {
+ err := log.SetLogLevel(level)
+ if err != nil {
+ log.Errorf("[Log]log level setting error: %s",
err.Error())
+ return
+ }
+ c.Log.LogLevel = level
+ }
+}
+
+// WithLogPath set log path
+func WithLogPath(path string) Option {
+ return func(c *Config) {
+ err := log.SetLogPath(path)
+ if err != nil {
+ log.Errorf("[Log]log path setting error %s",
err.Error())
+ return
+ }
+ c.Log.LogPath = path
+ }
+}
diff --git a/inlong-tubemq/tubemq-client-twins/tubemq-client-go/log/config.go
b/inlong-tubemq/tubemq-client-twins/tubemq-client-go/log/config.go
index fee8f4a75e..df281b7051 100644
--- a/inlong-tubemq/tubemq-client-twins/tubemq-client-go/log/config.go
+++ b/inlong-tubemq/tubemq-client-twins/tubemq-client-go/log/config.go
@@ -17,6 +17,11 @@
package log
+import (
+ "errors"
+ "os"
+)
+
// OutputConfig defines the output config which can be reconfigured by user.
type OutputConfig struct {
// LogPath is the path for the log.
@@ -39,3 +44,41 @@ var defaultConfig = &OutputConfig{
MaxAge: 3,
Level: "warn",
}
+
+// SetLogLevel set log level
+func SetLogLevel(level string) error {
+ if _, exist := LevelNames[level]; !exist {
+ return errors.New("the supported log levels are: trace, debug,
info, warn, error, fatal. Please check your log level")
+ }
+ defaultConfig.Level = level
+ return nil
+}
+
+// SetLogPath set log path
+func SetLogPath(path string) error {
+ err := verifyLogPath(path)
+ if err != nil {
+ return err
+ }
+ defaultConfig.LogPath = path
+ return nil
+}
+
+func verifyLogPath(path string) error {
+ // Check if file already exists
+ if _, err := os.Stat(path); err == nil {
+ return nil
+ }
+
+ // Attempt to create it
+ var d []byte
+ err := os.WriteFile(path, d, 0644)
+ if err == nil {
+ removeErr := os.Remove(path)
+ if removeErr != nil {
+ return removeErr
+ } // And delete it
+ return nil
+ }
+ return err
+}