(new): add log level helpers and HTTP method levels
Golang lint / lint (push) Successful in 42s

- move LogLevel and predefined levels into levels.go
- add level-specific constructors for info/warn/error/fatal/debug
- add predefined HTTP method log levels and LogLevelForMethod
- unify Logger Print/Println internals and add Printf
- improve GoDoc coverage for exported APIs
This commit is contained in:
2026-04-27 16:36:30 +03:00
parent 3eac1851ec
commit 812caed471
8 changed files with 356 additions and 192 deletions
+198
View File
@@ -0,0 +1,198 @@
package sneklog
// Predefined log levels.
var (
INFO = NewInfoLogLevelWithColors("info", FgWhite, BgNone)
WARN = NewWarnLogLevelWithColors("warn", FgHiYellow, BgNone)
ERROR = NewErrorLogLevelWithColors("error", FgHiRed, BgNone)
FATAL = NewFatalLogLevelWithColors("fatal", FgRed, BgNone)
DEBUG = NewDebugLogLevelWithColors("debug", FgGreen, BgNone)
)
// LogLevel describes a logging severity.
type LogLevel struct {
n uint8
t string
fg FgColor
fg256 FgColor256
fgRgb FgColorRGB
bg BgColor
bg256 BgColor256
bgRgb BgColorRGB
attrs []Attribute
}
// NewLogLevel creates a log level without predefined colors.
func NewLogLevel(index uint8, name string) LogLevel {
return NewLogLevelWithColors(index, name, 0, 0)
}
// NewLogLevelWithColors creates a log level with ANSI foreground and background colors.
func NewLogLevelWithColors(index uint8, name string, fg FgColor, bg BgColor) LogLevel {
return LogLevel{n: index, 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) }
// 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)
}
// NewWarnLogLevel creates a warn-level log level without predefined colors.
func NewWarnLogLevel(name string) LogLevel { return NewLogLevel(1, 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)
}
// NewErrorLogLevel creates an error-level log level without predefined colors.
func NewErrorLogLevel(name string) LogLevel { return NewLogLevel(2, 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)
}
// NewFatalLogLevel creates a fatal-level log level without predefined colors.
func NewFatalLogLevel(name string) LogLevel { return NewLogLevel(3, 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)
}
// NewDebugLogLevel creates a debug-level log level without predefined colors.
func NewDebugLogLevel(name string) LogLevel { return NewLogLevel(4, 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)
}
// GetName returns the lowercase textual representation of the level.
func (l *LogLevel) GetName() string { return l.t }
// SetFgColor sets the ANSI foreground color and clears other foreground color modes.
// Deprecated: use SetForegroundColor. This method will be removed in v3.
func (l *LogLevel) SetFgColor(color FgColor) *LogLevel {
return l.SetForegroundColor(color)
}
// GetFgColor returns the ANSI foreground color for the level.
// Deprecated: use GetForegroundColor. This method will be removed in v3.
func (l *LogLevel) GetFgColor() FgColor { return l.GetForegroundColor() }
// SetForegroundColor sets the ANSI foreground color and clears other foreground color modes.
func (l *LogLevel) SetForegroundColor(color FgColor) *LogLevel {
l.fg = color
l.fg256 = 0
l.fgRgb = nil
return l
}
// GetForegroundColor returns the ANSI foreground color for the level.
func (l *LogLevel) GetForegroundColor() FgColor { return l.fg }
// SetForeground256Color sets the 256-color foreground and clears other foreground color modes.
func (l *LogLevel) SetForeground256Color(color FgColor256) *LogLevel {
l.fg = 0
l.fg256 = color
l.fgRgb = nil
return l
}
// GetForeground256Color returns the configured 256-color foreground value.
func (l *LogLevel) GetForeground256Color() FgColor256 { return l.fg256 }
// SetForegroundRGB sets the RGB foreground color and clears other foreground color modes.
func (l *LogLevel) SetForegroundRGB(r, g, b uint8) *LogLevel {
l.fg = 0
l.fg256 = 0
l.fgRgb = FgColorRGB{r, g, b}
return l
}
// GetForegroundRGB returns the configured RGB foreground value.
func (l *LogLevel) GetForegroundRGB() FgColorRGB { return l.fgRgb }
// SetBgColor sets the ANSI background color and clears other background color modes.
// Deprecated: use SetBackgroundColor. This method will be removed in v3.
func (l *LogLevel) SetBgColor(color BgColor) *LogLevel {
return l.SetBackgroundColor(color)
}
// GetBgColor returns the ANSI background color for the level.
// Deprecated: use GetBackgroundColor. This method will be removed in v3.
func (l *LogLevel) GetBgColor() BgColor { return l.GetBackgroundColor() }
// SetBackgroundColor sets the ANSI background color and clears other background color modes.
func (l *LogLevel) SetBackgroundColor(color BgColor) *LogLevel {
l.bg = color
l.bg256 = 0
l.bgRgb = nil
return l
}
// GetBackgroundColor returns the ANSI background color for the level.
func (l *LogLevel) GetBackgroundColor() BgColor { return l.bg }
// SetBackground256Color sets the 256-color background and clears other background color modes.
func (l *LogLevel) SetBackground256Color(color BgColor256) *LogLevel {
l.bg = 0
l.bg256 = color
l.bgRgb = nil
return l
}
// GetBackground256Color returns the configured 256-color background value.
func (l *LogLevel) GetBackground256Color() BgColor256 { return l.bg256 }
// SetBackgroundRGB sets the RGB background color and clears other background color modes.
func (l *LogLevel) SetBackgroundRGB(r, g, b uint8) *LogLevel {
l.bg = 0
l.bg256 = 0
l.bgRgb = BgColorRGB{r, g, b}
return l
}
// GetBackgroundRGB returns the configured RGB background value.
func (l *LogLevel) GetBackgroundRGB() BgColorRGB { return l.bgRgb }
// AddAttribute appends an ANSI text attribute to the level.
func (l *LogLevel) AddAttribute(a Attribute) *LogLevel {
l.attrs = append(l.attrs, a)
return l
}
// RemoveAttribute removes all matching ANSI text attributes from the level.
func (l *LogLevel) RemoveAttribute(a Attribute) *LogLevel {
attrs := make([]Attribute, 0)
for _, attr := range l.attrs {
if attr != a {
attrs = append(attrs, attr)
}
}
l.attrs = attrs
return l
}
// SetAttributes replaces the level attributes with a copy of the provided slice.
func (l *LogLevel) SetAttributes(a []Attribute) *LogLevel {
attrs := make([]Attribute, len(a))
copy(attrs, a)
l.attrs = attrs
return l
}
// GetAttributes returns a copy of the configured ANSI text attributes.
func (l *LogLevel) GetAttributes() []Attribute {
attrs := make([]Attribute, len(l.attrs))
copy(attrs, l.attrs)
return attrs
}