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 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 ¶
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
}
Output:
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
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
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