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 ¶
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).
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.
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 ¶
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)
}
}
Output: