Skip to content

Commit

Permalink
[INLONG-8791][TubeMQ] Add log level and log path configuration API to…
Browse files Browse the repository at this point in the history
… Tubemq-client-go
  • Loading branch information
XiaoYou201 committed Apr 21, 2024
1 parent 27d5777 commit aecdf8d
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/apache/inlong/inlong-tubemq/tubemq-client-twins/tubemq-client-go/log"
"net/url"
"os"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -143,6 +145,10 @@ type Config struct {
// AfterFail is the heartbeat timeout after a heartbeat failure.
AfterFail time.Duration
}
Log struct {
LogPath string
LogLevel string
}
}

// NewDefaultConfig returns a default config of the client.
Expand Down Expand Up @@ -174,6 +180,8 @@ func NewDefaultConfig() *Config {
c.Heartbeat.Interval = 10000 * time.Millisecond
c.Heartbeat.MaxRetryTimes = 5
c.Heartbeat.AfterFail = 60000 * time.Millisecond
c.Log.LogLevel = "warn"
c.Log.LogPath = "../log/tubemq.log"

return c
}
Expand Down Expand Up @@ -465,6 +473,10 @@ func getConfigFromToken(config *Config, values []string) error {
config.Net.Auth.UserName = values[1]
case "authPassword":
config.Net.Auth.Password = values[1]
case "logPath":
config.Log.LogPath, err = parseLogPath(values[1])
case "logLevel":
config.Log.LogLevel, err = parseLogLevel(values[1])
default:
return fmt.Errorf("address format invalid, unknown keys: %v", values[0])
}
Expand All @@ -481,6 +493,31 @@ func parseDuration(val string) (time.Duration, error) {
}
return time.Duration(maxWait) * time.Millisecond, err
}
func parseLogPath(path string) (string, error) {
// Check if file already exists
if _, err := os.Stat(path); err == nil {
return path, 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 path, nil
}
return "", err
}

func parseLogLevel(level string) (string, error) {
if _, exist := log.LevelNames[level]; !exist {
return "",
errors.New("the supported log levels are: trace, debug, info, warn, error, fatal. Please check your log level")
}
return level, nil
}

type Option func(*Config)

Expand Down Expand Up @@ -620,3 +657,17 @@ func WithConsumePosition(consumePosition int) Option {
c.Consumer.ConsumePosition = consumePosition
}
}

// WithLogLevel set log level
func WithLogLevel(level string) Option {
return func(c *Config) {
c.Log.LogLevel = level
}
}

// WithLogPath set log path
func WithLogPath(path string) Option {
return func(c *Config) {
c.Log.LogPath = path
}
}
42 changes: 42 additions & 0 deletions inlong-tubemq/tubemq-client-twins/tubemq-client-go/log/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -39,3 +44,40 @@ 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 := isValidLogPath(path)
if err != nil {
return err
}
defaultConfig.LogPath = path
return nil
}
func isValidLogPath(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
}

0 comments on commit aecdf8d

Please sign in to comment.