chore: establish chub go foundation
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
package logging
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log/slog"
|
||||
)
|
||||
|
||||
// New creates the process logger. Callers provide the sink so tests and the
|
||||
// command entry point do not need to share global logging state.
|
||||
func New(w io.Writer) *slog.Logger {
|
||||
if w == nil {
|
||||
w = io.Discard
|
||||
}
|
||||
return slog.New(slog.NewJSONHandler(w, &slog.HandlerOptions{Level: slog.LevelInfo}))
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package logging
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNewWritesStructuredLog(t *testing.T) {
|
||||
var output bytes.Buffer
|
||||
logger := New(&output)
|
||||
logger.Info("test event", "component", "test")
|
||||
|
||||
value := output.String()
|
||||
if !strings.Contains(value, `"msg":"test event"`) || !strings.Contains(value, `"component":"test"`) {
|
||||
t.Fatalf("log output = %q", value)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewNilWriterDoesNotPanic(t *testing.T) {
|
||||
New(nil).Info("discarded")
|
||||
}
|
||||
Reference in New Issue
Block a user