Skip to content

Commit

Permalink
add caller to log output (#620)
Browse files Browse the repository at this point in the history
* fix log caller

* add test for log output
  • Loading branch information
paulwe authored Feb 26, 2024
1 parent 4a97b58 commit 3339e80
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ type zapLogger[T zaputil.Encoder[T]] struct {
}

func NewZapLogger(conf *Config, opts ...ZapLoggerOption) (ZapLogger, error) {
zap := zap.New(nil, zap.AddCaller(), zap.AddStacktrace(zap.ErrorLevel)).Sugar()
zap := zap.New(nil).WithOptions(zap.AddCaller(), zap.AddStacktrace(zap.ErrorLevel)).Sugar()

zc := &zapConfig{
conf: conf,
Expand Down
32 changes: 32 additions & 0 deletions logger/logger_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package logger

import (
"bytes"
"encoding/json"
"testing"

"github.com/stretchr/testify/require"
"go.uber.org/zap/zapcore"

"github.com/livekit/protocol/logger/zaputil"
)

func zapLoggerCore(l Logger) zapcore.Core {
Expand Down Expand Up @@ -73,4 +77,32 @@ func TestLoggerComponent(t *testing.T) {
require.True(t, sub.Enabled(zapcore.InfoLevel))
require.True(t, sub2.Enabled(zapcore.InfoLevel))
})

t.Run("log output matches expected values", func(t *testing.T) {
ws := &testBufferedWriteSyncer{}
l, err := NewZapLogger(&Config{}, WithTap(zaputil.NewWriteEnabler(ws, zapcore.DebugLevel)))
require.NoError(t, err)
l.Debugw("foo", "bar", "baz")

var log struct {
Level string
TS float64
Caller string
Msg string
Bar string
}
require.NoError(t, json.Unmarshal(ws.Bytes(), &log))

require.Equal(t, "debug", log.Level)
require.NotEqual(t, 0, log.TS)
require.NotEqual(t, "", log.Caller)
require.Equal(t, "foo", log.Msg)
require.Equal(t, "baz", log.Bar)
})
}

type testBufferedWriteSyncer struct {
bytes.Buffer
}

func (t *testBufferedWriteSyncer) Sync() error { return nil }

0 comments on commit 3339e80

Please sign in to comment.