klogutil

package
v0.0.65 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 14, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromContext added in v0.0.64

func FromContext(ctx context.Context) klog.Logger

FromContext returns the logger associated with ctx, enriched so that the OpenTelemetry log bridge (otellogr) can extract the active trace span.

logr.LogSink.Info/Error have no context.Context parameter, so klog.FromContext(ctx).Info(...) discards the span carried by ctx. Calling WithValues("ctx", ctx) stores the context inside the otellogr sink (via its convertKVs path), which then passes it to Emit — giving the OTel SDK the span context it needs for trace-log correlation.

The context is wrapped in otelCtx so that text loggers that don't strip context values (e.g. in tests without the teeSink) produce a harmless placeholder instead of formatting the full context chain, which could contain sensitive data such as HTTP request headers.

func LogInfo

func LogInfo(logger klog.Logger, msg string, attrs ...Attr)

LogInfo logs msg with the provided attributes on the given logger. Pass a V-leveled logger (e.g. logger.V(4)) to preserve verbosity gating.

func LogWarn

func LogWarn(logger klog.Logger, msg string, attrs ...Attr)

LogWarn logs msg with a log.severity=WARN tag and the provided attributes. This is done because the klog.Logger interface has no native Warn method. Pass a V-leveled logger (e.g. logger.V(1)) to preserve verbosity gating.

func SetOtelLogSinkActive added in v0.0.64

func SetOtelLogSinkActive(active bool)

SetOtelLogSinkActive records whether the OTel log bridge is active. When false, FromContext skips the WithValues("ctx", …) call, avoiding a per-call allocation and keeping the text log output free of the placeholder.

Types

type Attr

type Attr struct {
	K string
	V any
}

Attr is a single, complete key/value log attribute. Because every constructor returns a complete pair, attributes compose through the variadic logr/klog logging API without the "even number of varargs" footgun: keys and values cannot get out of sync.

func Err

func Err(err error) Attr

Err returns an Attr carrying the error message under the OpenTelemetry semantic-convention key "exception.message". It centralizes that key so there is a single source of truth across every error log site. A nil error yields an empty message rather than panicking.

func Field

func Field(k string, v any) Attr

Field returns an Attr for an arbitrary key/value pair.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL