Skip to content

Commit

Permalink
feat: add set level and test
Browse files Browse the repository at this point in the history
  • Loading branch information
zstone12 committed Oct 30, 2024
1 parent 9cf34cb commit 2554467
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
12 changes: 12 additions & 0 deletions logging/slog/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package slog

import (
"fmt"
"io"
"log/slog"
)

Expand Down Expand Up @@ -71,3 +72,14 @@ func (w *Writer) Logger() *slog.Logger {
func (w *Writer) Printf(format string, v ...interface{}) {
w.log.Info(fmt.Sprintf(format, v...))
}

func (w *Writer) SetLvel(level slog.Level) {
w.config.coreConfig.level.Set(level)
w.config.coreConfig.withLevel = true
}

func (w *Writer) SetOutput(writer io.Writer) {
log := slog.New(NewTraceHandler(writer, w.config.coreConfig.opt, w.config.traceConfig))
w.config.coreConfig.writer = writer
w.log = log
}
36 changes: 16 additions & 20 deletions logging/slog/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,35 +81,33 @@ func TestLogger(t *testing.T) {
errSpan.End()
}

func TestLogger1(t *testing.T) {
func TestLoggerBase(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 := NewWriter(
WithTraceErrorSpanLevel(slog.LevelWarn),
WithOutput(buf),
WithRecordStackTraceInSpan(true),
)

logger.Info(ctx, "log from origin slog")
logger.log.InfoContext(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"))
// TODO fix it
logger.log.WarnContext(ctx, "hello world")
assert.True(t, strings.Contains(buf.String(), "TraceID"))
assert.True(t, strings.Contains(buf.String(), "SpanID"))
assert.True(t, strings.Contains(buf.String(), "TraceFlags"))
buf.Reset()

span.End()
Expand All @@ -131,14 +129,12 @@ func TestLogLevel(t *testing.T) {
WithRecordStackTraceInSpan(true),
)

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

logger.Debug("this is a debug log")
assert.False(t, strings.Contains(buf.String(), "this is a debug log"))
logger.SetLvel(slog.LevelDebug)

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"))
}
Expand Down

0 comments on commit 2554467

Please sign in to comment.