[client] Use the netbird logger for ice and grpc (#3603)

updates the logging implementation to use the netbird logger for both ICE and gRPC components. The key changes include:

- Introducing a gRPC logger configuration in util/log.go that integrates with the netbird logging setup.
- Updating the log hook in formatter/hook/hook.go to ensure a default caller is used when not set.
- Refactoring ICE agent and UDP multiplexers to use a unified logger via the new getLogger() method.
This commit is contained in:
Maycon Santos
2025-04-04 18:30:47 +02:00
committed by GitHub
parent 80702b9323
commit fbd783ad58
5 changed files with 40 additions and 4 deletions

View File

@ -150,7 +150,7 @@ func isZeros(ip net.IP) bool {
// NewUDPMuxDefault creates an implementation of UDPMux // NewUDPMuxDefault creates an implementation of UDPMux
func NewUDPMuxDefault(params UDPMuxParams) *UDPMuxDefault { func NewUDPMuxDefault(params UDPMuxParams) *UDPMuxDefault {
if params.Logger == nil { if params.Logger == nil {
params.Logger = logging.NewDefaultLoggerFactory().NewLogger("ice") params.Logger = getLogger()
} }
mux := &UDPMuxDefault{ mux := &UDPMuxDefault{
@ -455,3 +455,9 @@ func newBufferHolder(size int) *bufferHolder {
buf: make([]byte, size), buf: make([]byte, size),
} }
} }
func getLogger() logging.LeveledLogger {
fac := logging.NewDefaultLoggerFactory()
fac.Writer = log.StandardLogger().Writer()
return fac.NewLogger("ice")
}

View File

@ -49,7 +49,7 @@ type UniversalUDPMuxParams struct {
// NewUniversalUDPMuxDefault creates an implementation of UniversalUDPMux embedding UDPMux // NewUniversalUDPMuxDefault creates an implementation of UniversalUDPMux embedding UDPMux
func NewUniversalUDPMuxDefault(params UniversalUDPMuxParams) *UniversalUDPMuxDefault { func NewUniversalUDPMuxDefault(params UniversalUDPMuxParams) *UniversalUDPMuxDefault {
if params.Logger == nil { if params.Logger == nil {
params.Logger = logging.NewDefaultLoggerFactory().NewLogger("ice") params.Logger = getLogger()
} }
if params.XORMappedAddrCacheTTL == 0 { if params.XORMappedAddrCacheTTL == 0 {
params.XORMappedAddrCacheTTL = time.Second * 25 params.XORMappedAddrCacheTTL = time.Second * 25

View File

@ -4,6 +4,7 @@ import (
"time" "time"
"github.com/pion/ice/v3" "github.com/pion/ice/v3"
"github.com/pion/logging"
"github.com/pion/randutil" "github.com/pion/randutil"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -35,6 +36,9 @@ func NewAgent(iFaceDiscover stdnet.ExternalIFaceDiscover, config Config, candida
log.Errorf("failed to create pion's stdnet: %s", err) log.Errorf("failed to create pion's stdnet: %s", err)
} }
fac := logging.NewDefaultLoggerFactory()
fac.Writer = log.StandardLogger().Writer()
agentConfig := &ice.AgentConfig{ agentConfig := &ice.AgentConfig{
MulticastDNSMode: ice.MulticastDNSModeDisabled, MulticastDNSMode: ice.MulticastDNSModeDisabled,
NetworkTypes: []ice.NetworkType{ice.NetworkTypeUDP4, ice.NetworkTypeUDP6}, NetworkTypes: []ice.NetworkType{ice.NetworkTypeUDP4, ice.NetworkTypeUDP6},
@ -51,6 +55,7 @@ func NewAgent(iFaceDiscover stdnet.ExternalIFaceDiscover, config Config, candida
RelayAcceptanceMinWait: &iceRelayAcceptanceMinWait, RelayAcceptanceMinWait: &iceRelayAcceptanceMinWait,
LocalUfrag: ufrag, LocalUfrag: ufrag,
LocalPwd: pwd, LocalPwd: pwd,
LoggerFactory: fac,
} }
if config.DisableIPv6Discovery { if config.DisableIPv6Discovery {

View File

@ -3,6 +3,7 @@ package hook
import ( import (
"fmt" "fmt"
"path" "path"
"runtime"
"runtime/debug" "runtime/debug"
"strings" "strings"
@ -40,8 +41,12 @@ func (hook ContextHook) Levels() []logrus.Level {
// Fire extend with the source information the entry.Data // Fire extend with the source information the entry.Data
func (hook ContextHook) Fire(entry *logrus.Entry) error { func (hook ContextHook) Fire(entry *logrus.Entry) error {
src := hook.parseSrc(entry.Caller.File) caller := &runtime.Frame{Line: 0, File: "caller_not_available"}
entry.Data[EntryKeySource] = fmt.Sprintf("%s:%v", src, entry.Caller.Line) if entry.Caller != nil {
caller = entry.Caller
}
src := hook.parseSrc(caller.File)
entry.Data[EntryKeySource] = fmt.Sprintf("%s:%v", src, caller.Line)
additionalEntries(entry) additionalEntries(entry)
if entry.Context == nil { if entry.Context == nil {

View File

@ -8,6 +8,7 @@ import (
"strconv" "strconv"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"google.golang.org/grpc/grpclog"
"gopkg.in/natefinch/lumberjack.v2" "gopkg.in/natefinch/lumberjack.v2"
"github.com/netbirdio/netbird/formatter" "github.com/netbirdio/netbird/formatter"
@ -48,9 +49,28 @@ func InitLog(logLevel string, logPath string) error {
formatter.SetTextFormatter(log.StandardLogger()) formatter.SetTextFormatter(log.StandardLogger())
} }
log.SetLevel(level) log.SetLevel(level)
setGRPCLibLogger()
return nil return nil
} }
func setGRPCLibLogger() {
logOut := log.StandardLogger().Writer()
if os.Getenv("GRPC_GO_LOG_SEVERITY_LEVEL") != "info" {
grpclog.SetLoggerV2(grpclog.NewLoggerV2(io.Discard, logOut, logOut))
return
}
var v int
vLevel := os.Getenv("GRPC_GO_LOG_VERBOSITY_LEVEL")
if vl, err := strconv.Atoi(vLevel); err == nil {
v = vl
}
grpclog.SetLoggerV2(grpclog.NewLoggerV2WithVerbosity(logOut, logOut, logOut, v))
}
func getLogMaxSize() int { func getLogMaxSize() int {
if sizeVar, ok := os.LookupEnv("NB_LOG_MAX_SIZE_MB"); ok { if sizeVar, ok := os.LookupEnv("NB_LOG_MAX_SIZE_MB"); ok {
size, err := strconv.ParseInt(sizeVar, 10, 64) size, err := strconv.ParseInt(sizeVar, 10, 64)