Skip to content

Commit e418881

Browse files
authored
refactor pion prefix matching (#691)
1 parent da776da commit e418881

File tree

3 files changed

+34
-41
lines changed

3 files changed

+34
-41
lines changed

logger/pionlogger/logadapter.go

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@ package pionlogger
1616

1717
import (
1818
"fmt"
19-
"strings"
2019

2120
"github.com/livekit/protocol/logger"
2221
)
2322

2423
// implements webrtc.LeveledLogger
2524
type logAdapter struct {
2625
logger logger.Logger
27-
ignoredPrefixes []string
26+
ignoredPrefixes prefixSet
2827
}
2928

3029
func (l *logAdapter) Trace(msg string) {
@@ -36,70 +35,61 @@ func (l *logAdapter) Tracef(format string, args ...interface{}) {
3635
}
3736

3837
func (l *logAdapter) Debug(msg string) {
39-
if l.shouldIgnore(msg) {
38+
if l.ignoredPrefixes.Match(msg) {
4039
return
4140
}
4241
l.logger.Debugw(msg)
4342
}
4443

4544
func (l *logAdapter) Debugf(format string, args ...interface{}) {
4645
msg := fmt.Sprintf(format, args...)
47-
if l.shouldIgnore(msg) {
46+
if l.ignoredPrefixes.Match(msg) {
4847
return
4948
}
5049
l.logger.Debugw(msg)
5150
}
5251

5352
func (l *logAdapter) Info(msg string) {
54-
if l.shouldIgnore(msg) {
53+
if l.ignoredPrefixes.Match(msg) {
5554
return
5655
}
5756
l.logger.Infow(msg)
5857
}
5958

6059
func (l *logAdapter) Infof(format string, args ...interface{}) {
6160
msg := fmt.Sprintf(format, args...)
62-
if l.shouldIgnore(msg) {
61+
if l.ignoredPrefixes.Match(msg) {
6362
return
6463
}
6564
l.logger.Infow(msg)
6665
}
6766

6867
func (l *logAdapter) Warn(msg string) {
69-
if l.shouldIgnore(msg) {
68+
if l.ignoredPrefixes.Match(msg) {
7069
return
7170
}
7271
l.logger.Warnw(msg, nil)
7372
}
7473

7574
func (l *logAdapter) Warnf(format string, args ...interface{}) {
7675
msg := fmt.Sprintf(format, args...)
77-
if l.shouldIgnore(msg) {
76+
if l.ignoredPrefixes.Match(msg) {
7877
return
7978
}
8079
l.logger.Warnw(msg, nil)
8180
}
8281

8382
func (l *logAdapter) Error(msg string) {
84-
if l.shouldIgnore(msg) {
83+
if l.ignoredPrefixes.Match(msg) {
8584
return
8685
}
8786
l.logger.Errorw(msg, nil)
8887
}
8988

9089
func (l *logAdapter) Errorf(format string, args ...interface{}) {
9190
msg := fmt.Sprintf(format, args...)
92-
if l.shouldIgnore(msg) {
91+
if l.ignoredPrefixes.Match(msg) {
9392
return
9493
}
9594
l.logger.Errorw(msg, nil)
9695
}
97-
98-
func (l *logAdapter) shouldIgnore(msg string) bool {
99-
for _, prefix := range l.ignoredPrefixes {
100-
if strings.HasPrefix(msg, prefix) {
101-
return true
102-
}
103-
}
104-
return false
105-
}

logger/pionlogger/logger.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515
package pionlogger
1616

1717
import (
18+
"strings"
19+
1820
"github.com/pion/logging"
1921

2022
"github.com/livekit/protocol/logger"
2123
)
2224

2325
var (
24-
pionIgnoredPrefixes = map[string][]string{
26+
pionIgnoredPrefixes = map[string]prefixSet{
2527
"ice": {
2628
"pingAllCandidates called with no candidate pairs",
2729
"failed to send packet: io: read/write on closed pipe",
@@ -48,6 +50,17 @@ var (
4850
}
4951
)
5052

53+
type prefixSet []string
54+
55+
func (s prefixSet) Match(msg string) bool {
56+
for _, prefix := range s {
57+
if strings.HasPrefix(msg, prefix) {
58+
return true
59+
}
60+
}
61+
return false
62+
}
63+
5164
func NewLoggerFactory(l logger.Logger) logging.LoggerFactory {
5265
if zl, ok := l.(logger.ZapLogger); ok {
5366
return &zapLoggerFactory{zl}

logger/pionlogger/zaplogadapter.go

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package pionlogger
1616

1717
import (
1818
"fmt"
19-
"strings"
2019

2120
"go.uber.org/atomic"
2221
"go.uber.org/zap"
@@ -34,16 +33,16 @@ const (
3433
// implements webrtc.LeveledLogger
3534
type zapLogAdapter struct {
3635
state atomic.Uint32
37-
logger logger.ZapLogger
36+
logger logger.Logger
3837
level zapcore.LevelEnabler
3938
scope string
40-
ignoredPrefixes []string
39+
ignoredPrefixes prefixSet
4140
}
4241

4342
func (l *zapLogAdapter) init() {
4443
for l.state.Load() != zapLogAdapterStateReady {
4544
if l.state.CompareAndSwap(zapLogAdapterStateCreated, zapLogAdapterStateBuilding) {
46-
l.logger = l.logger.WithComponent(formatComponent(l.scope)).WithCallDepth(1).(logger.ZapLogger)
45+
l.logger = l.logger.WithComponent(formatComponent(l.scope)).WithCallDepth(1)
4746
l.state.Store(zapLogAdapterStateReady)
4847
}
4948
}
@@ -65,7 +64,7 @@ func (l *zapLogAdapter) Tracef(format string, args ...interface{}) {
6564

6665
func (l *zapLogAdapter) Debug(msg string) {
6766
if l.level.Enabled(zap.DebugLevel) {
68-
if !l.shouldIgnore(msg) {
67+
if !l.ignoredPrefixes.Match(msg) {
6968
l.init()
7069
l.logger.Debugw(msg)
7170
}
@@ -75,7 +74,7 @@ func (l *zapLogAdapter) Debug(msg string) {
7574
func (l *zapLogAdapter) Debugf(format string, args ...interface{}) {
7675
if l.level.Enabled(zap.DebugLevel) {
7776
msg := fmt.Sprintf(format, args...)
78-
if !l.shouldIgnore(msg) {
77+
if !l.ignoredPrefixes.Match(msg) {
7978
l.init()
8079
l.logger.Debugw(msg)
8180
}
@@ -84,7 +83,7 @@ func (l *zapLogAdapter) Debugf(format string, args ...interface{}) {
8483

8584
func (l *zapLogAdapter) Info(msg string) {
8685
if l.level.Enabled(zap.InfoLevel) {
87-
if !l.shouldIgnore(msg) {
86+
if !l.ignoredPrefixes.Match(msg) {
8887
l.init()
8988
l.logger.Infow(msg)
9089
}
@@ -94,7 +93,7 @@ func (l *zapLogAdapter) Info(msg string) {
9493
func (l *zapLogAdapter) Infof(format string, args ...interface{}) {
9594
if l.level.Enabled(zap.InfoLevel) {
9695
msg := fmt.Sprintf(format, args...)
97-
if !l.shouldIgnore(msg) {
96+
if !l.ignoredPrefixes.Match(msg) {
9897
l.init()
9998
l.logger.Infow(msg)
10099
}
@@ -103,7 +102,7 @@ func (l *zapLogAdapter) Infof(format string, args ...interface{}) {
103102

104103
func (l *zapLogAdapter) Warn(msg string) {
105104
if l.level.Enabled(zap.WarnLevel) {
106-
if !l.shouldIgnore(msg) {
105+
if !l.ignoredPrefixes.Match(msg) {
107106
l.init()
108107
l.logger.Warnw(msg, nil)
109108
}
@@ -113,7 +112,7 @@ func (l *zapLogAdapter) Warn(msg string) {
113112
func (l *zapLogAdapter) Warnf(format string, args ...interface{}) {
114113
if l.level.Enabled(zap.WarnLevel) {
115114
msg := fmt.Sprintf(format, args...)
116-
if !l.shouldIgnore(msg) {
115+
if !l.ignoredPrefixes.Match(msg) {
117116
l.init()
118117
l.logger.Warnw(msg, nil)
119118
}
@@ -122,7 +121,7 @@ func (l *zapLogAdapter) Warnf(format string, args ...interface{}) {
122121

123122
func (l *zapLogAdapter) Error(msg string) {
124123
if l.level.Enabled(zap.ErrorLevel) {
125-
if !l.shouldIgnore(msg) {
124+
if !l.ignoredPrefixes.Match(msg) {
126125
l.init()
127126
l.logger.Errorw(msg, nil)
128127
}
@@ -132,18 +131,9 @@ func (l *zapLogAdapter) Error(msg string) {
132131
func (l *zapLogAdapter) Errorf(format string, args ...interface{}) {
133132
if l.level.Enabled(zap.ErrorLevel) {
134133
msg := fmt.Sprintf(format, args...)
135-
if !l.shouldIgnore(msg) {
134+
if !l.ignoredPrefixes.Match(msg) {
136135
l.init()
137136
l.logger.Errorw(msg, nil)
138137
}
139138
}
140139
}
141-
142-
func (l *zapLogAdapter) shouldIgnore(msg string) bool {
143-
for _, prefix := range l.ignoredPrefixes {
144-
if strings.HasPrefix(msg, prefix) {
145-
return true
146-
}
147-
}
148-
return false
149-
}

0 commit comments

Comments
 (0)