logger

package
v1.12.0 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package logger provides context-scoped structured logging for Genkit.

This package wraps the standard library's log/slog package. Genkit itself logs through it, and application code inside flows and tools can use it to get the same behavior: logs flow through the process default logger, carry any attributes bound to the context's logger, and, during local development, are streamed to the Genkit Dev UI attached to the trace span that emitted them.

Usage

Log with the package-level functions, passing the context:

func myFlow(ctx context.Context, input string) (string, error) {
	logger.Info(ctx, "processing input", "size", len(input))

	result, err := process(input)
	if err != nil {
		logger.Error(ctx, "processing failed", "error", err)
		return "", err
	}

	logger.Debug(ctx, "processing complete", "resultSize", len(result))
	return result, nil
}

Passing the context is what ties a record to its surroundings: the context carries the active trace span (for Dev UI and Cloud Logging correlation) and optionally a logger with pre-bound attributes. Equivalent behavior is available from any standard logger via the *Context methods, e.g. slog.InfoContext(ctx, ...).

Log levels

The default minimum level for the console is Info, and the level only ever governs the console: during development the Dev UI receives every record at debug level and above regardless, so the terminal stays quiet while the full debug narrative lands in the trace viewer. To also see Genkit's per-request detail (action runs, model calls, tool loops) in the terminal, run with:

GENKIT_LOG_LEVEL=debug go run .

or set the level programmatically:

logger.SetLevel(slog.LevelDebug)

For an interactive CLI app whose terminal should stay pristine, run with GENKIT_LOG_LEVEL=warn to silence the startup info lines too; the Dev UI still receives everything.

SetLevel installs Genkit's console handler as the process default. Applications that configure their own slog handler should set that handler's level instead: Genkit respects a custom default handler, so both SetLevel and GENKIT_LOG_LEVEL warn and leave it alone rather than replacing it.

The Dev UI

In the dev environment (GENKIT_ENV=dev, as set by `genkit start`), Genkit tees every record at debug level and above to the Dev UI's telemetry server in addition to the console, independent of the console level. Records logged with a context are attached to the trace span active at that moment and appear in the span's Logs panel in the trace viewer. Set GENKIT_OTEL_ENABLE_LOGS=false to turn this off.

Context integration

FromContext returns the context's logger (or the process default) bound to that context, so records logged even through its plain methods (Info, Error, ...) still carry the span that was active when the logger was obtained. Store a derived logger with WithContext to bind attributes to everything logged downstream:

ctx = logger.WithContext(ctx, logger.FromContext(ctx).With("requestId", id))

The package-level logging functions use the context's logger automatically, so code below the WithContext call needs no extra plumbing for its records to carry requestId.

Additional destinations

AddHandler tees the default logger's records to another slog.Handler without disturbing console output. Genkit uses it internally for the Dev UI export; applications can use it to mirror logs to a file or a test recorder.

Package logger provides a context-scoped slog.Logger.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddHandler added in v1.12.0

func AddHandler(h slog.Handler)

AddHandler registers h as an additional destination for records logged through the default logger: every record whose level passes h's Enabled method is handed to h in addition to the current default handler. Genkit uses this in dev mode to stream logs to the Dev UI; applications can use it to mirror logs to a file or a test recorder.

The registration survives SetLevel, but a later call to slog.SetDefault replaces the composed handler entirely.

func Debug added in v1.12.0

func Debug(ctx context.Context, msg string, args ...any)

Debug logs at slog.LevelDebug using the logger in ctx.

The package-level logging functions are the preferred way to log within Genkit: they use the context's logger (with any attributes bound via WithContext) and pass ctx through to the handler, which is what lets a context-aware handler correlate the record with the active trace span.

func Error added in v1.12.0

func Error(ctx context.Context, msg string, args ...any)

Error logs at slog.LevelError using the logger in ctx. See Debug.

func FromContext

func FromContext(ctx context.Context) *slog.Logger

FromContext returns the logger carried by ctx, or the process default logger if there is none, bound to ctx: records logged even through the returned logger's context-free methods (Info, Error, ...) reach the handler carrying ctx, where those methods would otherwise hand the handler a background context. The binding is what lets a context-aware handler, such as the one that streams logs to the Dev UI, correlate such records with the span that was active when the logger was obtained. A context passed explicitly at the call site (InfoContext, Log) wins whenever it carries a span of its own.

func GetLevel added in v0.3.0

func GetLevel() slog.Level

GetLevel returns the level most recently passed to SetLevel, or slog.LevelInfo if SetLevel has not been called. slog.LevelVar is safe for concurrent use, so no lock is needed.

func HasCustomDefault added in v1.12.0

func HasCustomDefault() bool

HasCustomDefault reports whether the process-wide default logger is built on a handler the application installed itself, rather than the stdlib default handler or the managed console handler this package installs. Genkit uses it to decide whether console logging configuration (such as the GENKIT_LOG_LEVEL environment variable) is Genkit's to apply.

func Info added in v1.12.0

func Info(ctx context.Context, msg string, args ...any)

Info logs at slog.LevelInfo using the logger in ctx. See Debug.

func SetDefaultHandler added in v1.12.0

func SetDefaultHandler(h slog.Handler)

SetDefaultHandler installs h as the base handler of the process-wide default logger, replacing the current base while keeping every sink registered with AddHandler. Components that bring their own destination handler (for example a plugin that ships logs to a cloud service) should use this instead of slog.SetDefault, which would silently disconnect the registered sinks, including dev-mode streaming to the Dev UI.

func SetLevel added in v0.3.0

func SetLevel(l slog.Level)

SetLevel sets the minimum level of Genkit's console log handler and installs it as the process-wide default logger. Handlers previously installed with AddHandler are preserved.

A default handler the application installed itself (via slog.SetDefault or SetDefaultHandler) is not Genkit's to manage: SetLevel records the level for the managed console handler but leaves the application's handler in place and warns, exactly as GENKIT_LOG_LEVEL does, since that handler's own level is what governs output. Set the application handler's level directly instead.

func Warn added in v1.12.0

func Warn(ctx context.Context, msg string, args ...any)

Warn logs at slog.LevelWarn using the logger in ctx. See Debug.

func WithContext added in v1.12.0

func WithContext(ctx context.Context, l *slog.Logger) context.Context

WithContext returns a copy of ctx carrying l. FromContext and the package-level logging functions use l for anything logged under the returned context, so attributes bound with l.With flow to every log statement downstream:

ctx = logger.WithContext(ctx, logger.FromContext(ctx).With("requestId", id))

Types

This section is empty.

Jump to

Keyboard shortcuts

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