Documentation
¶
Overview ¶
Package logging provides structured logging for Kahi using stdlib slog.
Index ¶
- func CleanupStaleLogs(logDir string, patterns []string) error
- func DaemonLogger(level, format, logfile string) (*slog.Logger, func(), error)
- func FormatJSONLine(processName, stream, line string) []byte
- func New(cfg LogConfig) *slog.Logger
- func ParseSize(s string) int64
- func PipeToWriter(r io.ReadCloser, cw *CaptureWriter, containerStdout io.Writer)
- func RotateIfNeeded(path string, cfg RotationConfig) error
- func StripANSI(data []byte) []byte
- func ValidateLevel(s string) error
- func WithFields(logger *slog.Logger, fields ...any) *slog.Logger
- type CaptureConfig
- type CaptureWriter
- type LevelVar
- type LogConfig
- type RingBuffer
- type RotationConfig
- type SyslogForwarder
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CleanupStaleLogs ¶
CleanupStaleLogs removes stale log files on daemon startup.
func DaemonLogger ¶
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 ¶
FormatJSONLine formats a line of process output as a JSON log entry.
func ParseSize ¶
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 ValidateLevel ¶
ValidateLevel returns an error if the level string is not recognized.
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).
type LevelVar ¶
type LevelVar struct {
// contains filtered or unexported fields
}
LevelVar wraps a slog.LevelVar for dynamic level changes at runtime.
func NewLevelVar ¶
NewLevelVar creates a LevelVar initialized to the given level.
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) 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) 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.