(new): add threshold-based log level filtering and docs
Golang lint / lint (push) Successful in 1m36s
Golang lint / lint (push) Successful in 1m36s
- add threshold-aware LogLevel constructors - add Logger.SetThresholdMode and SameThreshold - preserve legacy SameLevel behavior for compatibility - extend tests for threshold mode and compatibility - update README and release notes for v2.2.0
This commit is contained in:
@@ -11,8 +11,9 @@ var (
|
||||
|
||||
// LogLevel describes a logging severity.
|
||||
type LogLevel struct {
|
||||
n uint8
|
||||
t string
|
||||
n uint8
|
||||
th uint8
|
||||
t string
|
||||
|
||||
fg FgColor
|
||||
fg256 FgColor256
|
||||
@@ -26,53 +27,68 @@ type LogLevel struct {
|
||||
}
|
||||
|
||||
// NewLogLevel creates a log level without predefined colors.
|
||||
// Deprecated: use NewThresholdLogLevel. This function will be removed in v3.
|
||||
func NewLogLevel(index uint8, name string) LogLevel {
|
||||
return NewLogLevelWithColors(index, name, 0, 0)
|
||||
}
|
||||
|
||||
// NewLogLevelWithColors creates a log level with ANSI foreground and background colors.
|
||||
// Deprecated: use NewThresholdLogLevelWithColors. This function will be removed in v3.
|
||||
func NewLogLevelWithColors(index uint8, name string, fg FgColor, bg BgColor) LogLevel {
|
||||
return LogLevel{n: index, t: name, fg: fg, bg: bg, attrs: []Attribute{}}
|
||||
}
|
||||
|
||||
// NewThresholdLogLevel creates a log level with both a legacy severity index and
|
||||
// a threshold value used when Logger.SetThresholdMode(true) is enabled.
|
||||
func NewThresholdLogLevel(index, th uint8, name string) LogLevel {
|
||||
return NewThresholdLogLevelWithColors(index, th, name, FgNone, BgNone)
|
||||
}
|
||||
|
||||
// NewThresholdLogLevelWithColors creates a log level with both a legacy
|
||||
// severity index and a threshold value, plus ANSI foreground and background
|
||||
// colors.
|
||||
func NewThresholdLogLevelWithColors(index, th uint8, name string, fg FgColor, bg BgColor) LogLevel {
|
||||
return LogLevel{n: index, th: th, t: name, fg: fg, bg: bg, attrs: []Attribute{}}
|
||||
}
|
||||
|
||||
// NewInfoLogLevel creates an info-level log level without predefined colors.
|
||||
func NewInfoLogLevel(name string) LogLevel { return NewLogLevel(0, name) }
|
||||
func NewInfoLogLevel(name string) LogLevel { return NewThresholdLogLevel(0, 10, name) }
|
||||
|
||||
// NewInfoLogLevelWithColors creates an info-level log level with ANSI colors.
|
||||
func NewInfoLogLevelWithColors(name string, fg FgColor, bg BgColor) LogLevel {
|
||||
return NewLogLevelWithColors(0, name, fg, bg)
|
||||
return NewThresholdLogLevelWithColors(0, 10, name, fg, bg)
|
||||
}
|
||||
|
||||
// NewWarnLogLevel creates a warn-level log level without predefined colors.
|
||||
func NewWarnLogLevel(name string) LogLevel { return NewLogLevel(1, name) }
|
||||
func NewWarnLogLevel(name string) LogLevel { return NewThresholdLogLevel(1, 20, name) }
|
||||
|
||||
// NewWarnLogLevelWithColors creates a warn-level log level with ANSI colors.
|
||||
func NewWarnLogLevelWithColors(name string, fg FgColor, bg BgColor) LogLevel {
|
||||
return NewLogLevelWithColors(1, name, fg, bg)
|
||||
return NewThresholdLogLevelWithColors(1, 20, name, fg, bg)
|
||||
}
|
||||
|
||||
// NewErrorLogLevel creates an error-level log level without predefined colors.
|
||||
func NewErrorLogLevel(name string) LogLevel { return NewLogLevel(2, name) }
|
||||
func NewErrorLogLevel(name string) LogLevel { return NewThresholdLogLevel(2, 30, name) }
|
||||
|
||||
// NewErrorLogLevelWithColors creates an error-level log level with ANSI colors.
|
||||
func NewErrorLogLevelWithColors(name string, fg FgColor, bg BgColor) LogLevel {
|
||||
return NewLogLevelWithColors(2, name, fg, bg)
|
||||
return NewThresholdLogLevelWithColors(2, 30, name, fg, bg)
|
||||
}
|
||||
|
||||
// NewFatalLogLevel creates a fatal-level log level without predefined colors.
|
||||
func NewFatalLogLevel(name string) LogLevel { return NewLogLevel(3, name) }
|
||||
func NewFatalLogLevel(name string) LogLevel { return NewThresholdLogLevel(3, 40, name) }
|
||||
|
||||
// NewFatalLogLevelWithColors creates a fatal-level log level with ANSI colors.
|
||||
func NewFatalLogLevelWithColors(name string, fg FgColor, bg BgColor) LogLevel {
|
||||
return NewLogLevelWithColors(3, name, fg, bg)
|
||||
return NewThresholdLogLevelWithColors(3, 40, name, fg, bg)
|
||||
}
|
||||
|
||||
// NewDebugLogLevel creates a debug-level log level without predefined colors.
|
||||
func NewDebugLogLevel(name string) LogLevel { return NewLogLevel(4, name) }
|
||||
func NewDebugLogLevel(name string) LogLevel { return NewThresholdLogLevel(4, 0, name) }
|
||||
|
||||
// NewDebugLogLevelWithColors creates a debug-level log level with ANSI colors.
|
||||
func NewDebugLogLevelWithColors(name string, fg FgColor, bg BgColor) LogLevel {
|
||||
return NewLogLevelWithColors(4, name, fg, bg)
|
||||
return NewThresholdLogLevelWithColors(4, 0, name, fg, bg)
|
||||
}
|
||||
|
||||
// GetName returns the lowercase textual representation of the level.
|
||||
@@ -198,13 +214,21 @@ func (l *LogLevel) GetAttributes() []Attribute {
|
||||
}
|
||||
|
||||
// SameLevel reports whether two log levels have the same severity index.
|
||||
// Deprecated: use SameThreshold for threshold-only comparisons or Equal for the
|
||||
// full level configuration. SameLevel remains severity-only for backward
|
||||
// compatibility and ignores threshold values.
|
||||
func (l *LogLevel) SameLevel(other LogLevel) bool {
|
||||
return l.n == other.n
|
||||
}
|
||||
|
||||
// SameThreshold reports whether two log levels have the same threshold value.
|
||||
func (l *LogLevel) SameThreshold(other LogLevel) bool {
|
||||
return l.th == other.th
|
||||
}
|
||||
|
||||
// Equal reports whether two log levels have identical configuration.
|
||||
func (l *LogLevel) Equal(other LogLevel) bool {
|
||||
if l.t != other.t || l.n != other.n {
|
||||
if l.t != other.t || l.n != other.n || l.th != other.th {
|
||||
return false
|
||||
}
|
||||
if l.fg != other.fg || l.bg != other.bg {
|
||||
|
||||
Reference in New Issue
Block a user