1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-14 11:57:48 +00:00

fix: call depth in logging package and global logger support for call depth (#4834)

* upgrade controller-runtime dependency

Signed-off-by: damilola olayinka <holayinkajr@gmail.com>

* reset global logger in logging package

Signed-off-by: damilola olayinka <holayinkajr@gmail.com>

* add description to globallogger

Signed-off-by: damilola olayinka <holayinkajr@gmail.com>

* push only relevant changes

Signed-off-by: damilola olayinka <holayinkajr@gmail.com>

Signed-off-by: damilola olayinka <holayinkajr@gmail.com>
Co-authored-by: Charles-Edouard Brétéché <charled.breteche@gmail.com>
This commit is contained in:
yinka 2022-10-06 20:37:10 +01:00 committed by GitHub
parent 266f2d397f
commit e38f9a79a8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -22,6 +22,12 @@ const (
TextFormat = "text"
)
// Initially, globalLog comes from controller-runtime/log with logger created earlier by controller-runtime.
// When logging.Setup is called, globalLog is switched to the real logger.
// Call depth of all loggers created before logging.Setup will not work, including package level loggers as they are created before main.
// All loggers created after logging.Setup won't be subject to the call depth limitation and will work if the underlying sink supports it.
var globalLog = log.Log
func Init(flags *flag.FlagSet) {
// clear flags initialized in static dependencies
if flag.CommandLine.Lookup("log_dir") != nil {
@ -36,7 +42,7 @@ func Setup(logFormat string) error {
switch logFormat {
case TextFormat:
// in text mode we use FormatSerialize format
log.SetLogger(klogr.New())
globalLog = klogr.New()
case JSONFormat:
zapLog, err := zap.NewProduction()
if err != nil {
@ -44,16 +50,17 @@ func Setup(logFormat string) error {
}
klog.SetLogger(zapr.NewLogger(zapLog))
// in json mode we use FormatKlog format
log.SetLogger(klog.NewKlogr())
globalLog = klog.NewKlogr()
default:
return errors.New("log format not recognized, pass `text` for text mode or `json` to enable JSON logging")
}
log.SetLogger(globalLog)
return nil
}
// GlobalLogger returns a logr.Logger as configured in main.
func GlobalLogger() logr.Logger {
return log.Log
return globalLog
}
// WithName returns a new logr.Logger instance with the specified name element added to the Logger's name.
@ -73,12 +80,12 @@ func V(level int) logr.Logger {
// Info logs a non-error message with the given key/value pairs.
func Info(msg string, keysAndValues ...interface{}) {
GlobalLogger().Info(msg, keysAndValues...)
GlobalLogger().WithCallDepth(1).Info(msg, keysAndValues...)
}
// Error logs an error, with the given message and key/value pairs.
func Error(err error, msg string, keysAndValues ...interface{}) {
GlobalLogger().Error(err, msg, keysAndValues...)
GlobalLogger().WithCallDepth(1).Error(err, msg, keysAndValues...)
}
// FromContext returns a logger with predefined values from a context.Context.