From d83b7432d456310758929ab13ca520ed2df447a5 Mon Sep 17 00:00:00 2001 From: ThinkChaos Date: Sun, 14 Jan 2024 15:43:34 -0500 Subject: [PATCH] refactor(log): use `logrus.Level` directly and document `trace` level --- config/config.go | 2 +- config/config_test.go | 6 +-- docs/config.yml | 2 +- docs/configuration.md | 12 +++--- log/logger.go | 24 +++-------- log/logger_enum.go | 92 ------------------------------------------- 6 files changed, 16 insertions(+), 122 deletions(-) diff --git a/config/config.go b/config/config.go index 95b542e1..f3d4031a 100644 --- a/config/config.go +++ b/config/config.go @@ -240,7 +240,7 @@ type Config struct { Upstream *UpstreamGroups `yaml:"upstream"` UpstreamTimeout *Duration `yaml:"upstreamTimeout"` DisableIPv6 *bool `yaml:"disableIPv6"` - LogLevel *log.Level `yaml:"logLevel"` + LogLevel *logrus.Level `yaml:"logLevel"` LogFormat *log.FormatType `yaml:"logFormat"` LogPrivacy *bool `yaml:"logPrivacy"` LogTimestamp *bool `yaml:"logTimestamp"` diff --git a/config/config_test.go b/config/config_test.go index d652b71e..4d963611 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -69,10 +69,10 @@ var _ = Describe("Config", func() { When("parameter 'logLevel' is set", func() { It("should convert to log.level", func() { - c.Deprecated.LogLevel = ptrOf(log.LevelDebug) + c.Deprecated.LogLevel = ptrOf(logrus.DebugLevel) c.migrate(logger) Expect(hook.Messages).Should(ContainElement(ContainSubstring("log.level"))) - Expect(c.Log.Level).Should(Equal(log.LevelDebug)) + Expect(c.Log.Level).Should(Equal(logrus.DebugLevel)) }) }) @@ -434,7 +434,7 @@ bootstrapDns: c, err = LoadConfig(tmpDir.JoinPath("config.yml"), false) Expect(err).Should(Succeed()) - Expect(c.Log.Level).Should(Equal(log.LevelInfo)) + Expect(c.Log.Level).Should(Equal(logrus.InfoLevel)) }) }) }) diff --git a/docs/config.yml b/docs/config.yml index 8b583d48..a60dd399 100644 --- a/docs/config.yml +++ b/docs/config.yml @@ -316,7 +316,7 @@ ports: # optional: logging configuration log: - # optional: Log level (one from debug, info, warn, error). Default: info + # optional: Log level (one from trace, debug, info, warn, error). Default: info level: info # optional: Log format (text or json). Default: text format: text diff --git a/docs/configuration.md b/docs/configuration.md index a84b3593..ff48f13a 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -49,12 +49,12 @@ All logging port are optional. All logging options are optional. -| Parameter | Type | Default value | Description | -| ------------- | ------------------------------- | ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------ | -| log.level | enum (debug, info, warn, error) | info | Log level | -| log.format | enum (text, json) | text | Log format (text or json). | -| log.timestamp | bool | true | Log time stamps (true or false). | -| log.privacy | bool | false | Obfuscate log output (replace all alphanumeric characters with *) for user sensitive data like request domains or responses to increase privacy. | +| Parameter | Type | Default value | Description | +| ------------- | -------------------------------------- | ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------ | +| log.level | enum (trace, debug, info, warn, error) | info | Log level | +| log.format | enum (text, json) | text | Log format (text or json). | +| log.timestamp | bool | true | Log timestamps (true or false). | +| log.privacy | bool | false | Obfuscate log output (replace all alphanumeric characters with *) for user sensitive data like request domains or responses to increase privacy. | !!! example diff --git a/log/logger.go b/log/logger.go index c8af0faa..09414025 100644 --- a/log/logger.go +++ b/log/logger.go @@ -32,22 +32,12 @@ var ( // ) type FormatType int -// Level log level ENUM( -// info -// trace -// debug -// warn -// error -// fatal -// ) -type Level int - // Config defines all logging configurations type Config struct { - Level Level `yaml:"level" default:"info"` - Format FormatType `yaml:"format" default:"text"` - Privacy bool `yaml:"privacy" default:"false"` - Timestamp bool `yaml:"timestamp" default:"true"` + Level logrus.Level `yaml:"level" default:"info"` + Format FormatType `yaml:"format" default:"text"` + Privacy bool `yaml:"privacy" default:"false"` + Timestamp bool `yaml:"timestamp" default:"true"` } // DefaultConfig returns a new Config initialized with default values. @@ -106,11 +96,7 @@ func Configure(cfg *Config) { // Configure applies configuration to the given logger. func ConfigureLogger(logger *logrus.Logger, cfg *Config) { - if level, err := logrus.ParseLevel(cfg.Level.String()); err != nil { - logger.Fatalf("invalid log level %s %v", cfg.Level, err) - } else { - logger.SetLevel(level) - } + logger.SetLevel(cfg.Level) switch cfg.Format { case FormatTypeText: diff --git a/log/logger_enum.go b/log/logger_enum.go index d4bc69c2..2f652212 100644 --- a/log/logger_enum.go +++ b/log/logger_enum.go @@ -84,95 +84,3 @@ func (x *FormatType) UnmarshalText(text []byte) error { *x = tmp return nil } - -const ( - // LevelInfo is a Level of type Info. - LevelInfo Level = iota - // LevelTrace is a Level of type Trace. - LevelTrace - // LevelDebug is a Level of type Debug. - LevelDebug - // LevelWarn is a Level of type Warn. - LevelWarn - // LevelError is a Level of type Error. - LevelError - // LevelFatal is a Level of type Fatal. - LevelFatal -) - -var ErrInvalidLevel = fmt.Errorf("not a valid Level, try [%s]", strings.Join(_LevelNames, ", ")) - -const _LevelName = "infotracedebugwarnerrorfatal" - -var _LevelNames = []string{ - _LevelName[0:4], - _LevelName[4:9], - _LevelName[9:14], - _LevelName[14:18], - _LevelName[18:23], - _LevelName[23:28], -} - -// LevelNames returns a list of possible string values of Level. -func LevelNames() []string { - tmp := make([]string, len(_LevelNames)) - copy(tmp, _LevelNames) - return tmp -} - -var _LevelMap = map[Level]string{ - LevelInfo: _LevelName[0:4], - LevelTrace: _LevelName[4:9], - LevelDebug: _LevelName[9:14], - LevelWarn: _LevelName[14:18], - LevelError: _LevelName[18:23], - LevelFatal: _LevelName[23:28], -} - -// String implements the Stringer interface. -func (x Level) String() string { - if str, ok := _LevelMap[x]; ok { - return str - } - return fmt.Sprintf("Level(%d)", x) -} - -// IsValid provides a quick way to determine if the typed value is -// part of the allowed enumerated values -func (x Level) IsValid() bool { - _, ok := _LevelMap[x] - return ok -} - -var _LevelValue = map[string]Level{ - _LevelName[0:4]: LevelInfo, - _LevelName[4:9]: LevelTrace, - _LevelName[9:14]: LevelDebug, - _LevelName[14:18]: LevelWarn, - _LevelName[18:23]: LevelError, - _LevelName[23:28]: LevelFatal, -} - -// ParseLevel attempts to convert a string to a Level. -func ParseLevel(name string) (Level, error) { - if x, ok := _LevelValue[name]; ok { - return x, nil - } - return Level(0), fmt.Errorf("%s is %w", name, ErrInvalidLevel) -} - -// MarshalText implements the text marshaller method. -func (x Level) MarshalText() ([]byte, error) { - return []byte(x.String()), nil -} - -// UnmarshalText implements the text unmarshaller method. -func (x *Level) UnmarshalText(text []byte) error { - name := string(text) - tmp, err := ParseLevel(name) - if err != nil { - return err - } - *x = tmp - return nil -}