Documentation
¶
Index ¶
- Constants
- Variables
- func Debug(s ...any)
- func Debugf(format string, args ...any)
- func DefaultTimestamp() string
- func Error(a ...any)
- func Errorf(format string, args ...any)
- func Fatal(a ...any)
- func Fatalf(format string, args ...any)
- func Info(a ...any)
- func Infof(format string, args ...any)
- func Log(level Level, a ...any)
- func LogFunc(level Level, msg func() string)
- func LogIf(level Level, log func())
- func Logf(level Level, format string, args ...any)
- func Print(a ...any)
- func Printf(format string, args ...any)
- func SetDefaultLogger(logger Logger)
- func SetLevel(level Level)
- func Warn(a ...any)
- func Warnf(format string, args ...any)
- type Color
- type Config
- type Entry
- type Fields
- type Format
- type Formatter
- func ConsoleFormatter() Formatter
- func JSONFormatter() Formatter
- func NewConsoleFormatter(cfg Config) Formatter
- func NewFormatter(format Format) Formatter
- func NewFormatterWithConfig(format Format, cfg Config) Formatter
- func NewJsonFormatter(cfg Config) Formatter
- func NewTextFormatter(cfg Config) Formatter
- func TextFormatter() Formatter
- type Level
- type Logger
- func (logger Logger) Copy() Logger
- func (logger Logger) Debug(a ...any)
- func (logger Logger) Debugf(format string, args ...any)
- func (logger Logger) Error(a ...any)
- func (logger Logger) Errorf(format string, args ...any)
- func (logger Logger) Fatal(a ...any)
- func (logger Logger) Fatalf(format string, args ...any)
- func (logger Logger) Info(a ...any)
- func (logger Logger) Infof(format string, args ...any)
- func (logger Logger) Log(level Level, a ...any)
- func (logger Logger) LogFunc(level Level, msg func() string)
- func (logger Logger) LogIf(level Level, log func())
- func (logger Logger) Logf(level Level, format string, args ...any)
- func (logger Logger) Print(a ...any)
- func (logger Logger) Printf(format string, args ...any)
- func (logger Logger) SetLevel(level Level)
- func (logger Logger) Warn(a ...any)
- func (logger Logger) Warnf(format string, args ...any)
- func (logger Logger) With(key string, value any) Logger
- func (logger Logger) WithError(err error) Logger
- func (logger Logger) WithFields(fields Fields) Logger
Constants ¶
const DefaultTimestampFormat = "2006-01-02T15:04:05"
DefaultTimestampFormat defines the layout used for default timestamps.
Variables ¶
var DefaultConfig = Config{ Timestamp: DefaultTimestamp, }
DefaultConfig is the fallback configuration using the DefaultTimestamp function.
var FormatNames = map[Format]string{ FormatJSON: "JSON", FormatText: "TEXT", FormatConsole: "CONSOLE", }
FormatNames maps Format values to their string identifiers.
var Formats = []Format{ FormatJSON, FormatText, FormatConsole, }
Formats is the list of all supported output formats.
var LevelColors map[Level]Color
LevelColors maps Level values to ANSI color codes for console output. This can be customized.
var LevelNames map[Level]string
LevelNames maps Level values to human-readable strings. This can be overridden by the user.
Functions ¶
func DefaultTimestamp ¶
func DefaultTimestamp() string
DefaultTimestamp returns the current local time formatted using DefaultTimestampFormat.
func Fatal ¶
func Fatal(a ...any)
Fatal logs a message at the fatal level using the DefaultLogger and panics.
func Fatalf ¶
Fatalf logs a formatted message at the fatal level using the DefaultLogger and panics.
func LogFunc ¶
LogFunc logs a lazily-evaluated message using the DefaultLogger if the level is enabled.
func LogIf ¶
func LogIf(level Level, log func())
LogIf executes a function if the level is enabled using the DefaultLogger.
func SetDefaultLogger ¶
func SetDefaultLogger(logger Logger)
SetDefaultLogger overrides the global DefaultLogger with a new one.
Types ¶
type Color ¶
type Color string
Color defines an ANSI escape sequence for terminal text and background coloring.
const ( // ColorReset resets the terminal color to default. ColorReset Color = "\033[0m" // Foreground text colors. ColorTextRed Color = "\033[31m" ColorTextYellow Color = "\033[33m" ColorTextGreen Color = "\033[32m" ColorTextBlue Color = "\033[34m" ColorTextMagenta Color = "\033[35m" ColorTextCyan Color = "\033[36m" ColorTextWhite Color = "\033[37m" ColorTextPurple Color = "\033[35m" // Alias for magenta ColorTextBlack Color = "\033[30m" // Background colors. ColorBgRed Color = "\033[41m" ColorBgYellow Color = "\033[43m" ColorBgGreen Color = "\033[42m" ColorBgBlue Color = "\033[44m" ColorBgMagenta Color = "\033[45m" ColorBgCyan Color = "\033[46m" ColorBgWhite Color = "\033[47m" ColorBgBlack Color = "\033[40m" )
type Config ¶
type Config struct {
Timestamp func() string
}
Config defines the configuration for a Formatter, such as a custom timestamp function.
type Formatter ¶
Formatter is the interface implemented by all formatters in the package. It defines how a log entry is rendered as a string.
func ConsoleFormatter ¶
func ConsoleFormatter() Formatter
ConsoleFormatter returns a new colorized console formatter with the default configuration.
func JSONFormatter ¶
func JSONFormatter() Formatter
JSONFormatter returns a new JSON formatter with the default configuration.
func NewConsoleFormatter ¶
NewConsoleFormatter creates a new consoleFormatter using the provided configuration.
func NewFormatter ¶
NewFormatter returns a new Formatter based on the provided Format and the default configuration.
func NewFormatterWithConfig ¶
NewFormatterWithConfig returns a new Formatter based on the provided Format and Config.
func NewJsonFormatter ¶
NewJsonFormatter creates a new jsonFormatter using the provided configuration.
func NewTextFormatter ¶
NewTextFormatter creates a new textFormatter using the provided configuration.
func TextFormatter ¶
func TextFormatter() Formatter
TextFormatter returns a new plain text formatter with the default configuration.
type Level ¶
type Level int
Level represents the severity of a log message.
const ( // LevelDebug represents fine-grained debug information. LevelDebug Level = iota - 1 // LevelInfo represents general operational entries about what's going on inside the application. LevelInfo // LevelWarn represents potentially harmful situations. LevelWarn // LevelError represents error events that might still allow the application to continue running. LevelError // LevelFatal represents very severe error events that will presumably lead the application to abort. LevelFatal // LevelPrint is used for messages that should always be printed regardless of level filtering. LevelPrint = math.MaxInt )
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger is the primary struct for logging messages with optional fields and errors.
var DefaultLogger Logger
DefaultLogger is the global logger used by package-level log functions.
func NewLogger ¶
NewLogger creates a new Logger instance with the given level, formatter, and output writer.
func WithFields ¶
WithFields returns a copy of the DefaultLogger with additional fields.
func (Logger) LogFunc ¶
LogFunc evaluates the message-producing function only if the log level is enabled.
func (Logger) WithFields ¶
WithFields returns a new Logger with additional key-value pairs.