logstore

package
v0.1.21 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func StartPruner

func StartPruner(ctx context.Context, store *LogStore, retention time.Duration, logger *zap.Logger)

StartPruner runs a background goroutine that prunes log entries older than the given retention duration. It prunes once immediately, then every 24 hours. The goroutine exits when ctx is cancelled.

Types

type LogEntry

type LogEntry struct {
	ID          int64     `json:"id"`
	Timestamp   time.Time `json:"ts"`
	Level       string    `json:"level"`
	Message     string    `json:"msg"`
	Component   string    `json:"component,omitempty"`
	SessionID   string    `json:"session_id,omitempty"`
	IssueNumber int       `json:"issue_number,omitempty"`
	Fields      string    `json:"fields,omitempty"`
}

LogEntry represents a single structured log record persisted to SQLite.

type LogStore

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

LogStore provides SQLite-backed structured log persistence.

func New

func New(dbPath string) (*LogStore, error)

New opens (or creates) a SQLite database at dbPath for log storage, enables WAL mode and busy_timeout, and runs migrations.

func (*LogStore) Close

func (s *LogStore) Close() error

Close closes the underlying database connection.

func (*LogStore) Insert

func (s *LogStore) Insert(ctx context.Context, entry *LogEntry) error

Insert writes a single log entry to SQLite.

func (*LogStore) Prune

func (s *LogStore) Prune(ctx context.Context, olderThan time.Duration) (int64, error)

Prune deletes log entries older than the given retention duration. Returns the number of rows deleted.

func (*LogStore) Query

func (s *LogStore) Query(ctx context.Context, f QueryFilter) ([]LogEntry, error)

Query returns log entries matching the given filter, ordered by ts DESC, id DESC.

type QueryFilter

type QueryFilter struct {
	Level     string
	Component string
	SessionID string
	Issue     int
	Since     time.Time
	Until     time.Time
	Search    string // substring match on msg
	Limit     int    // default 100, max 1000
	Offset    int    // for pagination
}

QueryFilter controls which log entries are returned by Query.

type Syncer

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

Syncer is a zap-compatible WriteSyncer that tees log output to both stdout and a SQLite LogStore. SQLite errors are swallowed (logged to stderr) to ensure logging never crashes the application.

func NewSyncer

func NewSyncer(stdout io.Writer, store *LogStore) *Syncer

NewSyncer creates a WriteSyncer that writes to stdout and persists structured JSON log lines to the given LogStore.

func (*Syncer) Sync

func (s *Syncer) Sync() error

Sync implements zapcore.WriteSyncer. It syncs stdout if it supports the Sync method (e.g., os.File). Errors from syncing stdout/stderr are ignored because /dev/stdout cannot be fsynced on Linux.

func (*Syncer) Write

func (s *Syncer) Write(p []byte) (n int, err error)

Write implements io.Writer. It always writes p to stdout. If p is a valid JSON log line, it also inserts the parsed entry into SQLite. SQLite errors are never returned; only stdout write errors propagate.

Jump to

Keyboard shortcuts

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