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 GetLevelName(level Level, cfg *Config) string
- func Info(a ...any)
- func Infof(format string, args ...any)
- func IsLevelEnabled(level Level) bool
- 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 SetLevelColor(level Level, color Color)
- func SetLevelName(level Level, name string)
- func Warn(a ...any)
- func Warnf(format string, args ...any)
- func WithLogger(ctx context.Context, logger Logger) context.Context
- 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) GetError() error
- func (logger Logger) GetFields() Fields
- func (logger Logger) GetLevel() Level
- func (logger Logger) GetTeeCount() int
- func (logger Logger) Info(a ...any)
- func (logger Logger) Infof(format string, args ...any)
- func (logger Logger) IsLevelEnabled(level Level) bool
- 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) Tee(loggers ...Logger) Logger
- 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) WithErrorHandler(handler func(error)) Logger
- func (logger Logger) WithFields(fields Fields) Logger
- func (logger Logger) WithLevel(level Level) Logger
Constants ¶
const CtxKeyLogger contextKey = "logos.logger"
CtxKeyLogger is the key used to store a Logger in context.Context.
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 DefaultLevels = map[string]Level{ "debug": LevelDebug, "info": LevelInfo, "warn": LevelWarn, "error": LevelError, "fatal": LevelFatal, "print": LevelPrint, }
DefaultLevels provides a default mapping for logging level strings to their corresponding Level values.
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. Access is protected by levelMu for thread safety.
var LevelNames map[Level]string
LevelNames maps Level values to human-readable strings. This can be overridden by the user. Access is protected by levelMu for thread safety.
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 default logger and panics.
func Fatalf ¶
Fatalf logs a formatted message at the fatal level using the default logger and panics.
func GetLevelName ¶ added in v0.2.0
GetLevelName returns the name for a level, using the Config's LevelNames if present, otherwise falling back to the global LevelNames map (with thread-safe access).
func IsLevelEnabled ¶ added in v0.2.0
IsLevelEnabled returns true if the default logger would log at the given level.
func LogFunc ¶
LogFunc logs a lazily-evaluated message using the default logger if the level is enabled.
func LogIf ¶
func LogIf(level Level, log func())
LogIf executes a function if the level is enabled using the default logger.
func SetDefaultLogger ¶
func SetDefaultLogger(logger Logger)
SetDefaultLogger overrides the global default logger with a new one. This function is thread-safe.
func SetLevelColor ¶ added in v0.2.0
SetLevelColor sets a custom color for a level in the global LevelColors map. This function is thread-safe.
func SetLevelName ¶ added in v0.2.0
SetLevelName sets a custom name for a level in the global LevelNames map. This function is thread-safe.
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" )
func GetLevelColor ¶ added in v0.2.0
GetLevelColor returns the color for a level, using the Config's LevelColors if present, otherwise falling back to the global LevelColors map (with thread-safe access).
type Config ¶
type Config struct {
Timestamp func() string
LevelNames map[Level]string // Optional: custom level names. Falls back to global LevelNames if nil.
LevelColors map[Level]Color // Optional: custom level colors. Falls back to global LevelColors if nil.
}
Config defines the configuration for a Formatter, such as a custom timestamp function, level names, and colors. If LevelNames or LevelColors are nil, the global defaults will be used.
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.
func FromContext ¶ added in v0.2.0
FromContext retrieves a Logger from the context. If a logger is found in the context, it returns that logger. Otherwise, it returns the default logger.
func NewLogger ¶
NewLogger creates a new Logger instance with the given level, formatter, and output writer.
func WithFields ¶
WithFields returns a copy of the default logger with additional fields.
func (Logger) Copy ¶
Copy creates a deep copy of the logger with an independent level. The copied logger shares the same mutex and writer as the original (for thread-safety), but has independent level, fields, and error state.
func (Logger) GetFields ¶ added in v0.2.0
GetFields returns a copy of the logger's fields. The returned map is independent and modifications won't affect the logger.
func (Logger) GetTeeCount ¶ added in v0.2.0
GetTeeCount returns the number of tee loggers attached to this logger.
func (Logger) IsLevelEnabled ¶ added in v0.2.0
IsLevelEnabled returns true if the logger would log at the given level. This is useful for avoiding expensive computations when the log level is not enabled.
func (Logger) LogFunc ¶
LogFunc evaluates the message-producing function only if at least one logger (main or tee) has the level enabled.
func (Logger) LogIf ¶
LogIf calls the provided function if at least one logger (main or tee) has the level enabled.
func (Logger) SetLevel ¶
SetLevel sets the logging level of the logger. Deprecated: Use WithLevel() instead for immutable logger creation.
func (Logger) Tee ¶ added in v0.2.0
Tee adds one or more loggers as tee destinations. Each tee logger will receive all log messages and handle its own level checking and formatting. This allows different destinations to have different levels, formatters, and fields.
func (Logger) WithError ¶
WithError returns a new Logger with an associated error. If the logger already has an error, it will be silently replaced.
func (Logger) WithErrorHandler ¶ added in v0.2.0
WithErrorHandler returns a new Logger with the specified error handler. The error handler is called whenever a write error occurs during logging. If handler is nil, write errors will be silently ignored.
func (Logger) WithFields ¶
WithFields returns a new Logger with additional key-value pairs.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
demos
|
|
|
01_basic
command
|
|
|
02_fields
command
|
|
|
03_levels
command
|
|
|
04_formatters
command
|
|
|
05_context
command
|
|
|
06_tee
command
|
|
|
07_errors
command
|
|
|
08_lazy_evaluation
command
|
|
|
09_custom_levels
command
|
|
|
10_custom_formatter
command
|
|
|
comprehensive_demo
command
|
|
|
master
command
|