mirror of
https://github.com/hanzoai/base.git
synced 2026-10-21 03:41:50 +00:00
App callers and plugins want one structured-logging surface that does
not pin them to log/slog. Add a tiny Logger interface and two
implementations:
- SlogAdapter wraps *slog.Logger so the BatchHandler-backed on-disk
storage path keeps working; exposes Slog()/Handler() for the
BatchHandler.SetLevel reload hook and tests that type-assert the
underlying handler.
- LuxfiAdapter wraps github.com/luxfi/log.Logger so packages that do
not own the storage path can run on the canonical Lux logger
directly.
Both adapters share the geth-style variadic API (msg + "k", v pairs)
that the prior commit already collapsed every call site to, so flipping
core.App.Logger() to return Logger in the next commit is a pure
type-narrowing change for ~95 percent of consumers.
45 lines
1.5 KiB
Go
45 lines
1.5 KiB
Go
package logger
|
|
|
|
import "log/slog"
|
|
|
|
// SlogAdapter wraps a *slog.Logger so that it satisfies Logger. It is
|
|
// the only adapter that exposes the underlying slog.Handler — that
|
|
// escape hatch is required by the in-app log batcher (BatchHandler)
|
|
// and by tests that need to type-assert the handler.
|
|
type SlogAdapter struct {
|
|
l *slog.Logger
|
|
}
|
|
|
|
// NewSlog wraps the given *slog.Logger. Pass slog.New(handler) here.
|
|
func NewSlog(l *slog.Logger) *SlogAdapter {
|
|
if l == nil {
|
|
l = slog.Default()
|
|
}
|
|
return &SlogAdapter{l: l}
|
|
}
|
|
|
|
// Slog returns the wrapped *slog.Logger. Reserved for the BatchHandler
|
|
// plumbing — do not call from business logic.
|
|
func (s *SlogAdapter) Slog() *slog.Logger { return s.l }
|
|
|
|
// Handler returns the underlying slog.Handler, mainly so that
|
|
// BatchHandler can be reached for SetLevel during settings-reload.
|
|
func (s *SlogAdapter) Handler() slog.Handler { return s.l.Handler() }
|
|
|
|
// Debug logs at debug level.
|
|
func (s *SlogAdapter) Debug(msg string, kv ...any) { s.l.Debug(msg, kv...) }
|
|
|
|
// Info logs at info level.
|
|
func (s *SlogAdapter) Info(msg string, kv ...any) { s.l.Info(msg, kv...) }
|
|
|
|
// Warn logs at warn level.
|
|
func (s *SlogAdapter) Warn(msg string, kv ...any) { s.l.Warn(msg, kv...) }
|
|
|
|
// Error logs at error level.
|
|
func (s *SlogAdapter) Error(msg string, kv ...any) { s.l.Error(msg, kv...) }
|
|
|
|
// With returns a child logger seeded with the given key/value pairs.
|
|
func (s *SlogAdapter) With(kv ...any) Logger {
|
|
return &SlogAdapter{l: s.l.With(kv...)}
|
|
}
|