log

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ErrorCodeContainerNotFound indicates that the requested container was not found
	// in either the container engine or in the log storage.
	ErrorCodeContainerNotFound = "CONTAINER_NOT_FOUND"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Collector

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

Collector monitors Docker containers, collects their logs and saves them to storage backend.

func NewCollector

func NewCollector(
	apiClient DockerClient,
	storage Storage,
	logger *slog.Logger,
	opts CollectorOptions,
) *Collector

NewCollector creates a new log collector that will monitor containers and stream their logs to the provided storage backend.

func (*Collector) Run

func (c *Collector) Run(ctx context.Context) error

Run starts the log collection process, discovering existing containers and watching for new ones. It blocks until the context is cancelled.

type CollectorOptions

type CollectorOptions struct {
	// Containers specifies a list of container names to monitor.
	// If empty, all containers will be monitored.
	Containers []string
}

CollectorOptions are optional parameters used to configure the behavior of the Collector

type Container

type Container struct {
	// ID is the container's unique identifier.
	ID string `json:"id"`

	// Name is the container's canonical name.
	Name string `json:"name"`

	// TTY indicates whether the container has a pseudo-TTY allocated.
	TTY bool `json:"tty"`
}

Container represents information about a Docker container.

type DockerClient

type DockerClient interface {
	// ListContainers returns a slice representing all running containers in Docker.
	ListContainers(ctx context.Context) ([]Container, error)

	// WatchContainersStart watches for new running container.
	WatchContainersStart(ctx context.Context) (<-chan Container, <-chan error)

	// FetchContainerLogs retrieves a stream of logs from a running container. The stream is represented as NDJSON with each line being a representation
	// of a [log.Record].
	// The query specifies which container and what type of logs to retrieve.
	FetchContainerLogs(ctx context.Context, query Query) (io.ReadCloser, error)
}

DockerClient provides access to Docker container operations

type Error

type Error struct {
	// Code is the specific error type that occurred
	Code ErrorCode

	// Message provides human-readable details about the error
	Message string
}

Error represents a domain-specific error.

func (*Error) Error

func (e *Error) Error() string

type ErrorCode

type ErrorCode string

ErrorCode represents specific error conditions that can occur when working with container logs.

type Query

type Query struct {
	// ContainerName is the name of the container to retrieve logs from.
	ContainerName string

	// IncludeStdout indicates whether to include stdout logs in the stream.
	IncludeStdout bool

	// IncludeStderr indicates whether to include stderr logs in the stream.
	IncludeStderr bool

	// Follow indicates whether to stream logs in real-time as they are generated.
	// When true, the connection remains open and new logs are streamed as they appear.
	Follow bool
}

Query represents the parameters for retrieving container logs.

type Record

type Record struct {
	// Timestamp is the time at which the log was emitted by the container.
	Timestamp time.Time `json:"timestamp,omitzero"`

	// Stream identifies the output stream (stdout or stderr).
	Stream StreamType `json:"stream"`

	// Log contains the raw log entry text.
	Log string `json:"output"`
}

Record represents a single log entry from a Docker container.

type Service

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

Service provides a unified interface for accessing container logs from both running containers and persisted storage. It automatically falls back to stored logs when a container cannot be found in Docker.

func NewService

func NewService(
	dockerClient DockerClient,
	storage Storage,
	logger *slog.Logger,
) *Service

NewService creates a new service for retrieving Docker container logs using the given Docker Engine API client or storage as a fallback.

func (*Service) GetContainerLogs

func (s *Service) GetContainerLogs(
	ctx context.Context,
	query Query,
) (io.ReadCloser, error)

GetContainerLogs retrieves logs for the specified container. It first attempts to fetch live logs from Docker, then falls back to stored logs if the container is not found. The returned stream is filtered according to the query parameters.

type Storage

type Storage interface {
	// Create creates a new log file for the specified container and
	// returns an [io.WriteCloser] to write directly to the storage.
	Create(container Container) (io.WriteCloser, error)

	// Open returns a reader for the stored logs of the specified container.
	Open(containerName string) (io.ReadCloser, error)
}

Storage provides access to persisted container logs from a storage backend. It abstracts the underlying storage mechanism (filesystem, cloud storage, etc.).

NOTE: We only wrote a filesystem implementation as for now for the test but we would most likely also accept a context.Context for implementations using the network.

type StreamType

type StreamType string

StreamType identifies the output stream of a log entry.

const (
	// StreamTypeStdout represents standard output stream.
	StreamTypeStdout StreamType = "stdout"
	// StreamTypeStderr represents standard error stream.
	StreamTypeStderr StreamType = "stderr"
)

Jump to

Keyboard shortcuts

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