Improved logging configuration (#342)

This commit is contained in:
David Zhao
2022-01-15 10:28:03 -08:00
committed by GitHub
parent d45435ed3d
commit c122c98e4d
11 changed files with 139 additions and 78 deletions
+17 -15
View File
@@ -59,6 +59,11 @@ func main() {
Usage: "IP address of the current node, used to advertise to clients. Automatically determined by default", Usage: "IP address of the current node, used to advertise to clients. Automatically determined by default",
EnvVars: []string{"NODE_IP"}, EnvVars: []string{"NODE_IP"},
}, },
&cli.IntFlag{
Name: "udp-port",
Usage: "Single UDP port to use for WebRTC traffic",
EnvVars: []string{"UDP_PORT"},
},
&cli.StringFlag{ &cli.StringFlag{
Name: "redis-host", Name: "redis-host",
Usage: "host (incl. port) to redis server", Usage: "host (incl. port) to redis server",
@@ -69,6 +74,17 @@ func main() {
Usage: "password to redis", Usage: "password to redis",
EnvVars: []string{"REDIS_PASSWORD"}, EnvVars: []string{"REDIS_PASSWORD"},
}, },
&cli.StringFlag{
Name: "turn-cert",
Usage: "tls cert file for TURN server",
EnvVars: []string{"LIVEKIT_TURN_CERT"},
},
&cli.StringFlag{
Name: "turn-key",
Usage: "tls key file for TURN server",
EnvVars: []string{"LIVEKIT_TURN_KEY"},
},
// debugging flags
&cli.StringFlag{ &cli.StringFlag{
Name: "cpuprofile", Name: "cpuprofile",
Usage: "write cpu profile to `file`", Usage: "write cpu profile to `file`",
@@ -81,16 +97,6 @@ func main() {
Name: "dev", Name: "dev",
Usage: "sets log-level to debug, and console formatter", Usage: "sets log-level to debug, and console formatter",
}, },
&cli.StringFlag{
Name: "turn-cert",
Usage: "tls cert file for TURN server",
EnvVars: []string{"LIVEKIT_TURN_CERT"},
},
&cli.StringFlag{
Name: "turn-key",
Usage: "tls key file for TURN server",
EnvVars: []string{"LIVEKIT_TURN_KEY"},
},
}, },
Action: startServer, Action: startServer,
Commands: []*cli.Command{ Commands: []*cli.Command{
@@ -160,11 +166,7 @@ func startServer(c *cli.Context) error {
return err return err
} }
if conf.Development { serverlogger.InitFromConfig(conf.Logging)
serverlogger.InitDevelopment(conf.LogLevel)
} else {
serverlogger.InitProduction(conf.LogLevel)
}
if cpuProfile != "" { if cpuProfile != "" {
if f, err := os.Create(cpuProfile); err != nil { if f, err := os.Create(cpuProfile); err != nil {
+13 -3
View File
@@ -2,9 +2,6 @@
# for production setups, this port should be placed behind a load balancer with TLS # for production setups, this port should be placed behind a load balancer with TLS
port: 7880 port: 7880
# log level, valid values: debug, info, warning, error
log_level: info
# when redis is set, LiveKit will automatically operate in a fully distributed fashion # when redis is set, LiveKit will automatically operate in a fully distributed fashion
# clients could connect to any node and be routed to the same room # clients could connect to any node and be routed to the same room
redis: redis:
@@ -67,6 +64,19 @@ keys:
key1: secret1 key1: secret1
key2: secret2 key2: secret2
# Logging config
# logging:
# # log level, valid values: debug, info, warning, error
# level: info
# # log level for pion, default error
# pion_level: error
# # when set to true, emit json fields
# json: false
# # for production setups, enables sampling algorithm
# # https://github.com/uber-go/zap/blob/master/FAQ.md#why-sample-application-logs
# sample: false
# Default room config # Default room config
# Each room created will inherit these settings. If rooms are created explicitly with CreateRoom, they will take # Each room created will inherit these settings. If rooms are created explicitly with CreateRoom, they will take
# precedence over defaults # precedence over defaults
+24 -2
View File
@@ -37,8 +37,10 @@ type Config struct {
KeyFile string `yaml:"key_file"` KeyFile string `yaml:"key_file"`
Keys map[string]string `yaml:"keys"` Keys map[string]string `yaml:"keys"`
Region string `yaml:"region"` Region string `yaml:"region"`
LogLevel string `yaml:"log_level"` // LogLevel is deprecated
Limit LimitConfig `yaml:"limit"` LogLevel string `yaml:"log_level"`
Logging LoggingConfig `yaml:"logging"`
Limit LimitConfig `yaml:"limit"`
Development bool `yaml:"development"` Development bool `yaml:"development"`
} }
@@ -112,6 +114,13 @@ type CodecSpec struct {
FmtpLine string `yaml:"fmtp_line"` FmtpLine string `yaml:"fmtp_line"`
} }
type LoggingConfig struct {
JSON bool `yaml:"json"`
Level string `yaml:"level"`
Sample bool `yaml:"sample"`
PionLevel string `yaml:"pion_level"`
}
type TURNConfig struct { type TURNConfig struct {
Enabled bool `yaml:"enabled"` Enabled bool `yaml:"enabled"`
Domain string `yaml:"domain"` Domain string `yaml:"domain"`
@@ -187,6 +196,9 @@ func NewConfig(confString string, c *cli.Context) (*Config, error) {
}, },
EmptyTimeout: 5 * 60, EmptyTimeout: 5 * 60,
}, },
Logging: LoggingConfig{
PionLevel: "error",
},
TURN: TURNConfig{ TURN: TURNConfig{
Enabled: false, Enabled: false,
}, },
@@ -244,6 +256,13 @@ func NewConfig(confString string, c *cli.Context) (*Config, error) {
conf.Limit.BytesPerSec = defaultLimitBytesPerSec conf.Limit.BytesPerSec = defaultLimitBytesPerSec
} }
if conf.LogLevel != "" {
conf.Logging.Level = conf.LogLevel
}
if conf.Logging.Level == "" && conf.Development {
conf.Logging.Level = "debug"
}
return conf, nil return conf, nil
} }
@@ -281,6 +300,9 @@ func (conf *Config) updateFromCLI(c *cli.Context) error {
if c.IsSet("node-ip") { if c.IsSet("node-ip") {
conf.RTC.NodeIP = c.String("node-ip") conf.RTC.NodeIP = c.String("node-ip")
} }
if c.IsSet("udp-port") {
conf.RTC.UDPPort = uint32(c.Int("udp-port"))
}
return nil return nil
} }
@@ -1,47 +1,24 @@
package rtc package serverlogger
import ( import (
"fmt" "fmt"
"github.com/go-logr/logr" "github.com/go-logr/logr"
"github.com/pion/logging" "go.uber.org/zap/zapcore"
) )
// implements webrtc.LoggerFactory
type loggerFactory struct {
logger logr.Logger
}
func newLoggerFactory(logger logr.Logger) *loggerFactory {
if logger.GetSink() == nil {
return nil
}
return &loggerFactory{
logger: logger,
}
}
func (f *loggerFactory) NewLogger(scope string) logging.LeveledLogger {
return &logAdapter{
logger: f.logger.WithName(scope),
// treat info as debug
verbosity: 1,
}
}
// implements webrtc.LeveledLogger // implements webrtc.LeveledLogger
type logAdapter struct { type logAdapter struct {
logger logr.Logger logger logr.Logger
// 0 - most verbose level zapcore.Level
verbosity int
} }
func (l *logAdapter) Trace(msg string) { func (l *logAdapter) Trace(msg string) {
l.Tracef(msg) // ignore trace
} }
func (l *logAdapter) Tracef(format string, args ...interface{}) { func (l *logAdapter) Tracef(format string, args ...interface{}) {
l.logger.V(2 + l.verbosity).Info(fmt.Sprintf(format, args...)) // ignore trace
} }
func (l *logAdapter) Debug(msg string) { func (l *logAdapter) Debug(msg string) {
@@ -49,7 +26,10 @@ func (l *logAdapter) Debug(msg string) {
} }
func (l *logAdapter) Debugf(format string, args ...interface{}) { func (l *logAdapter) Debugf(format string, args ...interface{}) {
l.logger.V(1 + l.verbosity).Info(fmt.Sprintf(format, args...)) if l.level > zapcore.DebugLevel {
return
}
l.logger.V(1).Info(fmt.Sprintf(format, args...))
} }
func (l *logAdapter) Info(msg string) { func (l *logAdapter) Info(msg string) {
@@ -57,7 +37,10 @@ func (l *logAdapter) Info(msg string) {
} }
func (l *logAdapter) Infof(format string, args ...interface{}) { func (l *logAdapter) Infof(format string, args ...interface{}) {
l.logger.V(l.verbosity).Info(fmt.Sprintf(format, args...)) if l.level > zapcore.InfoLevel {
return
}
l.logger.Info(fmt.Sprintf(format, args...))
} }
func (l *logAdapter) Warn(msg string) { func (l *logAdapter) Warn(msg string) {
@@ -65,6 +48,9 @@ func (l *logAdapter) Warn(msg string) {
} }
func (l *logAdapter) Warnf(format string, args ...interface{}) { func (l *logAdapter) Warnf(format string, args ...interface{}) {
if l.level > zapcore.WarnLevel {
return
}
l.logger.Info(fmt.Sprintf(format, args...)) l.logger.Info(fmt.Sprintf(format, args...))
} }
@@ -73,5 +59,8 @@ func (l *logAdapter) Error(msg string) {
} }
func (l *logAdapter) Errorf(format string, args ...interface{}) { func (l *logAdapter) Errorf(format string, args ...interface{}) {
if l.level > zapcore.ErrorLevel {
return
}
l.logger.Error(nil, fmt.Sprintf(format, args...)) l.logger.Error(nil, fmt.Sprintf(format, args...))
} }
+56 -20
View File
@@ -3,6 +3,7 @@ package serverlogger
import ( import (
"github.com/go-logr/logr" "github.com/go-logr/logr"
"github.com/go-logr/zapr" "github.com/go-logr/zapr"
"github.com/livekit/livekit-server/pkg/config"
"github.com/livekit/protocol/logger" "github.com/livekit/protocol/logger"
"github.com/pion/logging" "github.com/pion/logging"
"go.uber.org/zap" "go.uber.org/zap"
@@ -13,15 +14,29 @@ import (
) )
var ( var (
// pion/webrtc, pion/turn logConfig config.LoggingConfig
defaultFactory logging.LoggerFactory pionLevel zapcore.Level
) )
func LoggerFactory() logging.LoggerFactory { // implements webrtc.LoggerFactory
if defaultFactory == nil { type LoggerFactory struct {
defaultFactory = logging.NewDefaultLoggerFactory() logger logr.Logger
}
func NewLoggerFactory(logger logr.Logger) *LoggerFactory {
if logger.GetSink() == nil {
logger = logr.Discard()
}
return &LoggerFactory{
logger: logger,
}
}
func (f *LoggerFactory) NewLogger(scope string) logging.LeveledLogger {
return &logAdapter{
logger: f.logger.WithName(scope),
level: pionLevel,
} }
return defaultFactory
} }
// Note: only pass in logr.Logger with default depth // Note: only pass in logr.Logger with default depth
@@ -31,24 +46,45 @@ func SetLogger(l logr.Logger) {
buffer.Logger = sfu.Logger buffer.Logger = sfu.Logger
} }
func InitProduction(logLevel string) { func InitFromConfig(config config.LoggingConfig) {
initLogger(zap.NewProductionConfig(), logLevel) logConfig = config
} lvl := parseLevel(config.Level)
pionLevel = parseLevel(config.PionLevel)
func InitDevelopment(logLevel string) { if lvl > pionLevel {
initLogger(zap.NewDevelopmentConfig(), logLevel) 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 // valid levels: debug, info, warn, error, fatal, panic
func initLogger(config zap.Config, level string) { func initLogger(config zap.Config) {
if level != "" {
lvl := zapcore.Level(0)
if err := lvl.UnmarshalText([]byte(level)); err == nil {
config.Level = zap.NewAtomicLevelAt(lvl)
}
}
l, _ := config.Build() l, _ := config.Build()
zapLogger := zapr.NewLogger(l) zapLogger := zapr.NewLogger(l)
SetLogger(zapLogger) SetLogger(zapLogger)
} }
func parseLevel(level string) zapcore.Level {
lvl := zapcore.InfoLevel
if level != "" {
_ = lvl.UnmarshalText([]byte(level))
}
return lvl
}
+3 -2
View File
@@ -4,12 +4,13 @@ import (
"errors" "errors"
"net" "net"
logging "github.com/livekit/livekit-server/pkg/logger"
"github.com/livekit/protocol/logger"
"github.com/pion/ice/v2" "github.com/pion/ice/v2"
"github.com/pion/sdp/v3" "github.com/pion/sdp/v3"
"github.com/pion/webrtc/v3" "github.com/pion/webrtc/v3"
"github.com/livekit/livekit-server/pkg/config" "github.com/livekit/livekit-server/pkg/config"
serverlogger "github.com/livekit/livekit-server/pkg/logger"
"github.com/livekit/livekit-server/pkg/sfu/buffer" "github.com/livekit/livekit-server/pkg/sfu/buffer"
) )
@@ -60,7 +61,7 @@ func NewWebRTCConfig(conf *config.Config, externalIP string) (*WebRTCConfig, err
SDPSemantics: webrtc.SDPSemanticsUnifiedPlan, SDPSemantics: webrtc.SDPSemanticsUnifiedPlan,
} }
s := webrtc.SettingEngine{ s := webrtc.SettingEngine{
LoggerFactory: serverlogger.LoggerFactory(), LoggerFactory: logging.NewLoggerFactory(logger.GetLogger()),
} }
s.SetLite(true) s.SetLite(true)
+2 -2
View File
@@ -215,8 +215,8 @@ func (r *Room) Join(participant types.LocalParticipant, opts *ParticipantOptions
"pID", participant.ID(), "pID", participant.ID(),
"participant", participant.Identity(), "participant", participant.Identity(),
"protocol", participant.ProtocolVersion(), "protocol", participant.ProtocolVersion(),
"room", r.Room.Name, "roomID", r.Room.Sid,
"roomID", r.Room.Sid) "options", opts)
if participant.IsRecorder() && !r.Room.ActiveRecording { if participant.IsRecorder() && !r.Room.ActiveRecording {
r.Room.ActiveRecording = true r.Room.ActiveRecording = true
+1 -1
View File
@@ -24,7 +24,7 @@ const (
) )
func init() { func init() {
serverlogger.InitDevelopment("") serverlogger.InitFromConfig(config.LoggingConfig{Level: "debug"})
} }
var iceServersForRoom = []*livekit.ICEServer{{Urls: []string{"stun:stun.l.google.com:19302"}}} var iceServersForRoom = []*livekit.ICEServer{{Urls: []string{"stun:stun.l.google.com:19302"}}}
+2 -1
View File
@@ -8,6 +8,7 @@ import (
"github.com/bep/debounce" "github.com/bep/debounce"
"github.com/go-logr/logr" "github.com/go-logr/logr"
"github.com/livekit/livekit-server/pkg/logger"
"github.com/livekit/protocol/livekit" "github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger" "github.com/livekit/protocol/logger"
"github.com/pion/interceptor" "github.com/pion/interceptor"
@@ -85,7 +86,7 @@ func newPeerConnection(params TransportParams) (*webrtc.PeerConnection, *webrtc.
se := params.Config.SettingEngine se := params.Config.SettingEngine
se.DisableMediaEngineCopy(true) se.DisableMediaEngineCopy(true)
lf := newLoggerFactory(logr.Logger(params.Logger)) lf := serverlogger.NewLoggerFactory(logr.Logger(params.Logger))
if lf != nil { if lf != nil {
se.LoggerFactory = lf se.LoggerFactory = lf
} }
+1 -1
View File
@@ -36,7 +36,7 @@ func NewTurnServer(conf *config.Config, authHandler turn.AuthHandler) (*turn.Ser
serverConfig := turn.ServerConfig{ serverConfig := turn.ServerConfig{
Realm: LivekitRealm, Realm: LivekitRealm,
AuthHandler: authHandler, AuthHandler: authHandler,
LoggerFactory: logging.LoggerFactory(), LoggerFactory: logging.NewLoggerFactory(logger.GetLogger()),
} }
relayAddrGen := &turn.RelayAddressGeneratorPortRange{ relayAddrGen := &turn.RelayAddressGeneratorPortRange{
RelayAddress: net.ParseIP(conf.RTC.NodeIP), RelayAddress: net.ParseIP(conf.RTC.NodeIP),
+1 -1
View File
@@ -43,7 +43,7 @@ var (
) )
func init() { func init() {
serverlogger.InitDevelopment("") serverlogger.InitFromConfig(config.LoggingConfig{Level: "debug"})
} }
func setupSingleNodeTest(name string, roomName string) (*service.LivekitServer, func()) { func setupSingleNodeTest(name string, roomName string) (*service.LivekitServer, func()) {