diff --git a/.changeset/brave-ants-poke.md b/.changeset/brave-ants-poke.md new file mode 100644 index 00000000..a6cd8812 --- /dev/null +++ b/.changeset/brave-ants-poke.md @@ -0,0 +1,5 @@ +--- +"github.com/livekit/protocol": minor +--- + +Expose Logger constructor for Zap. diff --git a/logger/logger.go b/logger/logger.go index 140b6a78..453f0544 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -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, @@ -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,