Files
tasks/common/log/slog_test.go
zeekay 5a601d081b rebrand: go.temporal.io/server -> github.com/hanzoai/tasks
Full module path rebrand from Temporal to Hanzo Tasks:
- go.mod module: github.com/hanzoai/tasks
- All internal Go imports updated (1906 files)
- cmd/server renamed to cmd/tasksd
- Binary name: tasksd (was temporal-server)
- Docker images: hanzoai/tasks (was temporalio/server)
- README rewritten with Hanzo Tasks branding
- LLM.md created with integration notes
- Makefile, goreleaser, CI workflows updated
- go.temporal.io/sdk and go.temporal.io/api imports preserved (separate repos)

Fork of Temporal.io (MIT license).
2026-03-19 10:36:40 -07:00

52 lines
1.7 KiB
Go

package log
import (
"context"
"encoding/json"
"log/slog"
"strings"
"testing"
"github.com/stretchr/testify/require"
"github.com/hanzoai/tasks/common/log/tag"
"github.com/hanzoai/tasks/common/quotas"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type writeBuffer struct {
buf []byte
}
var _ zapcore.WriteSyncer = (*writeBuffer)(nil)
// Sync implements zapcore.WriteSyncer.
func (*writeBuffer) Sync() error {
return nil
}
// Write implements zapcore.WriteSyncer.
func (b *writeBuffer) Write(p []byte) (n int, err error) {
b.buf = append(b.buf, p...)
return len(p), nil
}
func TestSlogFromLayeredLogger(t *testing.T) {
buffer := writeBuffer{nil}
core := zapcore.NewCore(zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()), &buffer, zap.InfoLevel)
l := NewSlogLogger(NewThrottledLogger(With(NewZapLogger(zap.New(core)), tag.ClusterName("corn")), quotas.InfDuration.Hours))
require.True(t, l.Enabled(context.Background(), slog.LevelInfo))
require.False(t, l.Enabled(context.Background(), slog.LevelDebug))
l.With("a", "b").WithGroup("hey").With("ho", "lets").WithGroup("go").With("c", 4).Info("wow", "gg", "wp")
var record map[string]any
err := json.Unmarshal(buffer.buf, &record)
require.NoError(t, err)
require.Equal(t, "b", record["a"])
require.Equal(t, "lets", record["hey.ho"])
require.Equal(t, float64(4), record["hey.go.c"]) // untyped json unmarshals numbers as floats
require.Equal(t, "wp", record["hey.go.gg"])
require.Equal(t, "wow", record["msg"])
require.Equal(t, "corn", record["cluster-name"])
require.True(t, strings.HasSuffix(record["logging-call-at"].(string), "slog_test.go:40"), "%q doesn't end with %q", record["logging-call-at"], "slog_test.go:40")
}