Skip to content

Commit

Permalink
Remove unnecessary intermediate loggers (#1969)
Browse files Browse the repository at this point in the history
* Remove unnecessary intermediate loggers

* Make linter happy

* Collapse logging variable into if contexts
  • Loading branch information
markusthoemmes authored Dec 16, 2020
1 parent 95b8793 commit 261c9b4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
24 changes: 17 additions & 7 deletions controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/google/uuid"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -265,9 +266,12 @@ func (c *Impl) EnqueueAfter(obj interface{}, after time.Duration) {
// and enqueues that key in the slow lane.
func (c *Impl) EnqueueSlowKey(key types.NamespacedName) {
c.workQueue.SlowLane().Add(key)
c.logger.With(zap.String(logkey.Key, key.String())).
Debugf("Adding to the slow queue %s (depth(total/slow): %d/%d)",
safeKey(key), c.workQueue.Len(), c.workQueue.SlowLane().Len())

if logger := c.logger.Desugar(); logger.Core().Enabled(zapcore.DebugLevel) {
logger.Debug(fmt.Sprintf("Adding to the slow queue %s (depth(total/slow): %d/%d)",
safeKey(key), c.workQueue.Len(), c.workQueue.SlowLane().Len()),
zap.String(logkey.Key, key.String()))
}
}

// EnqueueSlow extracts namespaced name from the object and enqueues it on the slow
Expand Down Expand Up @@ -393,8 +397,11 @@ func (c *Impl) EnqueueNamespaceOf(obj interface{}) {
// EnqueueKey takes a namespace/name string and puts it onto the work queue.
func (c *Impl) EnqueueKey(key types.NamespacedName) {
c.workQueue.Add(key)
c.logger.With(zap.String(logkey.Key, key.String())).
Debugf("Adding to queue %s (depth: %d)", safeKey(key), c.workQueue.Len())

if logger := c.logger.Desugar(); logger.Core().Enabled(zapcore.DebugLevel) {
logger.Debug(fmt.Sprintf("Adding to queue %s (depth: %d)", safeKey(key), c.workQueue.Len()),
zap.String(logkey.Key, key.String()))
}
}

// MaybeEnqueueBucketKey takes a Bucket and namespace/name string and puts it onto
Expand All @@ -409,8 +416,11 @@ func (c *Impl) MaybeEnqueueBucketKey(bkt reconciler.Bucket, key types.Namespaced
// the work queue after given delay.
func (c *Impl) EnqueueKeyAfter(key types.NamespacedName, delay time.Duration) {
c.workQueue.AddAfter(key, delay)
c.logger.With(zap.String(logkey.Key, key.String())).
Debugf("Adding to queue %s (delay: %v, depth: %d)", safeKey(key), delay, c.workQueue.Len())

if logger := c.logger.Desugar(); logger.Core().Enabled(zapcore.DebugLevel) {
logger.Debug(fmt.Sprintf("Adding to queue %s (delay: %v, depth: %d)", safeKey(key), delay, c.workQueue.Len()),
zap.String(logkey.Key, key.String()))
}
}

// RunContext starts the controller's worker threads, the number of which is threadiness.
Expand Down
4 changes: 2 additions & 2 deletions logging/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ func UpdateLevelFromConfigMap(logger *zap.SugaredLogger, atomicLevel zap.AtomicL
case errors.Is(err, errEmptyLoggerConfig):
level = zap.NewAtomicLevel().Level()
case err != nil:
logger.With(zap.Error(err)).Errorf("Failed to parse logger configuration. "+
"Previous log level retained for %v", levelKey)
logger.Errorw("Failed to parse logger configuration. Previous log level retained for "+levelKey,
zap.Error(err))
return
default:
level = loggingCfg.Level.Level()
Expand Down
3 changes: 2 additions & 1 deletion websocket/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"bytes"
"encoding/gob"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http/httputil"
Expand Down Expand Up @@ -165,7 +166,7 @@ func NewDurableConnection(target string, messageChan chan []byte, logger *zap.Su
}
logger.Debug("Connected to ", target)
if err := c.keepalive(); err != nil {
logger.With(zap.Error(err)).Errorf("Connection to %s broke down, reconnecting...", target)
logger.Errorw(fmt.Sprintf("Connection to %s broke down, reconnecting...", target), zap.Error(err))
}
if err := c.closeConnection(); err != nil {
logger.Errorw("Failed to close the connection after crashing", zap.Error(err))
Expand Down

0 comments on commit 261c9b4

Please sign in to comment.