Use common logging init functions (#633)

* Use common logging init functions

* update protocol commit

* fix tests
This commit is contained in:
David Zhao
2022-04-20 00:15:11 -07:00
committed by GitHub
parent 431069af95
commit b821a0997d
18 changed files with 59 additions and 81 deletions
+4 -4
View File
@@ -10,6 +10,8 @@ import (
"github.com/pkg/errors"
"github.com/urfave/cli/v2"
"gopkg.in/yaml.v3"
"github.com/livekit/protocol/logger"
)
var DefaultStunServers = []string{
@@ -140,10 +142,8 @@ type CodecSpec struct {
}
type LoggingConfig struct {
JSON bool `yaml:"json"`
Level string `yaml:"level"`
Sample bool `yaml:"sample,omitempty"`
PionLevel string `yaml:"pion_level,omitempty"`
logger.Config `yaml:",inline"`
PionLevel string `yaml:"pion_level,omitempty"`
}
type TURNConfig struct {
+16 -4
View File
@@ -22,7 +22,10 @@ func (l *logAdapter) Tracef(format string, args ...interface{}) {
}
func (l *logAdapter) Debug(msg string) {
l.Debugf(msg)
if l.level > zapcore.DebugLevel {
return
}
l.logger.V(1).Info(msg)
}
func (l *logAdapter) Debugf(format string, args ...interface{}) {
@@ -33,7 +36,10 @@ func (l *logAdapter) Debugf(format string, args ...interface{}) {
}
func (l *logAdapter) Info(msg string) {
l.Infof(msg)
if l.level > zapcore.InfoLevel {
return
}
l.logger.Info(msg)
}
func (l *logAdapter) Infof(format string, args ...interface{}) {
@@ -44,7 +50,10 @@ func (l *logAdapter) Infof(format string, args ...interface{}) {
}
func (l *logAdapter) Warn(msg string) {
l.Warnf(msg)
if l.level > zapcore.WarnLevel {
return
}
l.logger.Info(msg)
}
func (l *logAdapter) Warnf(format string, args ...interface{}) {
@@ -55,7 +64,10 @@ func (l *logAdapter) Warnf(format string, args ...interface{}) {
}
func (l *logAdapter) Error(msg string) {
l.Errorf(msg)
if l.level > zapcore.ErrorLevel {
return
}
l.logger.Error(nil, msg)
}
func (l *logAdapter) Errorf(format string, args ...interface{}) {
+2 -41
View File
@@ -2,9 +2,7 @@ package serverlogger
import (
"github.com/go-logr/logr"
"github.com/go-logr/zapr"
"github.com/pion/logging"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"github.com/livekit/protocol/logger"
@@ -43,43 +41,6 @@ func SetLogger(l logr.Logger) {
}
func InitFromConfig(config config.LoggingConfig) {
lvl := parseLevel(config.Level)
pionLevel = parseLevel(config.PionLevel)
if lvl > pionLevel {
pionLevel = lvl
}
zapConfig := zap.Config{
Level: zap.NewAtomicLevelAt(lvl),
Development: false,
Encoding: "console",
EncoderConfig: zap.NewDevelopmentEncoderConfig(),
OutputPaths: []string{"stderr"},
ErrorOutputPaths: []string{"stderr"},
}
if config.Sample {
zapConfig.Sampling = &zap.SamplingConfig{
Initial: 100,
Thereafter: 100,
}
}
if config.JSON {
zapConfig.Encoding = "json"
zapConfig.EncoderConfig = zap.NewProductionEncoderConfig()
}
initLogger(zapConfig)
}
// valid levels: debug, info, warn, error, fatal, panic
func initLogger(config zap.Config) {
l, _ := config.Build()
zapLogger := zapr.NewLogger(l)
SetLogger(zapLogger)
}
func parseLevel(level string) zapcore.Level {
lvl := zapcore.InfoLevel
if level != "" {
_ = lvl.UnmarshalText([]byte(level))
}
return lvl
pionLevel = logger.ParseZapLevel(config.PionLevel)
logger.InitFromConfig(config.Config, "livekit")
}
+1 -1
View File
@@ -61,7 +61,7 @@ type ParticipantOptions struct {
func NewRoom(room *livekit.Room, config WebRTCConfig, audioConfig *config.AudioConfig, telemetry telemetry.TelemetryService) *Room {
r := &Room{
Room: proto.Clone(room).(*livekit.Room),
Logger: LoggerWithRoom(logger.Logger(logger.GetLogger()), livekit.RoomName(room.Name), livekit.RoomID(room.Sid)),
Logger: LoggerWithRoom(logger.GetDefaultLogger(), livekit.RoomName(room.Name), livekit.RoomID(room.Sid)),
config: config,
audioConfig: audioConfig,
telemetry: telemetry,
+4 -1
View File
@@ -8,6 +8,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger"
"github.com/livekit/protocol/webhook"
"github.com/livekit/livekit-server/pkg/config"
@@ -27,7 +28,9 @@ const (
)
func init() {
serverlogger.InitFromConfig(config.LoggingConfig{Level: "debug"})
serverlogger.InitFromConfig(config.LoggingConfig{
Config: logger.Config{Level: "debug"},
})
}
var iceServersForRoom = []*livekit.ICEServer{{Urls: []string{"stun:stun.l.google.com:19302"}}}
+1 -1
View File
@@ -124,7 +124,7 @@ func Recover() {
default:
err = errors.New("unknown panic")
}
logger.GetLogger().Error(err, "recovered panic", "panic", r)
logger.Errorw("recovered panic", err, "panic", r)
}
}
+2 -2
View File
@@ -392,7 +392,7 @@ func (r *RoomManager) rtcSessionWorker(room *rtc.Room, participant types.LocalPa
defer rtc.Recover()
pLogger := rtc.LoggerWithParticipant(
rtc.LoggerWithRoom(logger.Logger(logger.GetLogger()), room.Name(), room.ID()),
rtc.LoggerWithRoom(logger.GetDefaultLogger(), room.Name(), room.ID()),
participant.Identity(), participant.ID(),
)
@@ -448,7 +448,7 @@ func (r *RoomManager) handleRTCMessage(_ context.Context, roomName livekit.RoomN
sid = participant.ID()
}
pLogger := rtc.LoggerWithParticipant(
rtc.LoggerWithRoom(logger.Logger(logger.GetLogger()), roomName, room.ID()),
rtc.LoggerWithRoom(logger.GetDefaultLogger(), roomName, room.ID()),
identity,
sid,
)
+1 -1
View File
@@ -182,7 +182,7 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
pLogger := rtc.LoggerWithParticipant(
rtc.LoggerWithRoom(logger.Logger(logger.GetLogger()), roomName, ""),
rtc.LoggerWithRoom(logger.GetDefaultLogger(), roomName, ""),
pi.Identity, "",
)
done := make(chan struct{})
+3 -3
View File
@@ -99,14 +99,14 @@ type Options struct {
// NewBuffer constructs a new Buffer
func NewBuffer(ssrc uint32, vp, ap *sync.Pool) *Buffer {
logger := logger.Logger(logger.GetLogger()) // will be reset with correct context via SetLogger
l := logger.GetDefaultLogger() // will be reset with correct context via SetLogger
b := &Buffer{
mediaSSRC: ssrc,
videoPool: vp,
audioPool: ap,
pliThrottle: int64(500 * time.Millisecond),
logger: logger,
callbacksQueue: utils.NewOpsQueue(logger, "sfu-buffer", 50),
logger: l,
callbacksQueue: utils.NewOpsQueue(l, "sfu-buffer", 50),
}
b.extPackets.SetMinCapacity(7)
return b
+1 -1
View File
@@ -19,7 +19,7 @@ func disable(f *Forwarder) {
}
func newForwarder(codec webrtc.RTPCodecCapability, kind webrtc.RTPCodecType) *Forwarder {
return NewForwarder(codec, kind, logger.Logger(logger.GetLogger()))
return NewForwarder(codec, kind, logger.GetDefaultLogger())
}
+1 -1
View File
@@ -12,7 +12,7 @@ import (
)
func newRTPMunger() *RTPMunger {
return NewRTPMunger(logger.Logger(logger.GetLogger()))
return NewRTPMunger(logger.GetDefaultLogger())
}
func TestSetLastSnTs(t *testing.T) {
+2 -2
View File
@@ -13,7 +13,7 @@ import (
)
func Test_sequencer(t *testing.T) {
seq := newSequencer(500, logger.Logger(logger.GetLogger()))
seq := newSequencer(500, logger.GetDefaultLogger())
off := uint16(15)
for i := uint16(1); i < 518; i++ {
@@ -82,7 +82,7 @@ func Test_sequencer_getNACKSeqNo(t *testing.T) {
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
n := newSequencer(500, logger.Logger(logger.GetLogger()))
n := newSequencer(500, logger.GetDefaultLogger())
for _, i := range tt.fields.input {
n.push(i, i+tt.fields.offset, 123, 3)
+1 -1
View File
@@ -17,7 +17,7 @@ func newStreamTracker(samplesRequired uint32, cyclesRequired uint32, cycleDurati
SamplesRequired: samplesRequired,
CyclesRequired: cyclesRequired,
CycleDuration: cycleDuration,
Logger: logger.Logger(logger.GetLogger()),
Logger: logger.GetDefaultLogger(),
})
}
+1 -1
View File
@@ -27,7 +27,7 @@ func compare(expected *VP8Munger, actual *VP8Munger) bool {
}
func newVP8Munger() *VP8Munger {
return NewVP8Munger(logger.Logger(logger.GetLogger()))
return NewVP8Munger(logger.GetDefaultLogger())
}
func TestSetLast(t *testing.T) {
+1 -1
View File
@@ -45,7 +45,7 @@ const jobQueueBufferSize = 10000
func NewTelemetryService(notifier webhook.Notifier, analytics AnalyticsService) TelemetryService {
t := &telemetryService{
internalService: NewTelemetryServiceInternal(notifier, analytics),
jobQueue: utils.NewOpsQueue(logger.Logger(logger.GetLogger()), "telemetry", jobQueueBufferSize),
jobQueue: utils.NewOpsQueue(logger.GetDefaultLogger(), "telemetry", jobQueueBufferSize),
}
t.jobQueue.Start()