logs

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package logs provides workflow run log fetching, caching, filtering, and streaming functionality.

Index

Constants

View Source
const StreamPollInterval = 2 * time.Second

StreamPollInterval is the interval between log polling for active runs.

Variables

View Source
var QuickFilters = map[string]*FilterConfig{
	"all": {
		Level:         FilterAll,
		SearchTerm:    "",
		CaseSensitive: false,
		Regex:         false,
		StepIndex:     -1,
	},
	"errors": {
		Level:         FilterErrors,
		SearchTerm:    "",
		CaseSensitive: false,
		Regex:         false,
		StepIndex:     -1,
	},
	"warnings": {
		Level:         FilterWarnings,
		SearchTerm:    "",
		CaseSensitive: false,
		Regex:         false,
		StepIndex:     -1,
	},
}

QuickFilters provides common filter configurations.

Functions

func CheckGHCLIAvailable

func CheckGHCLIAvailable() error

CheckGHCLIAvailable checks if gh CLI is installed and authenticated.

func CheckGHCLIAvailableWithExecutor

func CheckGHCLIAvailableWithExecutor(executor exec.CommandExecutor) error

CheckGHCLIAvailableWithExecutor checks if gh CLI is installed and authenticated using a custom executor.

func GenerateANSILog

func GenerateANSILog() string

GenerateANSILog creates logs with ANSI color codes. Tests proper handling of terminal color escape sequences.

func GenerateLargeLogFixture

func GenerateLargeLogFixture(lines int) string

GenerateLargeLogFixture creates a realistic log file with N lines. Uses GitHub Actions log format patterns for authenticity.

func GenerateLargeLogWithErrors

func GenerateLargeLogWithErrors(lines int, errorRate float64) string

GenerateLargeLogWithErrors creates a log with error patterns. The errorRate is a float between 0 and 1 indicating percentage of lines that should be errors.

func GenerateLogWithTimestamps

func GenerateLogWithTimestamps(lines int) string

GenerateLogWithTimestamps creates log lines with timestamp prefixes. Tests timestamp parsing and display.

func GenerateMixedLog

func GenerateMixedLog(lines int) string

GenerateMixedLog creates a log with various patterns for comprehensive testing. Includes errors, warnings, unicode, ANSI codes, and normal logs.

func GenerateMultiStepLog

func GenerateMultiStepLog(numSteps, linesPerStep int) string

GenerateMultiStepLog creates a log output with multiple GitHub Actions steps. Simulates a real workflow run with step grouping.

func GenerateUnicodeLog

func GenerateUnicodeLog() string

GenerateUnicodeLog creates logs with unicode characters. Tests proper handling of international characters and emoji.

func LoadFixture

func LoadFixture(tb testing.TB, filename string) string

LoadFixture loads a test fixture file from testdata. Helper function for tests and benchmarks.

Types

type Cache

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

Cache stores fetched logs locally for quick access.

func NewCache

func NewCache(cacheDir string) *Cache

NewCache creates a new log cache. The cacheDir should be something like ~/.cache/lazydispatch/logs/.

func (*Cache) Clear

func (c *Cache) Clear() error

Clear removes expired entries from the cache.

func (*Cache) Get

func (c *Cache) Get(chainName string, runID int64) (*RunLogs, bool)

Get retrieves cached logs if available and not expired.

func (*Cache) Load

func (c *Cache) Load() error

Load loads the cache from disk.

func (*Cache) Put

func (c *Cache) Put(chainName string, runID int64, logs *RunLogs, ttl time.Duration) error

Put stores logs in the cache.

func (*Cache) Stats

func (c *Cache) Stats() CacheStats

Stats returns cache statistics.

type CacheEntry

type CacheEntry struct {
	CachedAt  time.Time     `json:"cached_at"`
	Logs      *RunLogs      `json:"logs"`
	ChainName string        `json:"chain_name"`
	RunID     int64         `json:"run_id"`
	TTL       time.Duration `json:"ttl"`
}

CacheEntry represents cached log data.

type CacheStats

type CacheStats struct {
	TotalEntries   int
	ValidEntries   int
	ExpiredEntries int
}

CacheStats provides cache metrics.

type Fetcher

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

Fetcher fetches and parses workflow logs.

