-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
unify logging to logrus in calico-apiserver (#9600)
1. change all logging statements inside calico-apiserver code base to use logrus. used within calico-apiserver will log with logrus. 3. fix some log levels to better reflect of the log severity.
- Loading branch information
Showing
15 changed files
with
299 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package server | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strconv" | ||
|
||
"github.com/go-logr/logr" | ||
"github.com/sirupsen/logrus" | ||
"k8s.io/component-base/logs" | ||
"k8s.io/klog/v2" | ||
|
||
"github.com/projectcalico/calico/libcalico-go/lib/logutils" | ||
) | ||
|
||
// logrusLevel sets LOG_LEVEL and if not defined will set it based on klog verbosity | ||
// v=0 --> ERROR | ||
// v=1 --> WARN | ||
// v=2 --> INFO | ||
// v=3-6 --> DEBUG | ||
// v=7-9 --> TRACE | ||
func logrusLevel() logrus.Level { | ||
if env := os.Getenv("LOG_LEVEL"); env != "" { | ||
return logutils.SafeParseLogLevel(env) | ||
} | ||
if klog.V(0).Enabled() && !klog.V(1).Enabled() { | ||
return logrus.ErrorLevel | ||
} | ||
if klog.V(1).Enabled() && !klog.V(2).Enabled() { | ||
return logrus.WarnLevel | ||
} | ||
if klog.V(2).Enabled() && !klog.V(3).Enabled() { | ||
return logrus.InfoLevel | ||
} | ||
if (klog.V(3).Enabled() || klog.V(4).Enabled() || klog.V(5).Enabled() || klog.V(6).Enabled()) && !klog.V(7).Enabled() { | ||
return logrus.DebugLevel | ||
} | ||
|
||
// klog.V(7).Enabled() || klog.V(8).Enabled() || klog.V(9).Enabled() | ||
return logrus.TraceLevel | ||
} | ||
|
||
// verbosityLevel sets VERBOSITY based on LOG_LEVEL | ||
// ERROR, FATAL --> v=0 | ||
// WARN --> v=1 | ||
// INFO --> v=2 | ||
// DEBUG --> v=3:6 | ||
// TRACE --> v=7 and above (i.e., show everything) | ||
func logLevelToVerbosityLevel() string { | ||
switch logrus.GetLevel() { | ||
case logrus.TraceLevel: | ||
return "20" | ||
case logrus.DebugLevel: | ||
return "6" | ||
case logrus.InfoLevel: | ||
return "2" | ||
case logrus.WarnLevel: | ||
return "1" | ||
case logrus.ErrorLevel: | ||
return "0" | ||
case logrus.FatalLevel: | ||
return "0" | ||
} | ||
return "2" // return default "2" for info. | ||
} | ||
|
||
// CustomLogger is a wrapper around logrus to forward klog messages | ||
type logrusKlog struct { | ||
logger *logrus.Entry | ||
} | ||
|
||
func (l logrusKlog) Init(info logr.RuntimeInfo) { | ||
l.logger.Logger.SetLevel(logrusLevel()) | ||
} | ||
|
||
func (l logrusKlog) Enabled(level int) bool { | ||
l.logger.Trace("loglevel: ", logLevelToVerbosityLevel()) | ||
logLevel, err := strconv.ParseInt(logLevelToVerbosityLevel(), 10, 64) | ||
if err != nil { | ||
l.logger.Error("failed to parseInt log level verbosity: ", logLevelToVerbosityLevel()) | ||
return false | ||
} | ||
return int64(level) == logLevel | ||
} | ||
|
||
func toLogrusFields(keysAndValues ...interface{}) logrus.Fields { | ||
fields := logrus.Fields{} | ||
for i := 0; i < len(keysAndValues); i += 2 { | ||
key := fmt.Sprintf("%v", keysAndValues[i]) | ||
fields[key] = keysAndValues[i+1] | ||
} | ||
return fields | ||
} | ||
|
||
func (l logrusKlog) Info(level int, msg string, keysAndValues ...any) { | ||
l.logger.WithFields(toLogrusFields(keysAndValues...)).Info(msg) | ||
} | ||
|
||
func (l logrusKlog) Error(err error, msg string, keysAndValues ...any) { | ||
l.logger.WithFields(toLogrusFields(keysAndValues...)).WithError(err).Error(msg) | ||
} | ||
|
||
func (l logrusKlog) WithName(name string) logr.LogSink { | ||
// do nothing | ||
return l | ||
} | ||
|
||
func (l logrusKlog) WithValues(keysAndValues ...any) logr.LogSink { | ||
// do nothing | ||
return l | ||
} | ||
|
||
func configureLogging() { | ||
// set logrus log-level for tigera-apiserver logging | ||
logrus.SetLevel(logrusLevel()) | ||
|
||
// create a logrus logger to be used by klog | ||
logrusLogger := logrus.New().WithField("klog-logger", "tigera-apiserver") | ||
logrusLogger.Logger.SetLevel(logrusLevel()) | ||
|
||
logrusLoggerWrapper := logrusKlog{logger: logrusLogger} | ||
|
||
klogLogger := logr.New(logrusLoggerWrapper) | ||
klog.SetLogger(klogLogger) | ||
|
||
// set klog verbosity for libraries used in tigera-apiserver | ||
msg, err := logs.GlogSetter(logLevelToVerbosityLevel()) | ||
if err != nil { | ||
logrus.Errorf("Failed to set glog setter: %v", err) | ||
} | ||
if err == nil && msg != "" { | ||
logrus.Tracef("Successfully set glog setter: %s", msg) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package server | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"github.com/sirupsen/logrus" | ||
"k8s.io/component-base/logs" | ||
) | ||
|
||
func TestLogLevel(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Logging Suite") | ||
} | ||
|
||
var _ = Describe("", func() { | ||
It("test logrus log level calculated with different verbosity values", func() { | ||
_, err := logs.GlogSetter("0") | ||
Expect(logrusLevel()).To(Equal(logrus.ErrorLevel)) | ||
Expect(err).To(BeNil()) | ||
|
||
_, err = logs.GlogSetter("1") | ||
Expect(logrusLevel()).To(Equal(logrus.WarnLevel)) | ||
Expect(err).To(BeNil()) | ||
|
||
_, err = logs.GlogSetter("2") | ||
Expect(logrusLevel()).To(Equal(logrus.InfoLevel)) | ||
Expect(err).To(BeNil()) | ||
|
||
_, err = logs.GlogSetter("3") | ||
Expect(logrusLevel()).To(Equal(logrus.DebugLevel)) | ||
Expect(err).To(BeNil()) | ||
|
||
_, err = logs.GlogSetter("4") | ||
Expect(logrusLevel()).To(Equal(logrus.DebugLevel)) | ||
Expect(err).To(BeNil()) | ||
|
||
_, err = logs.GlogSetter("5") | ||
Expect(logrusLevel()).To(Equal(logrus.DebugLevel)) | ||
Expect(err).To(BeNil()) | ||
|
||
_, err = logs.GlogSetter("6") | ||
Expect(logrusLevel()).To(Equal(logrus.DebugLevel)) | ||
Expect(err).To(BeNil()) | ||
|
||
_, err = logs.GlogSetter("7") | ||
Expect(logrusLevel()).To(Equal(logrus.TraceLevel)) | ||
Expect(err).To(BeNil()) | ||
|
||
_, err = logs.GlogSetter("8") | ||
Expect(logrusLevel()).To(Equal(logrus.TraceLevel)) | ||
Expect(err).To(BeNil()) | ||
|
||
_, err = logs.GlogSetter("9") | ||
Expect(logrusLevel()).To(Equal(logrus.TraceLevel)) | ||
Expect(err).To(BeNil()) | ||
}) | ||
|
||
It("test verbosity values calculated from various logrus log level", func() { | ||
logrus.SetLevel(logrus.TraceLevel) | ||
Expect(logLevelToVerbosityLevel()).To(Equal("20")) | ||
|
||
logrus.SetLevel(logrus.DebugLevel) | ||
Expect(logLevelToVerbosityLevel()).To(Equal("6")) | ||
|
||
logrus.SetLevel(logrus.InfoLevel) | ||
Expect(logLevelToVerbosityLevel()).To(Equal("2")) | ||
|
||
logrus.SetLevel(logrus.WarnLevel) | ||
Expect(logLevelToVerbosityLevel()).To(Equal("1")) | ||
|
||
logrus.SetLevel(logrus.ErrorLevel) | ||
Expect(logLevelToVerbosityLevel()).To(Equal("0")) | ||
|
||
logrus.SetLevel(logrus.FatalLevel) | ||
Expect(logLevelToVerbosityLevel()).To(Equal("0")) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.