Documentation ¶
Overview ¶
Package eslog is an extension of the pkg/log/slog.Logger logging package. This package formats logging messages in a human-readable form, contains two additional logging levels Trace and Fatal.
Includes Logger that combines functionality for setting logging level.
Demonstrates how to configure and use the logger with various logging levels and message types. It sets up the logger with custom configuration, including logging level and output format. Then, it overrides the logging level for demonstration purposes. Finally, it logs messages at different levels using the configured logger.
// Configures the logger with the specified settings. config := eslog.Config{ Level: "info", // trace, debug, info, warn, error, fatal (info: default) AddSource: true, JSON: false, } // Sets the logging level. lvl := new(slog.LevelVar) lvl.Set(config.Leveler()) // Creates a new instance of eslog with the configured settings. logger := eslog.New(pretty.NewHandler(os.Stdout, &pretty.HandlerOptions{ SlogOptions: &slog.HandlerOptions{ Level: lvl, AddSource: config.AddSource, }, JSON: config.JSON, }), lvl) // Overrides the logging level. logger.SetLevel(eslog.LevelTrace) //logger.SetLevel(slog.LevelDebug) //logger.SetLevel(slog.LevelInfo) //logger.SetLevel(slog.LevelWarn) //logger.SetLevel(slog.LevelError) //logger.SetLevel(eslog.LevelFatal) // Logs messages at different levels. logger.Trace("Trace example", slog.Any("message", "trace message")) logger.Debug("Debug example", slog.Any("message", "debug message")) logger.Info("Info example", slog.Any("message", "info message")) logger.Warn("Warn example", slog.Any("message", "warn message")) logger.Error("Error example", slog.Any("message", "error message")) logger.Fatal("Fatal example", slog.Any("message", "fatal message"))
Index ¶
Constants ¶
const ( LevelTrace slog.Level = -8 LevelFatal slog.Level = 12 )
Names for common levels. Complements standard slog levels. See: https://pkg.go.dev/golang.org/x/exp/slog#Level
Variables ¶
var Level = map[string]slog.Level{ "trace": LevelTrace, "debug": slog.LevelDebug, "info": slog.LevelInfo, "warn": slog.LevelWarn, "error": slog.LevelError, "fatal": LevelFatal, }
Level names for common levels.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct { Level string `yaml:"level" env-default:"info"` AddSource bool `yaml:"add-source"` JSON bool `yaml:"json"` }
Config contains configuration options for logging.
type Logger ¶
type Logger interface { // Leveler is an interface for setting the logging level. Leveler // Loggerer is an interface for logging messages at different levels. // Complements standard slog methods. Loggerer // Printerer is an interface for printing messages. Printerer // Slogger returns the underlying [slog.Logger]. Slogger() *slog.Logger }
Logger is an interface that combines functionality for setting logging level.
type Loggerer ¶
type Loggerer interface { // Trace logs a message at trace level. Trace(msg string, args ...any) // Debug logs a message at trace level. Debug(msg string, args ...any) // Info logs a message at trace level. Info(msg string, args ...any) // Warn logs a message at trace level. Warn(msg string, args ...any) // Error logs a message at trace level. Error(msg string, args ...any) // Fatal logs a message at trace level. Fatal(msg string, args ...any) }
Loggerer is an interface for logging messages at different levels. Complements standard slog methods.
type Printerer ¶
type Printerer interface { // Sprintf formats according to a format specifier and returns the resulting string. Sprintf(msg string, args ...any) string // Fatalf formats according to a format specifier and prints the resulting string, // then exits the program with a non-zero exit status. Fatalf(msg string, args ...any) // Print prints the message. Print(msg string, args ...any) // Printf formats according to a format specifier and prints the resulting string. Printf(msg string, args ...any) }
Printerer is an interface for printing messages.