mirror of https://github.com/0xERR0R/blocky.git
refactor(log): use `logrus.Level` directly and document `trace` level
This commit is contained in:
parent
9d50941e2f
commit
d83b7432d4
|
@ -240,7 +240,7 @@ type Config struct {
|
||||||
Upstream *UpstreamGroups `yaml:"upstream"`
|
Upstream *UpstreamGroups `yaml:"upstream"`
|
||||||
UpstreamTimeout *Duration `yaml:"upstreamTimeout"`
|
UpstreamTimeout *Duration `yaml:"upstreamTimeout"`
|
||||||
DisableIPv6 *bool `yaml:"disableIPv6"`
|
DisableIPv6 *bool `yaml:"disableIPv6"`
|
||||||
LogLevel *log.Level `yaml:"logLevel"`
|
LogLevel *logrus.Level `yaml:"logLevel"`
|
||||||
LogFormat *log.FormatType `yaml:"logFormat"`
|
LogFormat *log.FormatType `yaml:"logFormat"`
|
||||||
LogPrivacy *bool `yaml:"logPrivacy"`
|
LogPrivacy *bool `yaml:"logPrivacy"`
|
||||||
LogTimestamp *bool `yaml:"logTimestamp"`
|
LogTimestamp *bool `yaml:"logTimestamp"`
|
||||||
|
|
|
@ -69,10 +69,10 @@ var _ = Describe("Config", func() {
|
||||||
|
|
||||||
When("parameter 'logLevel' is set", func() {
|
When("parameter 'logLevel' is set", func() {
|
||||||
It("should convert to log.level", func() {
|
It("should convert to log.level", func() {
|
||||||
c.Deprecated.LogLevel = ptrOf(log.LevelDebug)
|
c.Deprecated.LogLevel = ptrOf(logrus.DebugLevel)
|
||||||
c.migrate(logger)
|
c.migrate(logger)
|
||||||
Expect(hook.Messages).Should(ContainElement(ContainSubstring("log.level")))
|
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)
|
c, err = LoadConfig(tmpDir.JoinPath("config.yml"), false)
|
||||||
|
|
||||||
Expect(err).Should(Succeed())
|
Expect(err).Should(Succeed())
|
||||||
Expect(c.Log.Level).Should(Equal(log.LevelInfo))
|
Expect(c.Log.Level).Should(Equal(logrus.InfoLevel))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -316,7 +316,7 @@ ports:
|
||||||
|
|
||||||
# optional: logging configuration
|
# optional: logging configuration
|
||||||
log:
|
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
|
level: info
|
||||||
# optional: Log format (text or json). Default: text
|
# optional: Log format (text or json). Default: text
|
||||||
format: text
|
format: text
|
||||||
|
|
|
@ -49,12 +49,12 @@ All logging port are optional.
|
||||||
|
|
||||||
All logging options are optional.
|
All logging options are optional.
|
||||||
|
|
||||||
| Parameter | Type | Default value | Description |
|
| Parameter | Type | Default value | Description |
|
||||||
| ------------- | ------------------------------- | ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------ |
|
| ------------- | -------------------------------------- | ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||||
| log.level | enum (debug, info, warn, error) | info | Log level |
|
| log.level | enum (trace, debug, info, warn, error) | info | Log level |
|
||||||
| log.format | enum (text, json) | text | Log format (text or json). |
|
| log.format | enum (text, json) | text | Log format (text or json). |
|
||||||
| log.timestamp | bool | true | Log time stamps (true or false). |
|
| 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. |
|
| 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
|
!!! example
|
||||||
|
|
||||||
|
|
|
@ -32,22 +32,12 @@ var (
|
||||||
// )
|
// )
|
||||||
type FormatType int
|
type FormatType int
|
||||||
|
|
||||||
// Level log level ENUM(
|
|
||||||
// info
|
|
||||||
// trace
|
|
||||||
// debug
|
|
||||||
// warn
|
|
||||||
// error
|
|
||||||
// fatal
|
|
||||||
// )
|
|
||||||
type Level int
|
|
||||||
|
|
||||||
// Config defines all logging configurations
|
// Config defines all logging configurations
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Level Level `yaml:"level" default:"info"`
|
Level logrus.Level `yaml:"level" default:"info"`
|
||||||
Format FormatType `yaml:"format" default:"text"`
|
Format FormatType `yaml:"format" default:"text"`
|
||||||
Privacy bool `yaml:"privacy" default:"false"`
|
Privacy bool `yaml:"privacy" default:"false"`
|
||||||
Timestamp bool `yaml:"timestamp" default:"true"`
|
Timestamp bool `yaml:"timestamp" default:"true"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// DefaultConfig returns a new Config initialized with default values.
|
// DefaultConfig returns a new Config initialized with default values.
|
||||||
|
@ -106,11 +96,7 @@ func Configure(cfg *Config) {
|
||||||
|
|
||||||
// Configure applies configuration to the given logger.
|
// Configure applies configuration to the given logger.
|
||||||
func ConfigureLogger(logger *logrus.Logger, cfg *Config) {
|
func ConfigureLogger(logger *logrus.Logger, cfg *Config) {
|
||||||
if level, err := logrus.ParseLevel(cfg.Level.String()); err != nil {
|
logger.SetLevel(cfg.Level)
|
||||||
logger.Fatalf("invalid log level %s %v", cfg.Level, err)
|
|
||||||
} else {
|
|
||||||
logger.SetLevel(level)
|
|
||||||
}
|
|
||||||
|
|
||||||
switch cfg.Format {
|
switch cfg.Format {
|
||||||
case FormatTypeText:
|
case FormatTypeText:
|
||||||
|
|
|
@ -84,95 +84,3 @@ func (x *FormatType) UnmarshalText(text []byte) error {
|
||||||
*x = tmp
|
*x = tmp
|
||||||
return nil
|
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
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue