log

package
v0.5.4 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2026 License: MIT Imports: 9 Imported by: ? 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 log reads system logs through an injected exec.Runner.

r, _ := exec.NewRunner(exec.Direct) // the system journal needs root
s, err := log.New(log.Journald, r)
if err != nil { ... }
lines, _ := s.Query(ctx, log.Query{Unit: "sshd.service", Lines: 200, Priority: "warning"})

Two backends are implemented: Journald (journalctl) and Syslog (/var/log/{syslog,messages}). The query filters and their hardening — line cap, priority allow-list, grep length cap, and the structural ReDoS guard — are shared and applied before any tool runs (validate-then-execute). Reads escalate through the Runner (system logs are root-readable). Detect lists the backends usable on the host.

Authorization of a remote log request is the consumer's concern; this package is the reader.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrInvalidQuery = errors.New("log: invalid query")

ErrInvalidQuery is returned when a Query field is unsafe or out of range (bad priority, an over-long or catastrophic-backtracking grep pattern).

View Source
var ErrUnknownBackend = errors.New("log: unknown backend")

ErrUnknownBackend is returned by New for the zero value or any unimplemented backend.

Functions

This section is empty.

Types

type Backend

type Backend int

Backend selects the log source. The zero value is invalid; only implemented backends exist.

const (
	// Journald reads the systemd journal via journalctl.
	Journald Backend = iota + 1
	// Syslog reads the classic /var/log/{syslog,messages} files.
	Syslog
)

func Detect

func Detect(ctx context.Context) []Backend

Detect reports the log backends usable on THIS host: Journald when journalctl is on PATH, Syslog when a classic log file exists. It lists; the consumer picks one and passes it to New.

The ctx is accepted for signature uniformity; the probe is PATH/stat lookups.

func (Backend) String

func (b Backend) String() string

String renders the backend as its canonical name.

type Query

type Query struct {
	// Unit filters by systemd unit (Journald only; ignored by Syslog).
	Unit string
	// Since and Until bound the time range (journalctl's --since/--until forms;
	// Journald only).
	Since, Until string
	// Priority filters by syslog priority (Journald only). Allowed values:
	// 0-7 or emerg/alert/crit/err/warning/notice/info/debug.
	Priority string
	// Grep keeps only entries matching this pattern. Capped at 256 chars and
	// refused if it is a catastrophic-backtracking shape (ReDoS guard).
	Grep string
	// Kernel restricts the result to kernel-ring messages (journalctl -k;
	// Journald only, ignored by Syslog).
	Kernel bool
	// Lines caps the number of entries returned. <=0 means 100; values above
	// 10000 are clamped to 10000.
	Lines int
}

Query selects which log entries to return.

type Source

type Source interface {
	// Query returns the matching log lines, most-recent-last, after validating
	// the query (validate-then-execute: an invalid query runs no command).
	Query(ctx context.Context, q Query) ([]string, error)
}

Source is the log read surface.

func New

func New(b Backend, runner exec.Runner) (Source, error)

New returns a Source for the named backend, driven by runner. Pure: validates the backend; does not probe (use Detect). Nil runner and unknown backend are rejected.

Example

ExampleNew reads recent warnings from a unit's journal.

package main

import (
	"context"
	"fmt"
	"log"

	"github.com/manchtools/power-manage-sdk/sys/exec"
	syslog "github.com/manchtools/power-manage-sdk/sys/log"
)

func main() {
	r, err := exec.NewRunner(exec.Direct) // the system journal needs root
	if err != nil {
		log.Fatal(err)
	}
	s, err := syslog.New(syslog.Journald, r)
	if err != nil {
		log.Fatal(err)
	}
	lines, err := s.Query(context.Background(), syslog.Query{
		Unit:     "sshd.service",
		Priority: "warning",
		Lines:    200,
	})
	if err != nil {
		log.Fatal(err)
	}
	for _, l := range lines {
		fmt.Println(l)
	}
}

Jump to

Keyboard shortcuts

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