logs

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: MIT Imports: 12 Imported by: 0 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 logs aggregates and formats log streams from multiple sources.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BestDetail added in v1.2.0

func BestDetail(msgs []string) string

BestDetail extracts a concise single-line preview from deduplicated messages.

func DedupeMessages added in v1.2.0

func DedupeMessages(msgs []string) []string

DedupeMessages collapses Cloudflare's multi-fragment log bursts (e.g. request echo, bare "⨯", short stack, full stack) into distinct, non-redundant messages.

func FormatReport added in v1.2.0

func FormatReport(w io.Writer, rep *AnalysisReport, topN int)

FormatReport writes a concise, ownership-focused triage report to w.

func OwnerBadge added in v1.2.0

func OwnerBadge(o Owner) string

OwnerBadge returns a consistent ANSI-colored badge for terminal output.

func StreamCFEntries added in v1.2.0

func StreamCFEntries(r io.Reader, fn func(CFExportEntry) error) (truncated bool, err error)

StreamCFEntries streams a JSON array ([...]) or NDJSON stream of Cloudflare log objects in constant memory, tolerating a truncated tail cleanly.

Types

type AnalysisReport added in v1.2.0

type AnalysisReport struct {
	TotalRawEvents   int           `json:"totalRawEvents"`
	DistinctRequests int           `json:"distinctRequests"`
	Truncated        bool          `json:"truncated,omitempty"`
	ByOwner          map[Owner]int `json:"byOwner"`
	Groups           []IssueGroup  `json:"groups"`
	TopPathStatus    []CountItem   `json:"topPathStatus"`
	TopCountries     []CountItem   `json:"topCountries"`
	ByMinute         []CountItem   `json:"byMinute"`
	Records          []ErrRecord   `json:"records,omitempty"`
}

AnalysisReport summarizes a log stream or exported log file.

func AnalyzeFile added in v1.2.0

func AnalyzeFile(path string, opts AnalyzeOptions) (*AnalysisReport, error)

AnalyzeFile opens path and analyzes it in constant streaming memory.

func AnalyzeReader added in v1.2.0

func AnalyzeReader(r io.Reader, opts AnalyzeOptions) (*AnalysisReport, error)

AnalyzeReader streams JSON/NDJSON Cloudflare logs from r and returns an AnalysisReport.

type AnalyzeOptions added in v1.2.0

type AnalyzeOptions struct {
	RouteFilter     string
	OwnerFilter     Owner
	AllLevels       bool
	RawNDJSONOut    io.Writer
	MergedNDJSONOut io.Writer
}

AnalyzeOptions configures streaming filtering and optional NDJSON output.

type CFExportEntry added in v1.2.0

type CFExportEntry struct {
	Source    CFExportSource   `json:"source"`
	Timestamp FlexTimestamp    `json:"timestamp"`
	Workers   CFExportWorkers  `json:"$workers"`
	Metadata  CFExportMetadata `json:"$metadata"`
}

CFExportEntry is the minimal shape of a Cloudflare Workers Observability event. Unknown fields are skipped during streaming decode.

type CFExportMetadata added in v1.2.0

type CFExportMetadata struct {
	ID        string `json:"id"`
	RequestID string `json:"requestId"`
	RayID     string `json:"rayId"`
	TraceID   string `json:"traceId"`
	Level     string `json:"level"`
	Error     string `json:"error"`
	Message   string `json:"message"`
	Trigger   string `json:"trigger"`
	Service   string `json:"service"`
}

type CFExportSource added in v1.2.0

type CFExportSource struct {
	Level   string `json:"level"`
	Message any    `json:"message"`
}

func (CFExportSource) MessageString added in v1.2.0

func (s CFExportSource) MessageString() string

MessageString converts Source.Message (string, array, or object) into a string.

type CFExportWorkers added in v1.2.0

type CFExportWorkers struct {
	Event         CFExportWorkersEvent `json:"event"`
	ScriptName    string               `json:"scriptName"`
	ScriptVersion struct {
		ID string `json:"id"`
	} `json:"scriptVersion"`
	Outcome    string  `json:"outcome"`
	WallTimeMs float64 `json:"wallTimeMs"`
	CPUTimeMs  float64 `json:"cpuTimeMs"`
}

func (CFExportWorkers) RequestIDCompat added in v1.2.0

func (w CFExportWorkers) RequestIDCompat() string

RequestIDCompat returns an empty string (Workers metadata requestId lives under $metadata.requestId in exports).

type CFExportWorkersEvent added in v1.2.0

type CFExportWorkersEvent struct {
	Request struct {
		URL     string            `json:"url"`
		Path    string            `json:"path"`
		Method  string            `json:"method"`
		Headers map[string]string `json:"headers"`
		CF      struct {
			Colo    string `json:"colo"`
			Country string `json:"country"`
			ASN     int    `json:"asn"`
		} `json:"cf"`
	} `json:"request"`
	Response struct {
		Status int `json:"status"`
	} `json:"response"`
}

type CountItem added in v1.2.0

type CountItem struct {
	Key   string `json:"key"`
	Count int    `json:"count"`
}

CountItem holds a label and occurrence count for ranking tables.

type Diagnosis added in v1.2.0

