logging

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package logging provides structured logging for Kahi using stdlib slog.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CleanupStaleLogs

func CleanupStaleLogs(logDir string, patterns []string) error

CleanupStaleLogs removes stale log files on daemon startup.

func DaemonLogger

func DaemonLogger(level, format, logfile string) (*slog.Logger, func(), error)

DaemonLogger creates a logger for the daemon, optionally writing to a file. Returns the logger and a cleanup function to close the file (if opened).

func FormatJSONLine

func FormatJSONLine(processName, stream, line string) []byte

FormatJSONLine formats a line of process output as a JSON log entry.

func New

func New(cfg LogConfig) *slog.Logger

New creates a configured *slog.Logger.

func ParseSize

func ParseSize(s string) int64

ParseSize parses a human-readable size string to bytes. Supports B, KB, MB, GB suffixes. Defaults to bytes if no suffix.

func PipeToWriter

func PipeToWriter(r io.ReadCloser, cw *CaptureWriter, containerStdout io.Writer)

PipeToWriter reads from a pipe and writes to a CaptureWriter, formatting as JSON lines for container stdout mode.

func RotateIfNeeded

func RotateIfNeeded(path string, cfg RotationConfig) error

RotateIfNeeded checks the file size and rotates if necessary.

func StripANSI

func StripANSI(data []byte) []byte

StripANSI removes ANSI escape sequences from data.

func ValidateLevel

func ValidateLevel(s string) error

ValidateLevel returns an error if the level string is not recognized.

func WithFields

func WithFields(logger *slog.Logger, fields ...any) *slog.Logger

WithFields returns a child logger with additional context fields.

Types

type CaptureConfig

type CaptureConfig struct {
	ProcessName    string
	Stream         string // "stdout" or "stderr"
	Logfile        string // path, empty for container mode
	RedirectStderr bool
	StripAnsi      bool
	MaxBytes       string // max file size before rotation (e.g. "10KB")
	Backups        int    // number of rotated backup files to keep
	Logger         *slog.Logger
}

CaptureConfig configures process output capture.

type CaptureWriter

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

CaptureWriter captures process output and routes it to configured destinations.

func NewCaptureWriter

func NewCaptureWriter(cfg CaptureConfig) (*CaptureWriter, error)

NewCaptureWriter creates a capture writer for a process stream.

func (*CaptureWriter) AddHandler

func (cw *CaptureWriter) AddHandler(h func(name string, data []byte))

AddHandler adds a callback for captured data.

func (*CaptureWriter) Close

func (cw *CaptureWriter) Close() error

Close closes the log file if open.

func (*CaptureWriter) ReadTail

func (cw *CaptureWriter) ReadTail(n int) []byte

ReadTail returns the last n bytes from the ring buffer.

func (*CaptureWriter) Reopen

func (cw *CaptureWriter) Reopen() error

Reopen closes and reopens the log file (for log rotation tools).

func (*CaptureWriter) Write

func (cw *CaptureWriter) Write(p []byte) (int, error)

Write implements io.Writer.

type LevelVar

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

LevelVar wraps a slog.LevelVar for dynamic level changes at runtime.

func NewLevelVar

func NewLevelVar(level string) *LevelVar

NewLevelVar creates a LevelVar initialized to the given level.

func (*LevelVar) Level

func (lv *LevelVar) Level() slog.Level

Level returns the current slog.Level.

func (*LevelVar) Set

func (lv *LevelVar) Set(level string)

Set changes the log level at runtime.

type LogConfig

type LogConfig struct {
	Level  string    // "debug", "info", "warn", "error"
	Format string    // "json" (default), "text"
	Output io.Writer // defaults to os.Stdout
}

LogConfig controls logger creation.

type RingBuffer

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

RingBuffer is a fixed-size circular buffer for process output.

func NewRingBuffer

func NewRingBuffer(size int) *RingBuffer

NewRingBuffer creates a ring buffer with the given capacity. Size is clamped to [1, maxRingBufSize].

func (*RingBuffer) Len

func (rb *RingBuffer) Len() int

Len returns the number of bytes stored.

func (*RingBuffer) Read

func (rb *RingBuffer) Read(n int) []byte

Read returns the last n bytes from the buffer. If n exceeds available data, returns all available data. Returns nil if n is out of range [1, maxReadAlloc].

func (*RingBuffer) Reset

func (rb *RingBuffer) Reset()

Reset clears the buffer.

func (*RingBuffer) Write

func (rb *RingBuffer) Write(p []byte)

Write appends data to the ring buffer.

type RotationConfig

type RotationConfig struct {
	Maxbytes string // e.g., "50MB", "0" means unlimited
	Backups  int    // number of backup files to keep
}

RotationConfig configures size-based log rotation.

type SyslogForwarder

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

SyslogForwarder sends process output to syslog.

func NewSyslogForwarder

func NewSyslogForwarder(tag string) (*SyslogForwarder, error)

NewSyslogForwarder creates a syslog forwarder for a process.

func (*SyslogForwarder) Close

func (sf *SyslogForwarder) Close() error

Close closes the syslog connection.

func (*SyslogForwarder) Write

func (sf *SyslogForwarder) Write(p []byte) (int, error)

Write sends data to syslog.

Jump to

Keyboard shortcuts

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