logging

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2026 License: AGPL-3.0 Imports: 11 Imported by: 0

Documentation

Overview

Package logging builds the agent's structured logger over a size- and age-bounded file on the state mount, teed to stderr.

Two guarantees this package exists to keep, both of them requirements:

  • Log lines written before a crash are readable after a restart. That is why the sink is a file on the bind-mounted state directory and not only stderr.
  • The agent must never be the reason a small VPS runs out of disk. That is why the on-disk total is bounded by whichever of size or age is hit first.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Rotator

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

Rotator forces a log rotation on a fixed interval.

This is NOT redundant with lumberjack's own behaviour, and deleting it would silently break age-based retention. lumberjack rotates on SIZE only, and it applies MaxAge only at the moment a rotation happens. A quiet agent never fills a file, so it never rotates, so it never prunes — its agent.log would keep entries from months ago while sitting comfortably under the size cap. The ticker is what makes DEVMON_LOG_MAX_AGE_DAYS mean anything.

func NewRotator

func NewRotator(lj *lumberjack.Logger, interval, maxAge time.Duration, log *slog.Logger) *Rotator

NewRotator returns a Rotator that rotates lj every interval, and that also rotates a stale current file once at startup (see Run and startupRotate). maxAge should be the same age budget lj.MaxAge was derived from (config.Config.LogMaxAge in production); a value of zero or less disables the startup pass entirely, which callers that construct a Rotator directly (rather than through NewSink) may rely on to skip it.

func (*Rotator) Run

func (r *Rotator) Run(ctx context.Context) error

Run rotates once immediately and then on each tick until ctx is cancelled, returning ctx.Err().

The immediate pass matters for the same reason the Rotator exists at all: lumberjack only applies MaxAge at the moment a rotation happens, and an agent restarted more often than r.interval (a redeploy, a host reboot, a crash loop) would otherwise never rotate — and so never enforce DEVMON_LOG_MAX_AGE_DAYS — before being replaced again. It runs synchronously before the loop rather than on a shortened first delay because runAll (cmd/devmon-agent/main.go) already starts this alongside the HTTP listener as an independent goroutine, so it overlaps with, and never blocks, the agent coming up. If ctx is already cancelled on entry, Run returns immediately without touching the log file.

The startup pass calls startupRotate, not rotateOnce directly, both to avoid spraying empty rotated backups on a crash-looping agent (see startupRotate's own doc comment for the missing/empty-file skip) and, since issue #99, to avoid rotating away the very lines the sink is currently writing: NewSink opens the file and the caller starts logging (self-identification, "agent listening") before this goroutine ever runs, so an unconditional startup rotation would move this boot's own opening lines into a backup before anything could ever read them from agent.log. startupRotate only rotates when the file's existing content is actually older than the configured max age.

A failed rotation is logged and the loop continues: losing retention is bad, but taking the agent down over it is worse.

type Sink

type Sink struct {
	Logger *slog.Logger
	// contains filtered or unexported fields
}

Sink owns the agent's logger and the file it writes to. It must be closed on shutdown so buffered bytes reach disk before the process exits.

func NewSink

func NewSink(cfg config.Config) (*Sink, error)

NewSink creates the logs directory, opens the rotated log file, and returns a slog logger writing to both that file and stderr.

The stderr tee is not redundant: `docker logs devmon-agent` is the operator's first diagnostic, and it only sees the process's standard streams.

func (*Sink) Close

func (s *Sink) Close() error

Close flushes and closes the log file.

func (*Sink) Run

func (s *Sink) Run(ctx context.Context) error

Run drives periodic rotation until ctx is cancelled.

Jump to

Keyboard shortcuts

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