Skip to content

Commit

Permalink
Log according to sig-instrumentation guidelines
Browse files Browse the repository at this point in the history
  • Loading branch information
vincepri committed Oct 18, 2023
1 parent b90810b commit 4f6b6ec
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
2 changes: 1 addition & 1 deletion controllers/awsmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ func (r *AWSMachineReconciler) reconcileDelete(machineScope *scope.MachineScope,
// and AWSMachine
// 3. Issue a delete
// 4. Scale controller deployment to 1
machineScope.Debug("Unable to locate EC2 instance by ID or tags")
machineScope.Warn("Unable to locate EC2 instance by ID or tags")
r.Recorder.Eventf(machineScope.AWSMachine, corev1.EventTypeWarning, "NoInstanceFound", "Unable to find matching EC2 instance")
controllerutil.RemoveFinalizer(machineScope.AWSMachine, infrav1.MachineFinalizer)
return ctrl.Result{}, nil
Expand Down
2 changes: 1 addition & 1 deletion exp/controllers/awsmachinepool_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ func (r *AWSMachinePoolReconciler) reconcileDelete(machinePoolScope *scope.Machi
}

if asg == nil {
machinePoolScope.Debug("Unable to locate ASG")
machinePoolScope.Warn("Unable to locate ASG")
r.Recorder.Eventf(machinePoolScope.AWSMachinePool, corev1.EventTypeNormal, expinfrav1.ASGNotFoundReason, "Unable to find matching ASG")
} else {
machinePoolScope.SetASGStatus(asg.Status)
Expand Down
25 changes: 18 additions & 7 deletions pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ import (
"github.com/go-logr/logr"
)

// These are the log levels used by the logger.
// See https://github.com/kubernetes/community/blob/master/contributors/devel/sig-instrumentation/logging.md#what-method-to-use
const (
logLevelDebug = 2
logLevelWarn = 3
logLevelTrace = 4
logLevelWarn = 1
logLevelDebug = 4
logLevelTrace = 5
)

// Wrapper defines a convenient interface to use to log things.
Expand All @@ -43,43 +45,52 @@ type Wrapper interface {

// Logger is a concrete logger using logr underneath.
type Logger struct {
logger logr.Logger
callStackHelper func()
logger logr.Logger
}

// NewLogger creates a logger with a passed in logr.Logger implementation directly.
func NewLogger(log logr.Logger) *Logger {
helper, log := log.WithCallStackHelper()
return &Logger{
logger: log,
callStackHelper: helper,
logger: log,
}
}

// FromContext retrieves the logr implementation from Context and uses it as underlying logger.
func FromContext(ctx context.Context) *Logger {
log := logr.FromContextOrDiscard(ctx)
helper, log := logr.FromContextOrDiscard(ctx).WithCallStackHelper()
return &Logger{
logger: log,
callStackHelper: helper,
logger: log,
}
}

var _ Wrapper = &Logger{}

func (c *Logger) Info(msg string, keysAndValues ...any) {
c.callStackHelper()
c.logger.Info(msg, keysAndValues...)
}

func (c *Logger) Debug(msg string, keysAndValues ...any) {
c.callStackHelper()
c.logger.V(logLevelDebug).Info(msg, keysAndValues...)
}

func (c *Logger) Warn(msg string, keysAndValues ...any) {
c.callStackHelper()
c.logger.V(logLevelWarn).Info(msg, keysAndValues...)
}

func (c *Logger) Trace(msg string, keysAndValues ...any) {
c.callStackHelper()
c.logger.V(logLevelTrace).Info(msg, keysAndValues...)
}

func (c *Logger) Error(err error, msg string, keysAndValues ...any) {
c.callStackHelper()
c.logger.Error(err, msg, keysAndValues...)
}

Expand Down

0 comments on commit 4f6b6ec

Please sign in to comment.