Skip to content

Commit

Permalink
Fix log formatting on startup logs
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-innis committed Apr 19, 2023
1 parent ab4eccc commit bca7d98
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
2 changes: 0 additions & 2 deletions pkg/operator/injection/injection.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"knative.dev/pkg/logging"
"knative.dev/pkg/system"

"github.com/aws/karpenter-core/pkg/apis/settings"
Expand Down Expand Up @@ -85,7 +84,6 @@ func WithSettingsOrDie(ctx context.Context, kubernetesInterface kubernetes.Inter
cancelCtx, cancel := context.WithCancel(ctx)
defer cancel()

logging.FromContext(ctx).Debugf("waiting for configmaps")
factory := informers.NewSharedInformerFactoryWithOptions(kubernetesInterface, time.Second*30, informers.WithNamespace(system.Namespace()))
informer := factory.Core().V1().ConfigMaps().Informer()
factory.Start(cancelCtx.Done())
Expand Down
19 changes: 16 additions & 3 deletions pkg/operator/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,40 @@ package operator

import (
"context"
"log"

"github.com/go-logr/logr"
"github.com/go-logr/zapr"
"go.uber.org/zap"
"go.uber.org/zap/zapio"
"k8s.io/client-go/rest"
"k8s.io/klog/v2"
"knative.dev/pkg/configmap/informer"
"knative.dev/pkg/injection"
"knative.dev/pkg/injection/sharedmain"
"knative.dev/pkg/logging"
)

// LoggingContextOrDie injects a logger into the returned context. The logger is
// NewLogger returns a configured *zap.SugaredLogger. The logger is
// configured by the ConfigMap `config-logging` and live updates the level.
func NewLogger(ctx context.Context, componentName string, config *rest.Config, cmw *informer.InformedWatcher) *zap.SugaredLogger {
ctx, startinformers := injection.EnableInjectionOrDie(ctx, config)
ctx, startInformers := injection.EnableInjectionOrDie(logging.WithLogger(ctx, zap.S()), config)
logger, atomicLevel := sharedmain.SetupLoggerOrDie(ctx, componentName)
rest.SetDefaultWarningHandler(&logging.WarningHandler{Logger: logger})
sharedmain.WatchLoggingConfigOrDie(ctx, cmw, logger, atomicLevel, componentName)
startinformers()
startInformers()
return logger
}

// ConfigureGlobalLoggers sets up any package-wide loggers like "log" or "klog" that are utilized by other packages
// to use the configured *zap.SugaredLogger from the context
func ConfigureGlobalLoggers(ctx context.Context) {
klog.SetLogger(zapr.NewLogger(logging.FromContext(ctx).Desugar()))
w := &zapio.Writer{Log: logging.FromContext(ctx).Desugar()}
log.SetFlags(0)
log.SetOutput(w)
}

type ignoreDebugEventsSink struct {
name string
sink logr.LogSink
Expand Down
1 change: 1 addition & 0 deletions pkg/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func NewOperator() (context.Context, *Operator) {
// Logging
logger := NewLogger(ctx, component, config, configMapWatcher)
ctx = logging.WithLogger(ctx, logger)
ConfigureGlobalLoggers(ctx)

// Inject settings from the ConfigMap(s) into the context
ctx = injection.WithSettingsOrDie(ctx, kubernetesInterface, apis.Settings...)
Expand Down

0 comments on commit bca7d98

Please sign in to comment.