func NewFetcher

func NewFetcher(client GitHubClient) *Fetcher

NewFetcher creates a new log fetcher.

func (*Fetcher) FetchRunSummary

func (f *Fetcher) FetchRunSummary(runID int64) (string, error)

FetchRunSummary creates a summary of failed steps without full logs.

func (*Fetcher) FetchStepLogs

func (f *Fetcher) FetchStepLogs(runID int64, workflow string) ([]*StepLogs, error)

FetchStepLogs fetches logs for a specific workflow run. Returns a StepLogs for each job step in the workflow.

type Filter

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

Filter applies filtering logic to log entries.

func NewFilter

func NewFilter(config *FilterConfig) (*Filter, error)

NewFilter creates a new log filter with the given configuration.

func (*Filter) Apply

func (f *Filter) Apply(runLogs *RunLogs) *FilteredResult

Apply filters a RunLogs instance and returns filtered entries.

type FilterConfig

type FilterConfig struct {
	Level         FilterLevel
	SearchTerm    string
	CaseSensitive bool
	Regex         bool
	StepIndex     int // -1 for all steps
}

FilterConfig configures log filtering.

func NewFilterConfig

func NewFilterConfig() *FilterConfig

NewFilterConfig creates a default filter config.

type FilterLevel

type FilterLevel string

FilterLevel represents different log filtering modes.

const (
	FilterAll      FilterLevel = "all"
	FilterErrors   FilterLevel = "errors"
	FilterWarnings FilterLevel = "warnings"
	FilterCustom   FilterLevel = "custom"
)

Log filtering modes.

type FilteredLogEntry

type FilteredLogEntry struct {
	Original      LogEntry
	Matches       []MatchPosition
	OriginalIndex int
}

FilteredLogEntry wraps a log entry with match information.

type FilteredResult

type FilteredResult struct {
	Config *FilterConfig
	Steps  []*FilteredStepLogs
}

FilteredResult contains the filtered logs with match information.

func (*FilteredResult) TotalEntries

func (fr *FilteredResult) TotalEntries() int

TotalEntries returns the total number of filtered entries.

type FilteredStepLogs

type FilteredStepLogs struct {
	Workflow  string
	StepName  string
	Entries   []FilteredLogEntry
	StepIndex int
}

FilteredStepLogs contains filtered logs for a single step.

type GHFetcher

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

GHFetcher fetches real logs using gh CLI.

func NewGHFetcher

func NewGHFetcher(client GitHubClient) *GHFetcher

NewGHFetcher creates a fetcher that uses gh CLI for real log access.

func NewGHFetcherWithExecutor

func NewGHFetcherWithExecutor(client GitHubClient, executor exec.CommandExecutor) *GHFetcher

NewGHFetcherWithExecutor creates a fetcher with a custom executor (for testing).

func (*GHFetcher) FetchStepLogsReal

func (f *GHFetcher) FetchStepLogsReal(runID int64, workflow string) ([]*StepLogs, error)

FetchStepLogsReal fetches actual logs from GitHub using gh CLI.

func (*GHFetcher) FetchWorkflowLogs

func (f *GHFetcher) FetchWorkflowLogs(runID int64) (string, error)

FetchWorkflowLogs fetches all logs for a workflow run (all jobs).

type GitHubClient

type GitHubClient interface {
	GetWorkflowRun(runID int64) (*github.WorkflowRun, error)
	GetWorkflowRunJobs(runID int64) ([]github.Job, error)
}

GitHubClient interface for fetching workflow data.

type LogEntry

type LogEntry struct {
	Timestamp time.Time `json:"timestamp"`
	Content   string    `json:"content"`
	Level     LogLevel  `json:"level"`     // error, warning, info, debug
	StepName  string    `json:"step_name"` // for grouping
}

LogEntry represents a single log line with metadata.

func ParseLogOutput

func ParseLogOutput(rawLogs, stepName string) []LogEntry

ParseLogOutput parses raw log text into LogEntry structs. Detects log levels based on common patterns.

type LogFetcher

type LogFetcher interface {
	FetchStepLogs(runID int64, workflow string) ([]*StepLogs, error)
}

LogFetcher defines the interface for fetching logs.

type LogLevel

type LogLevel string

