-
Notifications
You must be signed in to change notification settings - Fork 350
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dataclients/kubernetes: improve logging (#2637)
* log number of received routegroups * log distinct messages once per ingress/routegroup * log number or all routes loaded and mapped Signed-off-by: Alexander Yastrebov <[email protected]>
- Loading branch information
1 parent
6e304f6
commit 6b332ac
Showing
7 changed files
with
182 additions
and
82 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,62 @@ | ||
package kubernetes | ||
|
||
import log "github.com/sirupsen/logrus" | ||
import ( | ||
"fmt" | ||
"sync" | ||
|
||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
type logger struct { | ||
logger *log.Entry | ||
|
||
mu sync.Mutex | ||
history map[string]struct{} | ||
} | ||
|
||
// newLogger creates a logger that logs each unique message once | ||
// for the resource identified by kind, namespace and name. | ||
// It logs nothing when disabled. | ||
func newLogger(kind, namespace, name string, enabled bool) *logger { | ||
if !enabled { | ||
return nil | ||
} | ||
return &logger{log.WithFields(log.Fields{"kind": kind, "ns": namespace, "name": name})} | ||
return &logger{logger: log.WithFields(log.Fields{"kind": kind, "ns": namespace, "name": name})} | ||
} | ||
|
||
func (l *logger) Debugf(format string, args ...any) { | ||
if l != nil { | ||
l.logger.Debugf(format, args...) | ||
l.once(log.DebugLevel, format, args...) | ||
} | ||
} | ||
|
||
func (l *logger) Infof(format string, args ...any) { | ||
if l != nil { | ||
l.logger.Infof(format, args...) | ||
l.once(log.InfoLevel, format, args...) | ||
} | ||
} | ||
|
||
func (l *logger) Errorf(format string, args ...any) { | ||
if l != nil { | ||
l.logger.Errorf(format, args...) | ||
l.once(log.ErrorLevel, format, args...) | ||
} | ||
} | ||
|
||
func (l *logger) once(level log.Level, format string, args ...any) { | ||
if !l.logger.Logger.IsLevelEnabled(level) { | ||
return | ||
} | ||
l.mu.Lock() | ||
defer l.mu.Unlock() | ||
|
||
if l.history == nil { | ||
l.history = make(map[string]struct{}) | ||
} | ||
|
||
msg := fmt.Sprintf(format, args...) | ||
key := fmt.Sprintf("%s %s", level, msg) | ||
if _, ok := l.history[key]; !ok { | ||
l.logger.Log(level, msg) | ||
l.history[key] = struct{}{} | ||
} | ||
} |
Oops, something went wrong.