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.
58 lines
1.4 KiB
Go
58 lines
1.4 KiB
Go
package logger_test
|
|
|
|
import (
|
|
"bytes"
|
|
"log/slog"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/hanzoai/base/tools/logger"
|
|
luxlog "github.com/luxfi/log"
|
|
)
|
|
|
|
func TestSlogAdapterWritesPositionalAttrs(t *testing.T) {
|
|
var buf bytes.Buffer
|
|
base := slog.New(slog.NewJSONHandler(&buf, nil))
|
|
|
|
adapter := logger.NewSlog(base)
|
|
adapter.Info("hello", "k", "v", "n", 7)
|
|
|
|
out := buf.String()
|
|
if !strings.Contains(out, `"k":"v"`) {
|
|
t.Fatalf("expected k=v attr, got %q", out)
|
|
}
|
|
if !strings.Contains(out, `"n":7`) {
|
|
t.Fatalf("expected n=7 attr, got %q", out)
|
|
}
|
|
}
|
|
|
|
func TestSlogAdapterWithSeedsAttrs(t *testing.T) {
|
|
var buf bytes.Buffer
|
|
base := slog.New(slog.NewJSONHandler(&buf, nil))
|
|
|
|
root := logger.NewSlog(base)
|
|
child := root.With("component", "x")
|
|
child.Warn("boom")
|
|
|
|
out := buf.String()
|
|
if !strings.Contains(out, `"component":"x"`) {
|
|
t.Fatalf("expected seeded component=x, got %q", out)
|
|
}
|
|
if !strings.Contains(out, `"level":"WARN"`) {
|
|
t.Fatalf("expected WARN level, got %q", out)
|
|
}
|
|
}
|
|
|
|
func TestLuxfiAdapterSmoke(t *testing.T) {
|
|
// The luxfi adapter is a pass-through; just exercise the Logger
|
|
// surface to be sure the methods are wired and don't panic.
|
|
l := logger.NewLuxfi(luxlog.New())
|
|
l.Debug("d", "k", "v")
|
|
l.Info("i", "k", "v")
|
|
l.Warn("w", "k", "v")
|
|
l.Error("e", "k", "v")
|
|
if child := l.With("component", "test"); child == nil {
|
|
t.Fatalf("With returned nil")
|
|
}
|
|
}
|