Skip to content

Commit

Permalink
[wip]feat: add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
zstone12 committed Oct 28, 2024
1 parent 99150d7 commit 3e31060
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
4 changes: 4 additions & 0 deletions logging/slog/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module gorm.io/plugin/opentelemetry/logging/slog
go 1.22

require (
github.com/stretchr/testify v1.9.0
go.opentelemetry.io/otel v1.31.0
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.31.0
go.opentelemetry.io/otel/sdk v1.31.0
Expand All @@ -12,13 +13,16 @@ require (
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/mattn/go-sqlite3 v1.14.22 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/otel/metric v1.31.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
2 changes: 2 additions & 0 deletions logging/slog/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/sqlite v1.5.6 h1:fO/X46qn5NUEEOZtnjJRWRzZMe8nqJiQ9E+0hi+hKQE=
Expand Down
95 changes: 95 additions & 0 deletions logging/slog/logger_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package slog

import (
"bytes"
"context"
"github.com/stretchr/testify/assert"
"log/slog"
"os"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -74,3 +79,93 @@ func TestLogger(t *testing.T) {

errSpan.End()
}

func TestLogger1(t *testing.T) {
ctx := context.Background()

buf := new(bytes.Buffer)

shutdown := stdoutProvider(ctx)
defer shutdown()

logger := logger.New(
NewWriter(
WithTraceErrorSpanLevel(slog.LevelWarn),
WithRecordStackTraceInSpan(true),
), logger.Config{
SlowThreshold: time.Millisecond,
LogLevel: logger.Warn,
Colorful: false,
})

logger.Info(ctx, "log from origin slog")
assert.True(t, strings.Contains(buf.String(), "log from origin slog"))
buf.Reset()

tracer := otel.Tracer("test otel std logger")

ctx, span := tracer.Start(ctx, "root")

assert.True(t, strings.Contains(buf.String(), "trace_id"))
assert.True(t, strings.Contains(buf.String(), "span_id"))
assert.True(t, strings.Contains(buf.String(), "trace_flags"))
buf.Reset()

span.End()

ctx, child := tracer.Start(ctx, "child")

child.End()

_, errSpan := tracer.Start(ctx, "error")

errSpan.End()
}

func TestLogLevel(t *testing.T) {
buf := new(bytes.Buffer)

logger := NewWriter(
WithTraceErrorSpanLevel(slog.LevelWarn),
WithRecordStackTraceInSpan(true),
)

/* // output to buffer
logger.SetOutput(buf)
logger.Debug("this is a debug log")
assert.False(t, strings.Contains(buf.String(), "this is a debug log"))
logger.SetLevel(klog.LevelDebug)
*/
logger.log.Debug("this is a debug log msg")
assert.True(t, strings.Contains(buf.String(), "this is a debug log"))
}

func TestLogOption(t *testing.T) {
buf := new(bytes.Buffer)

lvl := new(slog.LevelVar)
lvl.Set(slog.LevelDebug)
logger := NewWriter(
WithLevel(lvl),
WithOutput(buf),
WithHandlerOptions(&slog.HandlerOptions{
AddSource: true,
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
if a.Key == slog.MessageKey {
msg := a.Value.Any().(string)
msg = strings.ReplaceAll(msg, "log", "new log")
a.Value = slog.StringValue(msg)
}
return a
},
}),
)

logger.log.Debug("this is a debug log")
assert.True(t, strings.Contains(buf.String(), "this is a debug new log"))

dir, _ := os.Getwd()
assert.True(t, strings.Contains(buf.String(), dir))
}

0 comments on commit 3e31060

Please sign in to comment.