Skip to content

Commit 8a00ec4

Browse files
committed
🌱 Add ctrl.{LoggerFrom, LoggerInto}
Renames the current (unreleased at the time of writing) LoggerFromContext to LoggerFrom, and adds a new alias named LoggerInto. From a user standpoint these new aliases are really clear and straightfoward, example: ```go func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) { log := ctrl.LoggerFrom(ctx) ``` This reads much better than ctrl.LoggerFromContext(ctx) which repeats the context. Another use case that this change solves is to enrich the logger with more values, example: ```go ctx = ctrl.LoggerInto(ctx, log.WithValues(...)) ``` allows the ctx to still be the de-facto context, and the logger for the inner reconciler function to still only accept ctx, without adding any more parameters. Signed-off-by: Vince Prignano <[email protected]>
1 parent df9195b commit 8a00ec4

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

alias.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,15 @@ var (
125125
// get any actual logging.
126126
Log = log.Log
127127

128-
// LoggerFromContext returns a logger with predefined values from a context.Context.
128+
// LoggerFrom returns a logger with predefined values from a context.Context.
129129
//
130130
// This is meant to be used with the context supplied in a struct that satisfies the Reconciler interface.
131-
LoggerFromContext = log.FromContext
131+
LoggerFrom = log.FromContext
132+
133+
// LoggerInto takes a context and sets the logger as one of its keys.
134+
//
135+
// This is meant to be used in reconcilers to enrich the logger within a context with additional values.
136+
LoggerInto = log.IntoContext
132137

133138
// SetLogger sets a concrete logging implementation for all deferred Loggers.
134139
SetLogger = log.SetLogger

0 commit comments

Comments
 (0)