Skip to content

Commit

Permalink
Expose Logger constructor for Zap.
Browse files Browse the repository at this point in the history
  • Loading branch information
dennwc committed Apr 30, 2024
1 parent e7962f4 commit 4726711
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/brave-ants-poke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"github.com/livekit/protocol": minor
---

Expose Logger constructor for Zap.
11 changes: 9 additions & 2 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,11 @@ type zapLogger[T zaputil.Encoder[T]] struct {
sampler *zaputil.Sampler
}

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

zc := &zapConfig{
conf: conf,
Expand Down Expand Up @@ -234,6 +237,10 @@ func NewZapLogger(conf *Config, opts ...ZapLoggerOption) (ZapLogger, error) {
}
}

func NewZapLogger(conf *Config, opts ...ZapLoggerOption) (ZapLogger, error) {
return FromZapLogger(nil, conf, opts...)
}

func newZapLogger[T zaputil.Encoder[T]](zap *zap.SugaredLogger, zc *zapConfig, enc T, sampler *zaputil.Sampler) ZapLogger {
l := &zapLogger[T]{
zap: zap,
Expand Down

0 comments on commit 4726711

Please sign in to comment.