chore: establish chub go foundation

This commit is contained in:
QiuSW
2026-07-22 14:51:11 +08:00
commit f8aaaeda21
28 changed files with 886 additions and 0 deletions
+15
View File
@@ -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}))
}
+22
View File
@@ -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")
}