filelog

package
v1.12.0 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2026 License: Apache-2.0 Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package filelog tees the agent's stderr stream to a file on disk so that when agent-api is unreachable (network failure, firewall, expired credentials), the customer can be asked to share the local log file with support.

Mechanism: Start() opens an os.Pipe, swaps os.Stderr for the pipe's write end, and spawns a goroutine that reads from the pipe and writes to both the original stderr file and the on-disk log file. Stop() closes the write end, waits for the goroutine to drain, restores os.Stderr, and closes the file. This mirrors the in-memory capture already used by internal/telemetry/logcapture.go, with a file in place of the bytes.Buffer.

The file is opened with O_APPEND|O_SYNC. O_SYNC matters because os.Exit (called from many sites in main.go) skips defers, so the tee goroutine may not flush before the process dies — O_SYNC makes each individual Write durable at the cost of a few extra ms per run.

Rotation is single-file and per-open: if the existing file exceeds maxBytes at Start time, it is renamed to path+".prev" so one prior run survives. This is a diagnostic snapshot, not an audit log; the 5 MiB cap matches the precedent set by internal/aiagents/cli/errlog.go.

In service mode (launchd LaunchAgent, systemd user units, Windows Task Scheduler) the OS-level redirect is already writing the agent's stderr to a file. StartIfEligible detects this via ShouldSkip (os.Stderr.Stat reports a regular file) and returns (nil, nil) without opening anything, so the OS redirect remains the sole writer and no lines are duplicated.

Index

Constants

View Source
const DefaultMaxBytes int64 = 5 * 1024 * 1024

DefaultMaxBytes is the size cap that triggers single-file rotation at Start time. Matches internal/aiagents/cli/errlog.go's MaxErrorLogBytes for consistency with the existing on-disk-log convention.

View Source
const Filename = "agent.error.log"

Filename is the basename of the stderr log file the agent writes into the configured base directory. Exported so callers (main, configure prompts) can show the full resolved path to the user without duplicating the literal.

View Source
const StdoutFilename = "agent.log"

StdoutFilename is the basename of the stdout log file the OS-level scheduler redirect writes to in service mode. The agent itself does not write here, but RotateIfOverCap is applied to it at startup so it can't grow unbounded.

Variables

This section is empty.

Functions

func DefaultDir

func DefaultDir() string

DefaultDir returns the resolved install dir (paths.Home()) — that is the directory the agent writes agent.error.log into by default. Empty string if the home directory cannot be resolved; callers treat empty as "disabled."

func DefaultPath

func DefaultPath() string

DefaultPath is a convenience for DefaultDir() + Filename.

func RotateIfOverCap

func RotateIfOverCap(path string, maxBytes int64)

RotateIfOverCap renames path to path+".prev" when its current size exceeds maxBytes, freeing the original name for a fresh log. Used by Start before opening the stderr file, and by main at startup for the stdout-redirect file (agent.log) so neither log can grow unbounded. Best-effort: any stat/rename failure is swallowed so a missing file or a cross-device rename doesn't take down the agent.

func ShouldSkip

func ShouldSkip(stderr *os.File) bool

ShouldSkip reports whether the in-process file writer should be skipped because the given stream is already redirected to a regular file by the OS (service mode). Terminals report ModeCharDevice and pipes report ModeNamedPipe; only regular files trigger the skip.

Errors from Stat are treated as "no skip" — better to log to a fresh file than to log nowhere.

Types

type Capture

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

Capture owns the swapped os.Stderr, the pipe ends, the log file, and the tee goroutine's lifecycle. A nil receiver is safe to call Stop on — that simplifies main.go where StartIfEligible may return nil.

func Start

func Start(path string, maxBytes int64) (*Capture, error)

Start installs the stderr tee, opening (and rotating) the log file and swapping os.Stderr in a single best-effort sequence. On any failure before os.Stderr is reassigned, the global state is left untouched and an error is returned.

path must be non-empty. To install conditionally based on path / service-mode detection, use StartIfEligible instead.

func StartIfEligible

func StartIfEligible(path string, maxBytes int64) (*Capture, error)

StartIfEligible is the main.go-facing entrypoint. It returns (nil, nil) when capture should be skipped (empty path, or ShouldSkip(os.Stderr) returns true), and the result of Start otherwise. Callers can `defer cap.Stop()` unconditionally because Stop on a nil receiver is a no-op.

func (*Capture) Stop

func (c *Capture) Stop() error

Stop closes the pipe write end, waits for the tee goroutine to finish draining, restores os.Stderr, and closes the file. Idempotent and safe to call on a nil receiver.

Jump to

Keyboard shortcuts

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