github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/internal/controllers/sink.go (about)

     1  package controllers
     2  
     3  import (
     4  	"context"
     5  	"strings"
     6  
     7  	"github.com/go-logr/logr"
     8  	"github.com/go-logr/logr/funcr"
     9  	"k8s.io/apiserver/pkg/registry/generic/registry"
    10  
    11  	"github.com/tilt-dev/tilt/pkg/logger"
    12  )
    13  
    14  // loosely adapted from funcr.Logger
    15  type logSink struct {
    16  	funcr.Formatter
    17  
    18  	ctx    context.Context
    19  	logger logger.Logger
    20  }
    21  
    22  func (l logSink) WithName(name string) logr.LogSink {
    23  	l.Formatter.AddName(name)
    24  	return &l
    25  }
    26  
    27  func (l logSink) WithValues(kvList ...interface{}) logr.LogSink {
    28  	l.Formatter.AddValues(kvList)
    29  	return &l
    30  }
    31  
    32  func (l logSink) WithCallDepth(depth int) logr.LogSink {
    33  	l.Formatter.AddCallDepth(depth)
    34  	return &l
    35  }
    36  
    37  func (l logSink) Info(level int, msg string, kvList ...interface{}) {
    38  	if l.ctx.Err() != nil {
    39  		return // Stop logging when the context is cancelled.
    40  	}
    41  
    42  	// We don't care about the startup or teardown sequence.
    43  	if msg == "Starting EventSource" ||
    44  		msg == "Starting Controller" ||
    45  		msg == "Starting workers" ||
    46  		msg == "error received after stop sequence was engaged" ||
    47  		msg == "Shutdown signal received, waiting for all workers to finish" ||
    48  		msg == "All workers finished" {
    49  		return
    50  	}
    51  
    52  	prefix, args := l.FormatInfo(level, msg, kvList)
    53  
    54  	// V(3) was picked because while controller-runtime is a bit chatty at
    55  	// startup, once steady state is reached, most of the logging is generally
    56  	// useful.
    57  	if level < 3 {
    58  		l.logger.Debugf("[%s] %s", prefix, args)
    59  		return
    60  	}
    61  
    62  	l.logger.Infof("[%s] %s", prefix, args)
    63  }
    64  
    65  func (l logSink) Error(err error, msg string, kvList ...interface{}) {
    66  	if l.ctx.Err() != nil {
    67  		return // Stop logging when the context is cancelled.
    68  	}
    69  
    70  	// It's normal for reconcilers to fail with optimistic lock errors.
    71  	// They'll just retry.
    72  	if strings.Contains(err.Error(), registry.OptimisticLockErrorMsg) {
    73  		return
    74  	}
    75  
    76  	// Print errors to the global log on all builds.
    77  	//
    78  	// TODO(nick): Once we have resource grouping, we should consider having some
    79  	// some sort of system resource that we can hide by default and print
    80  	// these kinds of apiserver problems to.
    81  	prefix, args := l.FormatError(err, msg, kvList)
    82  	l.logger.Errorf("[%s] %s", prefix, args)
    83  }