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 ¶
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 ¶
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 ¶
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.
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 ¶
NewHandler creates a Loki slog handler with the app name read from SLOGGING_LOKI_APPNAME.
func NewHandlerWithConfig ¶
NewHandlerWithConfig creates a Loki slog handler from the given config, using the provided 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.