github.com/livekit/protocol@v1.16.1-0.20240517185851-47e4c6bba773/logger/pionlogger/zaplogadapter.go (about)

     1  // Copyright 2023 LiveKit, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package pionlogger
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"go.uber.org/atomic"
    21  	"go.uber.org/zap"
    22  	"go.uber.org/zap/zapcore"
    23  
    24  	"github.com/livekit/protocol/logger"
    25  )
    26  
    27  const (
    28  	zapLogAdapterStateCreated = iota
    29  	zapLogAdapterStateBuilding
    30  	zapLogAdapterStateReady
    31  )
    32  
    33  // implements webrtc.LeveledLogger
    34  type zapLogAdapter struct {
    35  	state           atomic.Uint32
    36  	logger          logger.Logger
    37  	level           zapcore.LevelEnabler
    38  	scope           string
    39  	ignoredPrefixes *prefixSet
    40  }
    41  
    42  func (l *zapLogAdapter) init() {
    43  	for l.state.Load() != zapLogAdapterStateReady {
    44  		if l.state.CompareAndSwap(zapLogAdapterStateCreated, zapLogAdapterStateBuilding) {
    45  			l.logger = l.logger.WithComponent(formatComponent(l.scope)).WithCallDepth(1)
    46  			l.state.Store(zapLogAdapterStateReady)
    47  		}
    48  	}
    49  }
    50  
    51  func (l *zapLogAdapter) Trace(msg string) {
    52  	if l.level.Enabled(zap.DebugLevel) {
    53  		l.init()
    54  		l.logger.Debugw(msg)
    55  	}
    56  }
    57  
    58  func (l *zapLogAdapter) Tracef(format string, args ...interface{}) {
    59  	if l.level.Enabled(zap.DebugLevel) {
    60  		l.init()
    61  		l.logger.Debugw(fmt.Sprintf(format, args...))
    62  	}
    63  }
    64  
    65  func (l *zapLogAdapter) Debug(msg string) {
    66  	if l.level.Enabled(zap.DebugLevel) {
    67  		if !l.ignoredPrefixes.Match(msg) {
    68  			l.init()
    69  			l.logger.Debugw(msg)
    70  		}
    71  	}
    72  }
    73  
    74  func (l *zapLogAdapter) Debugf(format string, args ...interface{}) {
    75  	if l.level.Enabled(zap.DebugLevel) {
    76  		msg := fmt.Sprintf(format, args...)
    77  		if !l.ignoredPrefixes.Match(msg) {
    78  			l.init()
    79  			l.logger.Debugw(msg)
    80  		}
    81  	}
    82  }
    83  
    84  func (l *zapLogAdapter) Info(msg string) {
    85  	if l.level.Enabled(zap.InfoLevel) {
    86  		if !l.ignoredPrefixes.Match(msg) {
    87  			l.init()
    88  			l.logger.Infow(msg)
    89  		}
    90  	}
    91  }
    92  
    93  func (l *zapLogAdapter) Infof(format string, args ...interface{}) {
    94  	if l.level.Enabled(zap.InfoLevel) {
    95  		msg := fmt.Sprintf(format, args...)
    96  		if !l.ignoredPrefixes.Match(msg) {
    97  			l.init()
    98  			l.logger.Infow(msg)
    99  		}
   100  	}
   101  }
   102  
   103  func (l *zapLogAdapter) Warn(msg string) {
   104  	if l.level.Enabled(zap.WarnLevel) {
   105  		if !l.ignoredPrefixes.Match(msg) {
   106  			l.init()
   107  			l.logger.Warnw(msg, nil)
   108  		}
   109  	}
   110  }
   111  
   112  func (l *zapLogAdapter) Warnf(format string, args ...interface{}) {
   113  	if l.level.Enabled(zap.WarnLevel) {
   114  		msg := fmt.Sprintf(format, args...)
   115  		if !l.ignoredPrefixes.Match(msg) {
   116  			l.init()
   117  			l.logger.Warnw(msg, nil)
   118  		}
   119  	}
   120  }
   121  
   122  func (l *zapLogAdapter) Error(msg string) {
   123  	if l.level.Enabled(zap.ErrorLevel) {
   124  		if !l.ignoredPrefixes.Match(msg) {
   125  			l.init()
   126  			l.logger.Errorw(msg, nil)
   127  		}
   128  	}
   129  }
   130  
   131  func (l *zapLogAdapter) Errorf(format string, args ...interface{}) {
   132  	if l.level.Enabled(zap.ErrorLevel) {
   133  		msg := fmt.Sprintf(format, args...)
   134  		if !l.ignoredPrefixes.Match(msg) {
   135  			l.init()
   136  			l.logger.Errorw(msg, nil)
   137  		}
   138  	}
   139  }