logging

package
v0.22.0 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package logging provides the host's structured, aggregated logger.

The host is the only component that touches log files: it writes its own JSON records and forwards each sidecar's stderr (via Writer) into one per-repo log file plus its own stderr. Services only write to stderr.

Index

Constants

View Source
const FileName = "anfra.log"

FileName is the log's file, in a repo's logs directory.

Variables

This section is empty.

Functions

func Follow added in v0.21.0

func Follow(ctx context.Context, path string, offset int64, f Filter, each func(Entry) error) error

Follow hands to each the entries f keeps as they're written to the log at path, from offset on, until ctx is done or each fails. A line is handed once it ends, with its newline. A log that shrinks (cut by hand) is read again from its start.

func LevelValid added in v0.21.0

func LevelValid(level string) bool

LevelValid reports whether level names a level, in any case.

Types

type Entry added in v0.21.0

type Entry struct {
	Time    time.Time      `json:"ts,omitzero" doc:"when it was written. Absent for a sidecar's line that doesn't say."`
	Level   string         `json:"level,omitempty" doc:"the record's level: DEBUG, INFO, WARN or ERROR. Absent for a sidecar's line."`
	Message string         `json:"msg,omitempty" doc:"what happened, in a sentence; from an older anfra-node, its event's name"`
	Service string         `` /* 129-byte string literal not displayed */
	Event   string         `json:"event,omitempty" doc:"the event's name, such as query_cache.unavailable"`
	Attrs   map[string]any `json:"attrs,omitempty" doc:"the record's other fields, such as error and hint"`
	Line    string         `json:"line,omitempty" doc:"a line a sidecar wrote that isn't a record, as it wrote it, its time taken out"`
}

Entry is one entry of a repo's log: a record anfra or a sidecar wrote, or a line a sidecar wrote that isn't a record.

func ParseEntry added in v0.21.0

func ParseEntry(line string) Entry

ParseEntry reads one line of the log: a record, a JSON object with its envelope (ts, level, msg); or another line, a sidecar's, with the time it starts with, if any.

func Read added in v0.21.0

func Read(path string, f Filter, n int) ([]Entry, int64, error)

Read is the last n entries of the log at path that f keeps, oldest first (n 0: every one), and where its last whole line ends, which Follow carries on from. A last line without its newline is still being written: Follow hands it over once it ends. It reads from the end back, so the last entries of a long log come at once. No log yet is no entry.

type Filter added in v0.21.0

type Filter struct {
	Level  string    // at least this level, in any case; "" any
	Events []string  // an event named one of these, or under it (query_cache matches query_cache.swept); none any
	Since  time.Time // written at or after it; zero any
}

Filter is which entries Read and Follow keep. A zero Filter keeps every one. A sidecar's line has no level and no event, so a filter on either leaves it out.

func (Filter) Keeps added in v0.21.0

func (f Filter) Keeps(e Entry) bool

Keeps reports whether f keeps e.

type Logging

type Logging struct {
	Logger *slog.Logger
	// App is the logger for what the repo's Data Apps report from the browser (appserve):
	// the same stream as Logger, with "service":"app".
	App          *slog.Logger
	StderrWriter io.Writer
	StdoutWriter io.Writer
	LogPath      string
	// contains filtered or unexported fields
}

Logging is the host's logging setup:

  • Logger: the host's own structured logger (writes to anfra.log).
  • StderrWriter: sink for forwarding sidecar stderr — the log stream, same destination as Logger (anfra.log; also host stderr when toStderr).
  • StdoutWriter: sink for forwarding sidecar stdout — discarded by default (banners / incidental noise), or host stdout if ANFRA_SIDECAR_STDOUT.

func Setup

func Setup(logsDir, repo string, toStderr bool) (*Logging, error)

Setup ensures logsDir exists, opens anfra.log there, and returns a host logger (and the same Writer the sidecars' output is forwarded into). The envelope ({ts, level, msg, service, repo, pid}) matches the Node sidecar's. Level comes from LOG_LEVEL (default info).

With toStderr, the log stream also goes to stderr: what `anfra serve` does unless ANFRA_LOG_STDERR=0, since a server's log is what its operator watches. Without, it goes only to the file, keeping a command's stderr for its error and warnings (ANFRA_LOG_STDERR=1 adds it, for debugging).

func (*Logging) Close

func (l *Logging) Close() error

Jump to

Keyboard shortcuts

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