loki

package
v1.7.2 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package loki pushes log records to Loki's HTTP API as an slog.Handler.

It composes with this module's fan-out:

client, _ := loki.NewClient()
handler, _ := loki.NewHandler(client, slog.LevelInfo, nil)
slogconf.AddHandler(handler)

Pushes are best-effort and never block the caller on failure: a logging handler that returned errors into slog would be discarded anyway, and one that retried would turn an unreachable Loki into an application stall.

Index

Constants

View Source
const (
	EnvVarNameURL     = "SLOGGING_LOKI_URL"
	EnvVarNameAppName = "SLOGGING_LOKI_APPNAME"
)

The environment variables read when a config is not supplied directly.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client pushes log entries directly to Loki's HTTP API.

func NewClient

func NewClient() (*Client, error)

NewClient creates a Loki client configured from SLOGGING_LOKI_URL.

The variable is read directly rather than through a struct-tag config loader: it is one string, and a tag-based loader is what previously made this module's variable names impossible to change without forking it.

func NewClientWithConfig

func NewClientWithConfig(cfg ClientConfig) (*Client, error)

NewClientWithConfig creates a Loki client from the given config.

func (*Client) Push

func (c *Client) Push(
	ctx context.Context,
	labels map[string]string,
	line string,
)

Push sends a single log line with labels to Loki.

Failures are dropped deliberately. This is called from a slog.Handler, and slog discards whatever Handle returns — so surfacing an error would achieve nothing, while blocking or retrying would let an unreachable Loki stall the application that is only trying to log.

func (*Client) PushLog

func (c *Client) PushLog(
	ctx context.Context,
	appName string,
	source string,
	level string,
	msg string,
	data map[string]any,
)

PushLog formats a log entry and sends it to Loki under the given app name.

type ClientConfig

type ClientConfig struct {
	// URL is the Loki base URL, without the push path.
	URL string
}

ClientConfig holds the connection settings for the Loki client.

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

Handler implements slog.Handler and pushes records to Loki. Several handlers can share one Client.

func NewHandler

func NewHandler(
	client *Client,
	level slog.Level,
	labelKeys map[string]bool,
) (*Handler, error)

NewHandler creates a Loki slog handler with the app name read from SLOGGING_LOKI_APPNAME.

func NewHandlerWithConfig

func NewHandlerWithConfig(
	client *Client,
	cfg HandlerConfig,
	level slog.Level,
) (*Handler, error)

NewHandlerWithConfig creates a Loki slog handler from the given config, using the provided client.

func (*Handler) Enabled

func (h *Handler) Enabled(_ context.Context, level slog.Level) bool

Enabled reports whether the handler emits records at this level.

func (*Handler) Handle

func (h *Handler) Handle(_ context.Context, r slog.Record) error

Handle renders the record into a Loki line plus labels and pushes it.

func (*Handler) WithAttrs

func (h *Handler) WithAttrs(attrs []slog.Attr) slog.Handler

WithAttrs returns a handler with the attrs bound, sharing the same client.

func (*Handler) WithGroup

func (h *Handler) WithGroup(name string) slog.Handler

WithGroup returns a handler with the group applied, sharing the same client.

type HandlerConfig

type HandlerConfig struct {
	// AppName is emitted as the `app` label on every stream.
	AppName string

	// LabelKeys are slog attribute keys that become Loki LABELS instead of
	// part of the log line.
	//
	// Choose these carefully: Loki indexes by label, and every distinct value
	// creates a new stream. A high-cardinality key like request_id would make
	// one stream per request.
	LabelKeys map[string]bool
}

HandlerConfig holds the settings for the slog Loki handler.

Jump to

Keyboard shortcuts

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