logger

package
v1.4.0 Latest Latest
Warning

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

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

Documentation

Overview

Package logger provides context-scoped structured logging for Genkit.

This package wraps the standard library's log/slog package to provide context-aware logging throughout Genkit operations. Logs are automatically associated with the current action or flow context.

Usage

Retrieve the logger from context within action or flow handlers:

func myFlow(ctx context.Context, input string) (string, error) {
	log := logger.FromContext(ctx)

	log.Info("Processing input", "size", len(input))
	log.Debug("Input details", "value", input)

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

	log.Info("Processing complete", "resultSize", len(result))
	return result, nil
}

Log Levels

Control the global log level to filter output:

// Show debug logs (verbose)
logger.SetLevel(slog.LevelDebug)

// Show info and above (default)
logger.SetLevel(slog.LevelInfo)

// Show only warnings and errors
logger.SetLevel(slog.LevelWarn)

// Show only errors
logger.SetLevel(slog.LevelError)

// Get the current log level
level := logger.GetLevel()

Context Integration

The logger is automatically available in action and flow contexts. It inherits from the context passed to [genkit.Init] and flows through all nested operations.

For custom operations outside of actions/flows, attach a logger to context:

log := slog.Default()
ctx = logger.WithContext(ctx, log)

slog Compatibility

The logger returned by FromContext is a standard *slog.Logger and supports all slog methods:

log := logger.FromContext(ctx)

// Structured logging with attributes
log.Info("User action",
	"userId", userID,
	"action", "login",
	"duration", elapsed,
)

// Grouped attributes
log.Info("Request completed",
	slog.Group("request",
		"method", r.Method,
		"path", r.URL.Path,
	),
	slog.Group("response",
		"status", status,
		"bytes", written,
	),
)

// With pre-set attributes
requestLog := log.With("requestId", requestID)
requestLog.Info("Starting")
// ... later ...
requestLog.Info("Finished")

This package is primarily used by Genkit internals but is useful for plugin developers who need consistent logging that integrates with Genkit's observability features.

Package logger provides a context-scoped slog.Logger.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromContext

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

FromContext returns the Logger in ctx, or the default Logger if there is none.

func GetLevel added in v0.3.0

func GetLevel() slog.Level

GetLevel gets the current global log level

func SetLevel added in v0.3.0

func SetLevel(level slog.Level)

SetLevel sets the global log level

Types

This section is empty.

Jump to

Keyboard shortcuts

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