type Diagnosis struct {
	Owner   Owner  `json:"owner"`
	Code    string `json:"code"`
	Summary string `json:"summary"`
	Hint    string `json:"hint"`
}

Diagnosis describes the classified fault owner, machine-readable code, and actionable hint.

func Classify added in v1.2.0

func Classify(sig Signal) Diagnosis

Classify determines whether a failure belongs to APP, NEXTDEPLOY, PLATFORM, or UNKNOWN.

type ErrRecord added in v1.2.0

type ErrRecord struct {
	TS              string  `json:"ts"`
	RequestID       string  `json:"requestId"`
	RayID           string  `json:"rayId"`
	Method          string  `json:"method,omitempty"`
	URL             string  `json:"url"`
	Path            string  `json:"path"`
	Status          int     `json:"status"`
	Outcome         string  `json:"outcome,omitempty"`
	SegmentPrefetch string  `json:"segmentPrefetch,omitempty"`
	Colo            string  `json:"colo,omitempty"`
	Country         string  `json:"country,omitempty"`
	ASN             int     `json:"asn,omitempty"`
	ScriptVer       string  `json:"scriptVersion,omitempty"`
	Owner           Owner   `json:"owner"`
	Code            string  `json:"code"`
	Summary         string  `json:"summary"`
	Hint            string  `json:"hint"`
	Message         string  `json:"message"`
	WallTimeMs      float64 `json:"wallTimeMs,omitempty"`
	CPUTimeMs       float64 `json:"cpuTimeMs,omitempty"`
}

ErrRecord is the compact, merged, classified representation of one failing request.

type FlexTimestamp added in v1.2.0

type FlexTimestamp string

FlexTimestamp parses either an ISO-8601 string or a Unix-millisecond integer/float.

func (*FlexTimestamp) UnmarshalJSON added in v1.2.0

func (f *FlexTimestamp) UnmarshalJSON(data []byte) error

type IssueGroup added in v1.2.0

type IssueGroup struct {
	Owner        Owner    `json:"owner"`
	Code         string   `json:"code"`
	Summary      string   `json:"summary"`
	Hint         string   `json:"hint"`
	Count        int      `json:"count"`
	SampleRoutes []string `json:"sampleRoutes"`
	ExtraRoutes  int      `json:"extraRoutes,omitempty"`
	SampleDetail string   `json:"sampleDetail,omitempty"`
}

IssueGroup aggregates distinct failing requests sharing the same (Owner, Code).

type LogAggregator

type LogAggregator struct {
	Out           io.Writer
	AppName       string
	ShowTimestamp bool
}

LogAggregator formats and writes log lines from multiple sources.

func NewAggregator

func NewAggregator(out io.Writer, appName string) *LogAggregator

NewAggregator returns a LogAggregator that writes formatted logs to out.

func (*LogAggregator) GetWriter

func (a *LogAggregator) GetWriter(source LogSource) io.Writer

GetWriter returns an io.Writer that tags all writes with source.

func (*LogAggregator) WriteSource

func (a *LogAggregator) WriteSource(source LogSource, p []byte) (n int, err error)

WriteSource formats and writes each non-empty line in p tagged with source.

type LogSource

type LogSource string

LogSource identifies the origin of a log entry.

const (
	SourceApp    LogSource = "APP"
	SourceAudit  LogSource = "AUDIT"
	SourceWAF    LogSource = "WAF"
	SourceDaemon LogSource = "DAEMON"
)

Supported log sources.

type Owner added in v1.2.0

type Owner string

Owner identifies which layer is responsible for an error.

const (
	OwnerApp        Owner = "APP"
	OwnerNextDeploy Owner = "NEXTDEPLOY"
	OwnerPlatform   Owner = "PLATFORM"
	OwnerUnknown    Owner = "UNKNOWN"
)

func ParseOwner added in v1.2.0

func ParseOwner(s string) (Owner, error)

ParseOwner parses a user-supplied owner filter string. An empty string means "no filter" and returns "" with nil error.

type Signal added in v1.2.0

type Signal struct {
	Method     string
	Path       string
	URL        string
	Status     int
	Outcome    string
	Headers    map[string]string
	Messages   []string
	Exceptions []string
}

Signal holds all available request/error signals used to classify a failure.

type SourceWriter

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

SourceWriter implements io.Writer for a specific source.

func (*SourceWriter) Write

func (sw *SourceWriter) Write(p []byte) (n int, err error)

type StreamCollector added in v1.2.0

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

StreamCollector merges multi-fragment Cloudflare log events by requestId and produces an AnalysisReport.

func NewStreamCollector added in v1.2.0

func NewStreamCollector(opts AnalyzeOptions) *StreamCollector

NewStreamCollector constructs a StreamCollector with the provided AnalyzeOptions.

func (*StreamCollector) Add added in v1.2.0

func (c *StreamCollector) Add(e CFExportEntry)

Add ingests a single CFExportEntry into the collector.

func (*StreamCollector) Finalize added in v1.2.0

func (c *StreamCollector) Finalize() *AnalysisReport

Finalize classifies all merged requests, applies filters, emits MergedNDJSONOut, and returns the report.

func (*StreamCollector) SetTruncated added in v1.2.0

func (c *StreamCollector) SetTruncated(t bool)

SetTruncated marks whether the underlying input stream terminated mid-JSON.

Jump to

Keyboard shortcuts

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