slogout

package
v0.18.0 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package slogout provides adapters to route container output through slog.Logger instead of directly to stdout/stderr. This is useful for modules that launch containers (like etcd) where we want structured logging instead of raw container output mixed with our application logs.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AttachLogger added in v0.15.2

func AttachLogger(logger *slog.Logger, module string, options ...LoggerOption) cio.Attach

AttachLogger creates a cio.Attach that reconnects an existing task's output FIFOs to slog.Logger.

func NewWriter

func NewWriter(logger *slog.Logger, level slog.Level, options ...LoggerOption) io.WriteCloser

NewWriter creates an io.WriteCloser that can be used as cmd.Stdout/cmd.Stderr to parse and route output through slog.Logger. The returned writer should be closed when done to flush any remaining buffered data.

func WithLogger

func WithLogger(logger *slog.Logger, module string, options ...LoggerOption) cio.Creator

WithLogger creates a cio.Creator that routes container output through slog.Logger instead of the default stdio. The module parameter is used to tag log entries with the source module (e.g., "etcd").

Example

ExampleWithLogger demonstrates how to use slogout.WithLogger to route container output through structured logging instead of stdout/stderr.

package main

import (
	"log/slog"
	"os"

	"miren.dev/runtime/pkg/slogout"
)

func main() {
	// Create a logger
	logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
		Level: slog.LevelInfo,
	}))

	// Create a CIO creator for etcd (which outputs JSON logs)
	etcdCreator := slogout.WithLogger(logger, "etcd", slogout.WithJSONParsing())

	// Create a CIO creator for ClickHouse (which has timestamp prefixes to ignore)
	clickhouseCreator := slogout.WithLogger(logger, "clickhouse",
		slogout.WithIgnorePattern(`^\d{4}\.\d{2}\.\d{2} \d{2}:\d{2}:\d{2}\.\d+`))

	// These creators can now be used with containerd containers:
	// task, err := container.NewTask(ctx, etcdCreator)
	// task, err := container.NewTask(ctx, clickhouseCreator)
	// Instead of:
	// task, err := container.NewTask(ctx, cio.NewCreator(cio.WithStdio))

	_ = etcdCreator       // Use the creator with containerd
	_ = clickhouseCreator // Use the creator with containerd
}

Types

type LoggerOption

type LoggerOption func(*LoggerOpts)

LoggerOption is a function that configures LoggerOpts

func WithIgnorePattern

func WithIgnorePattern(pattern string) LoggerOption

WithIgnorePattern sets a regexp pattern to ignore matching lines

func WithJSONParsing

func WithJSONParsing() LoggerOption

WithJSONParsing enables JSON parsing of log lines

func WithKeyValueParsing

func WithKeyValueParsing() LoggerOption

WithKeyValueParsing enables key=value parsing of log lines

func WithMaxLevel

func WithMaxLevel(level slog.Level) LoggerOption

WithMaxLevel sets the maximum log level to process

func WithSource added in v0.17.0

func WithSource(name string) LoggerOption

WithSource names the child process whose output this is, for log line counting. WithLogger and AttachLogger set it from their module argument.

func WithVictoriaParsing added in v0.17.0

func WithVictoriaParsing() LoggerOption

WithVictoriaParsing enables parsing of the VictoriaMetrics log format (timestamp, level, caller and message separated by tabs), used by vmagent, victoria-metrics and victoria-logs.

type LoggerOpts

type LoggerOpts struct {
	// IgnorePattern is a regexp that, if matched, will cause the line to be ignored
	IgnorePattern *regexp.Regexp
	// ParseJSON indicates whether to parse each line as JSON and extract key/value pairs
	ParseJSON bool
	// ParseKeyValue indicates whether to parse each line as key=value pairs
	ParseKeyValue bool
	// ParseVictoria indicates whether to parse each line in the tab-separated
	// format VictoriaMetrics components log in by default
	ParseVictoria bool

	// Source is the child process this output comes from. Its lines are
	// counted under that name and at their own level, even when ClampLevel
	// prints them lower. SourceMiren, the zero value, counts them as miren's.
	Source logcount.Source

	ClampLevel bool       // If true, will clamp log levels to MaxLevel
	MaxLevel   slog.Level // Maximum log level to process (default: Info)
}

LoggerOpts provides options for configuring log processing behavior

Jump to

Keyboard shortcuts

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