- 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:
@@ -1,3 +1,49 @@
|
||||
# v2.2.0
|
||||
|
||||
This release adds new public APIs for working with log levels, introduces HTTP method-specific log levels, and improves package documentation.
|
||||
|
||||
### Added
|
||||
|
||||
- Added dedicated log level constructors:
|
||||
- `NewInfoLogLevel`
|
||||
- `NewInfoLogLevelWithColors`
|
||||
- `NewWarnLogLevel`
|
||||
- `NewWarnLogLevelWithColors`
|
||||
- `NewErrorLogLevel`
|
||||
- `NewErrorLogLevelWithColors`
|
||||
- `NewFatalLogLevel`
|
||||
- `NewFatalLogLevelWithColors`
|
||||
- `NewDebugLogLevel`
|
||||
- `NewDebugLogLevelWithColors`
|
||||
- Added HTTP method-specific predefined log levels:
|
||||
- `HTTPGetLevel`
|
||||
- `HTTPHeadLevel`
|
||||
- `HTTPPostLevel`
|
||||
- `HTTPPutLevel`
|
||||
- `HTTPPatchLevel`
|
||||
- `HTTPDeleteLevel`
|
||||
- `HTTPOptionsLevel`
|
||||
- `HTTPConnectLevel`
|
||||
- `HTTPTraceLevel`
|
||||
- `HTTPUnknownLevel`
|
||||
- Added `LogLevelForMethod(method string) LogLevel` to map HTTP methods to predefined log levels.
|
||||
- Added `Logger.Printf(level, format, args...)` for formatted logging with an explicit log level.
|
||||
|
||||
### Changed
|
||||
|
||||
- Refactored log level definitions into a dedicated `levels.go` file.
|
||||
- Unified `Logger.Print` and `Logger.Println` through a shared internal implementation without changing their behavior.
|
||||
|
||||
### Documentation
|
||||
|
||||
- Improved GoDoc coverage for exported types, constructors, formatters, colors, HTTP helpers, and time layouts.
|
||||
- Expanded inline documentation for formatter tokens and default formatter behavior.
|
||||
|
||||
### Compatibility
|
||||
|
||||
- No breaking API changes.
|
||||
- Existing `Print`, `Println`, predefined levels, and color APIs remain compatible.
|
||||
|
||||
# v2.1.0
|
||||
|
||||
This release expands sneklog color customization while preserving compatibility with the v2.0.1 API.
|
||||
|
||||
@@ -5,12 +5,25 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// FgColor is a basic ANSI foreground color code.
|
||||
type FgColor uint8
|
||||
|
||||
// BgColor is a basic ANSI background color code.
|
||||
type BgColor uint8
|
||||
|
||||
// FgColor256 is an ANSI 256-color foreground value.
|
||||
type FgColor256 uint8
|
||||
|
||||
// BgColor256 is an ANSI 256-color background value.
|
||||
type BgColor256 uint8
|
||||
|
||||
// BgColorRGB is an ANSI truecolor background value encoded as RGB bytes.
|
||||
type BgColorRGB []uint8
|
||||
|
||||
// FgColorRGB is an ANSI truecolor foreground value encoded as RGB bytes.
|
||||
type FgColorRGB []uint8
|
||||
|
||||
// Attribute is an ANSI text attribute code.
|
||||
type Attribute uint8
|
||||
|
||||
// NewBgColorRGB builds an RGB background color value.
|
||||
@@ -23,6 +36,7 @@ func NewFgColorRGB(r, g, b uint8) FgColorRGB {
|
||||
return FgColorRGB{r, g, b}
|
||||
}
|
||||
|
||||
// Text attribute codes.
|
||||
const (
|
||||
Reset Attribute = iota
|
||||
Bold
|
||||
@@ -35,6 +49,8 @@ const (
|
||||
Conceal
|
||||
CrossedOut
|
||||
)
|
||||
|
||||
// Additional text attribute codes.
|
||||
const (
|
||||
DoubleUnderline Attribute = iota + 21 // Can be disable bold on some terminals
|
||||
DisableBold
|
||||
@@ -45,6 +61,8 @@ const (
|
||||
DisableConceal
|
||||
DisableCrossedOut
|
||||
)
|
||||
|
||||
// Basic foreground color codes.
|
||||
const (
|
||||
FgNone = 0
|
||||
FgBlack FgColor = iota + 30
|
||||
@@ -57,6 +75,8 @@ const (
|
||||
FgWhite
|
||||
FgDefault FgColor = 39
|
||||
)
|
||||
|
||||
// Basic background color codes.
|
||||
const (
|
||||
BgNone = 0
|
||||
BgBlack BgColor = iota + 40
|
||||
@@ -69,6 +89,8 @@ const (
|
||||
BgWhite
|
||||
BgDefault BgColor = 49
|
||||
)
|
||||
|
||||
// High-intensity foreground color codes.
|
||||
const (
|
||||
FgHiBlack FgColor = iota + 90
|
||||
FgHiRed
|
||||
@@ -79,6 +101,8 @@ const (
|
||||
FgHiCyan
|
||||
FgHiWhite
|
||||
)
|
||||
|
||||
// High-intensity background color codes.
|
||||
const (
|
||||
BgHiBlack BgColor = iota + 100
|
||||
BgHiRed
|
||||
@@ -90,7 +114,7 @@ const (
|
||||
BgHiWhite
|
||||
)
|
||||
|
||||
// This attributes rarely used and not supported by all terminals.
|
||||
// Rarely used text attribute codes that are not supported by all terminals.
|
||||
const (
|
||||
Border Attribute = iota + 51
|
||||
Outline
|
||||
@@ -99,6 +123,7 @@ const (
|
||||
DisableUpperline
|
||||
)
|
||||
|
||||
// ColorStringBuilder incrementally builds ANSI-colored strings.
|
||||
type ColorStringBuilder struct {
|
||||
str string
|
||||
}
|
||||
|
||||
+23
-8
@@ -6,16 +6,28 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// Formatter
|
||||
// Format: %t - time, %N - name, %l - level, %L - level uppercase, %b - traceback, %B - full traceback, %m - message,
|
||||
// %M - method, %f - filename, %n - line number, %s - method signature, %p - full path
|
||||
// Example: "%t [%l] %N: %m (%f:%n %s)"
|
||||
// Formatter configures how log records are rendered.
|
||||
//
|
||||
// TimeStampFormat: %Y - year, %m - month, %d - day, %H - hour, %M - minute, %S - second,
|
||||
// %z - timezone(i.e. 0300), %Z - timezone(i.e. 03:00)
|
||||
// Example: "%d.%m.%Y %H:%M:%S"
|
||||
// Format tokens:
|
||||
// - %t: time
|
||||
// - %N: name
|
||||
// - %l: level
|
||||
// - %L: uppercase level
|
||||
// - %b: first traceback frame
|
||||
// - %B: full traceback
|
||||
// - %m: message
|
||||
// - %M: method
|
||||
// - %f: filename
|
||||
// - %n: line number
|
||||
// - %s: method signature
|
||||
// - %p: full path
|
||||
//
|
||||
// TraceBackFormat: %M - method, %f - filename, %n - line number, %s - method signature, %p - full path
|
||||
// Example format: "%t [%l] %N: %m (%f:%n %s)"
|
||||
//
|
||||
// TimeStampFormat uses strftime-like directives such as %Y, %m, %d, %H, %M,
|
||||
// %S, %z, and %Z.
|
||||
//
|
||||
// TraceBackFormat supports the traceback-related tokens %M, %f, %n, %s, and %p.
|
||||
type Formatter struct {
|
||||
Format string
|
||||
MessageSeparator string
|
||||
@@ -31,6 +43,7 @@ type Formatter struct {
|
||||
ColorOnlyStdout bool
|
||||
}
|
||||
|
||||
// DefaultTextFormatter is the default formatter used by text writers.
|
||||
var DefaultTextFormatter = &Formatter{
|
||||
Format: "%t %l %N: %m",
|
||||
MessageSeparator: " ",
|
||||
@@ -40,6 +53,8 @@ var DefaultTextFormatter = &Formatter{
|
||||
ColorOutput: true,
|
||||
ColorOnlyStdout: true,
|
||||
}
|
||||
|
||||
// DefaultJsonFormatter is the default formatter used by JSON writers.
|
||||
var DefaultJsonFormatter = &Formatter{
|
||||
Format: "%m",
|
||||
MessageSeparator: " ",
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
package sneklog
|
||||
|
||||
import "net/http"
|
||||
|
||||
// HTTP method-specific log levels.
|
||||
var (
|
||||
HTTPGetLevel = NewInfoLogLevelWithColors("get", FgGreen, BgNone)
|
||||
HTTPHeadLevel = NewInfoLogLevelWithColors("head", FgCyan, BgNone)
|
||||
HTTPPostLevel = NewInfoLogLevelWithColors("post", FgBlue, BgNone)
|
||||
HTTPPutLevel = NewInfoLogLevelWithColors("put", FgYellow, BgNone)
|
||||
HTTPPatchLevel = NewInfoLogLevelWithColors("patch", FgMagenta, BgNone)
|
||||
HTTPDeleteLevel = NewInfoLogLevelWithColors("delete", FgRed, BgNone)
|
||||
)
|
||||
|
||||
// Additional HTTP method-specific log levels.
|
||||
var (
|
||||
HTTPOptionsLevel = NewDebugLogLevelWithColors("options", FgWhite, BgNone)
|
||||
HTTPConnectLevel = NewDebugLogLevelWithColors("connect", FgCyan, BgNone)
|
||||
HTTPTraceLevel = NewDebugLogLevelWithColors("trace", FgWhite, BgNone)
|
||||
HTTPUnknownLevel = NewDebugLogLevelWithColors("http", FgWhite, BgNone)
|
||||
)
|
||||
|
||||
// LogLevelForMethod returns the predefined log level associated with an HTTP method.
|
||||
func LogLevelForMethod(method string) LogLevel {
|
||||
switch method {
|
||||
case http.MethodGet:
|
||||
return HTTPGetLevel
|
||||
case http.MethodHead:
|
||||
return HTTPHeadLevel
|
||||
case http.MethodPost:
|
||||
return HTTPPostLevel
|
||||
case http.MethodPut:
|
||||
return HTTPPutLevel
|
||||
case http.MethodPatch:
|
||||
return HTTPPatchLevel
|
||||
case http.MethodDelete:
|
||||
return HTTPDeleteLevel
|
||||
case http.MethodOptions:
|
||||
return HTTPOptionsLevel
|
||||
case http.MethodConnect:
|
||||
return HTTPConnectLevel
|
||||
case http.MethodTrace:
|
||||
return HTTPTraceLevel
|
||||
default:
|
||||
return HTTPUnknownLevel
|
||||
}
|
||||
}
|
||||
@@ -83,9 +83,7 @@ func (l *Logger) Debugln(m ...any) {
|
||||
l.Println(DEBUG, m...)
|
||||
}
|
||||
|
||||
// Print write message without trailing "\n"
|
||||
// Good for database
|
||||
func (l *Logger) Print(level LogLevel, m ...any) {
|
||||
func (l *Logger) print(level LogLevel, newline bool, m ...any) {
|
||||
if l.level.n < level.n {
|
||||
return
|
||||
}
|
||||
@@ -94,30 +92,10 @@ func (l *Logger) Print(level LogLevel, m ...any) {
|
||||
}
|
||||
|
||||
tb := getFullTraceback(1)
|
||||
for _, writer := range l.writers {
|
||||
if writer == nil {
|
||||
continue
|
||||
messages := l.replaceAll(m...)
|
||||
if newline {
|
||||
messages = append(messages, any("\n"))
|
||||
}
|
||||
err := writer.Print(level, l.prefix, tb, l.replaceAll(m...)...)
|
||||
if err != nil {
|
||||
l.reportWriterError(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Println
|
||||
// Docker requires "\n" at end to write to log.
|
||||
// print not work for docker, otherwise it will work and write into stdout
|
||||
func (l *Logger) Println(level LogLevel, m ...any) {
|
||||
if l.level.n < level.n {
|
||||
return
|
||||
}
|
||||
if len(l.writers) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
tb := getFullTraceback(1)
|
||||
messages := append(append(make([]any, 0, len(m)+1), l.replaceAll(m...)...), "\n")
|
||||
for _, writer := range l.writers {
|
||||
if writer == nil {
|
||||
continue
|
||||
@@ -129,6 +107,17 @@ func (l *Logger) Println(level LogLevel, m ...any) {
|
||||
}
|
||||
}
|
||||
|
||||
// Print logs a message without appending a trailing newline semantic.
|
||||
func (l *Logger) Print(level LogLevel, m ...any) { l.print(level, false, m...) }
|
||||
|
||||
// Println logs a message and appends a trailing newline semantic for writers that need it.
|
||||
func (l *Logger) Println(level LogLevel, m ...any) { l.print(level, true, m...) }
|
||||
|
||||
// Printf formats according to a format specifier and logs the resulting message.
|
||||
func (l *Logger) Printf(level LogLevel, format string, args ...any) {
|
||||
l.print(level, false, fmt.Sprintf(format, args...))
|
||||
}
|
||||
|
||||
// reportWriterError writes internal writer failures directly to stderr to avoid
|
||||
// re-entering the same logger path that just failed.
|
||||
func (l *Logger) reportWriterError(err error) {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -7,154 +7,6 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// 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 LogLevel{n: index, t: name, attrs: []Attribute{}}
|
||||
}
|
||||
|
||||
// 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{}}
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// MethodTraceback describes a single stack frame attached to a log entry.
|
||||
type MethodTraceback struct {
|
||||
Method string `json:"method"`
|
||||
@@ -164,15 +16,6 @@ type MethodTraceback struct {
|
||||
FullPath string `json:"fullPath"`
|
||||
}
|
||||
|
||||
// Predefined log levels.
|
||||
var (
|
||||
INFO = NewLogLevelWithColors(0, "info", FgWhite, BgNone)
|
||||
WARN = NewLogLevelWithColors(1, "warn", FgHiYellow, BgNone)
|
||||
ERROR = NewLogLevelWithColors(2, "error", FgHiRed, BgNone)
|
||||
FATAL = NewLogLevelWithColors(3, "fatal", FgRed, BgNone)
|
||||
DEBUG = NewLogLevelWithColors(4, "debug", FgGreen, BgNone)
|
||||
)
|
||||
|
||||
// Logger routes log records to one or more configured writers.
|
||||
type Logger struct {
|
||||
prefix string
|
||||
|
||||
@@ -2,6 +2,7 @@ package sneklog
|
||||
|
||||
import "strings"
|
||||
|
||||
// Predefined strftime-like time layouts.
|
||||
const (
|
||||
Layout = "%m/%d %I:%M:%S%p '%y %z"
|
||||
ANSIC = "%a %b %e %H:%M:%S %Y"
|
||||
|
||||
Reference in New Issue
Block a user