diff --git a/cns/logger/cnslogger.go b/cns/logger/cnslogger.go index 294a89c761..51c93c0e0e 100644 --- a/cns/logger/cnslogger.go +++ b/cns/logger/cnslogger.go @@ -2,12 +2,15 @@ package logger import ( "fmt" + "os" "sync" "github.com/Azure/azure-container-networking/aitelemetry" "github.com/Azure/azure-container-networking/cns/types" "github.com/Azure/azure-container-networking/log" "github.com/pkg/errors" + "go.uber.org/zap" + "go.uber.org/zap/zapcore" ) type CNSLogger struct { @@ -17,6 +20,8 @@ type CNSLogger struct { DisableMetricLogging bool DisableEventLogging bool + zapLogger *zap.Logger + m sync.RWMutex Orchestrator string NodeID string @@ -28,7 +33,20 @@ func NewCNSLogger(fileName string, logLevel, logTarget int, logDir string) (*CNS return nil, errors.Wrap(err, "could not get new logger") } - return &CNSLogger{logger: l}, nil + encoderConfig := zap.NewProductionEncoderConfig() + encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder + jsonEncoder := zapcore.NewJSONEncoder(encoderConfig) + + platformCore, err := getPlatformCores(zapcore.DebugLevel, jsonEncoder) + if err != nil { + l.Errorf("Failed to get zap Platform cores: %v", err) + } + zapLogger := zap.New(platformCore, zap.AddCaller()).With(zap.Int("pid", os.Getpid())) + + return &CNSLogger{ + logger: l, + zapLogger: zapLogger, + }, nil } func (c *CNSLogger) InitAI(aiConfig aitelemetry.AIConfig, disableTraceLogging, disableMetricLogging, disableEventLogging bool) { @@ -69,6 +87,7 @@ func (c *CNSLogger) SetContextDetails(orchestrator, nodeID string) { func (c *CNSLogger) Printf(format string, args ...any) { c.logger.Logf(format, args...) + c.zapLogger.Info(fmt.Sprintf(format, args...)) if c.th == nil || c.DisableTraceLogging { return @@ -80,6 +99,7 @@ func (c *CNSLogger) Printf(format string, args ...any) { func (c *CNSLogger) Debugf(format string, args ...any) { c.logger.Debugf(format, args...) + c.zapLogger.Debug(fmt.Sprintf(format, args...)) if c.th == nil || c.DisableTraceLogging { return @@ -91,6 +111,7 @@ func (c *CNSLogger) Debugf(format string, args ...any) { func (c *CNSLogger) Warnf(format string, args ...any) { c.logger.Warnf(format, args...) + c.zapLogger.Warn(fmt.Sprintf(format, args...)) if c.th == nil || c.DisableTraceLogging { return @@ -102,6 +123,7 @@ func (c *CNSLogger) Warnf(format string, args ...any) { func (c *CNSLogger) Errorf(format string, args ...any) { c.logger.Errorf(format, args...) + c.zapLogger.Error(fmt.Sprintf(format, args...)) if c.th == nil || c.DisableTraceLogging { return diff --git a/cns/logger/cnslogger_linux.go b/cns/logger/cnslogger_linux.go new file mode 100644 index 0000000000..8a74c61a94 --- /dev/null +++ b/cns/logger/cnslogger_linux.go @@ -0,0 +1,9 @@ +package logger + +import ( + "go.uber.org/zap/zapcore" +) + +func getPlatformCores(zapcore.Level, zapcore.Encoder) (zapcore.Core, error) { + return zapcore.NewNopCore(), nil +} diff --git a/cns/logger/cnslogger_windows.go b/cns/logger/cnslogger_windows.go new file mode 100644 index 0000000000..b909991178 --- /dev/null +++ b/cns/logger/cnslogger_windows.go @@ -0,0 +1,27 @@ +package logger + +import ( + "github.com/Azure/azure-container-networking/zapetw" + "github.com/pkg/errors" + "go.uber.org/zap/zapcore" +) + +const ( + etwCNSEventName = "AzureCNS" +) + +func getPlatformCores(loggingLevel zapcore.Level, encoder zapcore.Encoder) (zapcore.Core, error) { + etwcore, err := getETWCore(loggingLevel, encoder) + if err != nil { + return nil, errors.Wrap(err, "failed to get ETW core") + } + return etwcore, nil +} + +func getETWCore(loggingLevel zapcore.Level, encoder zapcore.Encoder) (zapcore.Core, error) { + etwcore, err := zapetw.NewETWCore(etwCNSEventName, encoder, loggingLevel) + if err != nil { + return nil, errors.Wrap(err, "failed to create ETW core") + } + return etwcore, nil +} diff --git a/cns/service/main.go b/cns/service/main.go index 5a0bac7dc0..442f8d3f75 100644 --- a/cns/service/main.go +++ b/cns/service/main.go @@ -577,7 +577,6 @@ func main() { logger.InitAI(aiConfig, ts.DisableTrace, ts.DisableMetric, ts.DisableEvent) } } - logger.Printf("[Azure CNS] Using config: %+v", cnsconfig) _, envEnableConflistGeneration := os.LookupEnv(envVarEnableCNIConflistGeneration)