LogLevel indicates the severity of a log line.

const (
	LogLevelError   LogLevel = "error"
	LogLevelWarning LogLevel = "warning"
	LogLevelInfo    LogLevel = "info"
	LogLevelDebug   LogLevel = "debug"
	LogLevelUnknown LogLevel = "unknown"
)

Log severity levels.

type LogStreamer

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

LogStreamer polls for incremental log updates from active workflow runs.

func NewLogStreamer

func NewLogStreamer(client GitHubClient, runID int64, workflow string) *LogStreamer

NewLogStreamer creates a new LogStreamer for a specific run.

func (*LogStreamer) Start

func (s *LogStreamer) Start()

Start begins polling for log updates.

func (*LogStreamer) Stop

func (s *LogStreamer) Stop()

Stop stops the streamer and cleans up resources. Safe to call multiple times.

func (*LogStreamer) Updates

func (s *LogStreamer) Updates() <-chan StreamUpdate

Updates returns the channel for receiving log updates.

type Manager

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

Manager coordinates log fetching, caching, and access.

func NewManager

func NewManager(client GitHubClient, cacheDir string) *Manager

NewManager creates a new log manager that uses gh CLI if available.

func (*Manager) ClearExpired

func (m *Manager) ClearExpired() error

ClearExpired removes expired entries from the cache.

func (*Manager) GetLogsForChain

func (m *Manager) GetLogsForChain(chainState chain.ChainState, branch string) (*RunLogs, error)

GetLogsForChain fetches or retrieves cached logs for a chain execution. Per-step fetch errors are recorded on the affected step rather than returned, but the error return is kept for symmetry with GetLogsForRun since callers assign both into one var.

func (*Manager) GetLogsForRun

func (m *Manager) GetLogsForRun(runID int64, workflow string) (*RunLogs, error)

GetLogsForRun fetches logs for a single workflow run.

func (*Manager) LoadCache

func (m *Manager) LoadCache() error

LoadCache loads the log cache from disk.

type MatchPosition

type MatchPosition struct {
	Start int
	End   int
}

MatchPosition indicates where a search term was found in the content.

type RunLogs

type RunLogs struct {
	ChainName string      `json:"chain_name"`
	Branch    string      `json:"branch"`
	Steps     []*StepLogs `json:"steps"`
	// contains filtered or unexported fields
}

RunLogs contains logs for all steps in a workflow run or chain.

func GenerateRunLogsWithEntries

func GenerateRunLogsWithEntries(totalEntries int) *RunLogs

GenerateRunLogsWithEntries creates RunLogs with N total entries for benchmarking. Distributes entries across multiple steps with varying log levels.

func NewRunLogs

func NewRunLogs(chainName, branch string) *RunLogs

NewRunLogs creates a new RunLogs instance.

func (*RunLogs) AddStep

func (rl *RunLogs) AddStep(stepLogs *StepLogs)

AddStep adds step logs to the run logs.

func (*RunLogs) AllSteps

func (rl *RunLogs) AllSteps() []*StepLogs

AllSteps returns all step logs.

func (*RunLogs) GetStep

func (rl *RunLogs) GetStep(idx int) *StepLogs

GetStep returns step logs by index.

type StepLogs

type StepLogs struct {
	FetchedAt  time.Time  `json:"fetched_at"`
	Error      error      `json:"-"`
	Workflow   string     `json:"workflow"`
	JobName    string     `json:"job_name"`
	StepName   string     `json:"step_name"`
	Status     string     `json:"status"`
	Conclusion string     `json:"conclusion"`
	Entries    []LogEntry `json:"entries"`
	StepIndex  int        `json:"step_index"`
	RunID      int64      `json:"run_id"`
}

StepLogs contains all log entries for a single workflow step.

type StreamState

type StreamState struct {
	StepLineCounts map[int]int // map[stepIndex]lineCount
}

StreamState tracks the state of logs for incremental updates.

func NewStreamState

func NewStreamState() *StreamState

NewStreamState creates a new StreamState.

type StreamUpdate

type StreamUpdate struct {
	Error      error
	Status     string
	Conclusion string
	NewSteps   []*StepLogs
	RunID      int64
}

StreamUpdate represents new log content detected during streaming.

Jump to

Keyboard shortcuts

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