github.com/livekit/protocol@v1.16.1-0.20240517185851-47e4c6bba773/logger/pionlogger/logger.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 "strings" 19 20 "github.com/pion/logging" 21 22 "github.com/livekit/protocol/logger" 23 ) 24 25 var ( 26 pionIgnoredPrefixes = map[string]*prefixSet{ 27 "ice": { 28 "pingAllCandidates called with no candidate pairs", 29 "failed to send packet: io: read/write on closed pipe", 30 "Ignoring remote candidate with tcpType active", 31 "discard message from", 32 "Failed to discover mDNS candidate", 33 "Failed to read from candidate tcp", 34 "remote mDNS candidate added, but mDNS is disabled", 35 }, 36 "pc": { 37 "Failed to accept RTCP stream is already closed", 38 "Failed to accept RTP stream is already closed", 39 "Incoming unhandled RTCP ssrc", 40 }, 41 "tcp_mux": { 42 "Error reading first packet from", 43 "error closing connection", 44 }, 45 "turn": { 46 "error when handling datagram", 47 "Failed to send ChannelData from allocation", 48 "Failed to handle datagram", 49 }, 50 } 51 ) 52 53 type prefixSet []string 54 55 func (s *prefixSet) Match(msg string) bool { 56 if s == nil { 57 return false 58 } 59 60 for _, prefix := range *s { 61 if strings.HasPrefix(msg, prefix) { 62 return true 63 } 64 } 65 return false 66 } 67 68 func NewLoggerFactory(l logger.Logger) logging.LoggerFactory { 69 if zl, ok := l.(logger.ZapLogger); ok { 70 return &zapLoggerFactory{zl} 71 } 72 return &loggerFactory{l} 73 } 74 75 // zapLoggerFactory implements logging.LoggerFactory interface for zap loggers 76 type zapLoggerFactory struct { 77 logger logger.ZapLogger 78 } 79 80 func (f *zapLoggerFactory) NewLogger(scope string) logging.LeveledLogger { 81 return &zapLogAdapter{ 82 logger: f.logger, 83 level: f.logger.ComponentLeveler().ComponentLevel(formatComponent(scope)), 84 scope: scope, 85 ignoredPrefixes: pionIgnoredPrefixes[scope], 86 } 87 } 88 89 // loggerFactory implements logging.LoggerFactory interface for generic loggers 90 type loggerFactory struct { 91 logger logger.Logger 92 } 93 94 func (f *loggerFactory) NewLogger(scope string) logging.LeveledLogger { 95 return &logAdapter{ 96 logger: f.logger.WithComponent(formatComponent(scope)).WithCallDepth(1), 97 ignoredPrefixes: pionIgnoredPrefixes[scope], 98 } 99 } 100 101 func formatComponent(scope string) string { 102 return "pion." + scope 103 }