Initialize narratio Go module and CLI scaffold
This commit is contained in:
2
internal/logging/doc.go
Normal file
2
internal/logging/doc.go
Normal file
@@ -0,0 +1,2 @@
|
||||
// Package logging provides structured logger construction for narratio.
|
||||
package logging
|
||||
17
internal/logging/logging.go
Normal file
17
internal/logging/logging.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package logging
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log/slog"
|
||||
"os"
|
||||
)
|
||||
|
||||
// NewLogger creates a text slog logger.
|
||||
func NewLogger(out io.Writer, level slog.Level) *slog.Logger {
|
||||
if out == nil {
|
||||
out = os.Stderr
|
||||
}
|
||||
|
||||
handler := slog.NewTextHandler(out, &slog.HandlerOptions{Level: level})
|
||||
return slog.New(handler)
|
||||
}
|
||||
31
internal/logging/logging_test.go
Normal file
31
internal/logging/logging_test.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package logging
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"log/slog"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNewLoggerWritesOutput(t *testing.T) {
|
||||
var buf bytes.Buffer
|
||||
logger := NewLogger(&buf, slog.LevelInfo)
|
||||
|
||||
if logger == nil {
|
||||
t.Fatal("logger is nil")
|
||||
}
|
||||
|
||||
logger.Info("hello", "component", "test")
|
||||
out := buf.String()
|
||||
if !strings.Contains(out, "hello") {
|
||||
t.Fatalf("output = %q, want to contain message", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewLoggerNilWriter(t *testing.T) {
|
||||
logger := NewLogger(nil, slog.LevelInfo)
|
||||
if logger == nil {
|
||||
t.Fatal("logger is nil")
|
||||
}
|
||||
logger.Info("should not panic")
|
||||
}
|
||||
Reference in New Issue
Block a user