feat(logz)!: promote to v1.0.0 — configurable io.Writer output destination

Add Writer io.Writer field to Options; when nil, defaults to os.Stdout (no
breaking change). Enables log capture in tests via the public API and supports
writing to files, multi-writers, or any io.Writer implementation in production.
All remaining roadmap items validated in production (xerrors enrichment
concurrency, WithFields merge semantics, Logger interface finality). API
committed as stable.
This commit is contained in:
2026-05-11 18:50:37 -06:00
parent 3667b92fab
commit 05c99f72ff
3 changed files with 41 additions and 18 deletions

View File

@@ -36,9 +36,8 @@ func (e *errFull) ErrorContext() map[string]any { return e.fields }
// Helper: logger that writes to a buffer for inspection
// ---------------------------------------------------------------
func newTestLogger(buf *bytes.Buffer, level slog.Level) *slogLogger {
handler := slog.NewJSONHandler(buf, &slog.HandlerOptions{Level: level})
return &slogLogger{logger: slog.New(handler)}
func newTestLogger(buf *bytes.Buffer, level slog.Level) Logger {
return New(Options{Writer: buf, Level: level, JSON: true})
}
// ---------------------------------------------------------------
@@ -56,10 +55,8 @@ func TestNew_ZeroOptions(t *testing.T) {
func TestNew_JSONFormat(t *testing.T) {
var buf bytes.Buffer
sl := &slogLogger{
logger: slog.New(slog.NewJSONHandler(&buf, &slog.HandlerOptions{Level: slog.LevelInfo})),
}
sl.Info("json test", "key", "value")
l := New(Options{JSON: true, Writer: &buf})
l.Info("json test", "key", "value")
var out map[string]any
if err := json.Unmarshal(buf.Bytes(), &out); err != nil {
@@ -70,13 +67,19 @@ func TestNew_JSONFormat(t *testing.T) {
}
}
func TestNew_Writer(t *testing.T) {
var buf bytes.Buffer
l := New(Options{Writer: &buf})
l.Info("writer test")
if !strings.Contains(buf.String(), "writer test") {
t.Errorf("output not written to provided writer: %s", buf.String())
}
}
func TestNew_StaticArgs(t *testing.T) {
var buf bytes.Buffer
handler := slog.NewJSONHandler(&buf, &slog.HandlerOptions{Level: slog.LevelInfo})
base := slog.New(handler).With("service", "test-svc")
sl := &slogLogger{logger: base}
sl.Info("static args test")
l := New(Options{Writer: &buf, JSON: true, StaticArgs: []any{"service", "test-svc"}})
l.Info("static args test")
if !strings.Contains(buf.String(), "test-svc") {
t.Errorf("static arg not found in output: %s", buf.String())