mirror of
https://github.com/lordmathis/lemma.git
synced 2025-11-05 23:44:22 +00:00
Add Logger interface
This commit is contained in:
@@ -6,8 +6,23 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Logger represents the interface for logging operations
|
||||||
|
type Logger interface {
|
||||||
|
Debug(msg string, args ...any)
|
||||||
|
Info(msg string, args ...any)
|
||||||
|
Warn(msg string, args ...any)
|
||||||
|
Error(msg string, args ...any)
|
||||||
|
WithGroup(name string) Logger
|
||||||
|
With(args ...any) Logger
|
||||||
|
}
|
||||||
|
|
||||||
|
// Implementation of the Logger interface using slog
|
||||||
|
type logger struct {
|
||||||
|
logger *slog.Logger
|
||||||
|
}
|
||||||
|
|
||||||
// Logger is the global logger instance
|
// Logger is the global logger instance
|
||||||
var Logger *slog.Logger
|
var defaultLogger Logger
|
||||||
|
|
||||||
// LogLevel represents the log level
|
// LogLevel represents the log level
|
||||||
type LogLevel slog.Level
|
type LogLevel slog.Level
|
||||||
@@ -26,7 +41,9 @@ func Setup(minLevel LogLevel) {
|
|||||||
Level: slog.Level(minLevel),
|
Level: slog.Level(minLevel),
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger = slog.New(slog.NewTextHandler(os.Stdout, opts))
|
defaultLogger = &logger{
|
||||||
|
logger: slog.New(slog.NewTextHandler(os.Stdout, opts)),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseLogLevel converts a string to a LogLevel
|
// ParseLogLevel converts a string to a LogLevel
|
||||||
@@ -43,32 +60,57 @@ func ParseLogLevel(level string) LogLevel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Implementation of Logger interface methods
|
||||||
|
func (l *logger) Debug(msg string, args ...any) {
|
||||||
|
l.logger.Debug(msg, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *logger) Info(msg string, args ...any) {
|
||||||
|
l.logger.Info(msg, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *logger) Warn(msg string, args ...any) {
|
||||||
|
l.logger.Warn(msg, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *logger) Error(msg string, args ...any) {
|
||||||
|
l.logger.Error(msg, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *logger) WithGroup(name string) Logger {
|
||||||
|
return &logger{logger: l.logger.WithGroup(name)}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *logger) With(args ...any) Logger {
|
||||||
|
return &logger{logger: l.logger.With(args...)}
|
||||||
|
}
|
||||||
|
|
||||||
// Debug logs a debug message
|
// Debug logs a debug message
|
||||||
func Debug(msg string, args ...any) {
|
func Debug(msg string, args ...any) {
|
||||||
Logger.Debug(msg, args...)
|
defaultLogger.Debug(msg, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Info logs an info message
|
// Info logs an info message
|
||||||
func Info(msg string, args ...any) {
|
func Info(msg string, args ...any) {
|
||||||
Logger.Info(msg, args...)
|
defaultLogger.Info(msg, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Warn logs a warning message
|
// Warn logs a warning message
|
||||||
func Warn(msg string, args ...any) {
|
func Warn(msg string, args ...any) {
|
||||||
Logger.Warn(msg, args...)
|
defaultLogger.Warn(msg, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Error logs an error message
|
// Error logs an error message
|
||||||
func Error(msg string, args ...any) {
|
func Error(msg string, args ...any) {
|
||||||
Logger.Error(msg, args...)
|
defaultLogger.Error(msg, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithGroup adds a group to the logger context
|
// WithGroup adds a group to the logger context
|
||||||
func WithGroup(name string) *slog.Logger {
|
func WithGroup(name string) Logger {
|
||||||
return Logger.WithGroup(name)
|
return defaultLogger.WithGroup(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
// With adds key-value pairs to the logger context
|
// With adds key-value pairs to the logger context
|
||||||
func With(args ...any) *slog.Logger {
|
func With(args ...any) Logger {
|
||||||
return Logger.With(args...)
|
return defaultLogger.With(args...)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user