logbuffer

package
v0.0.0-...-785df0d Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2025 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package logbuffer implements a fixed-size in-memory ring buffer for line-separated logs. It implements io.Writer and splits the data into lines. The lines are kept in a ring where the oldest are overwritten once it's full. It allows retrieval of the last n lines. There is a built-in line length limit to bound the memory usage at maxLineLength * size.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Line

type Line struct {
	Data           string
	OriginalLength int
}

Line is a line stored in the log buffer - a string, that has been perhaps truncated (due to exceeded limits).

func LineFromLogProto

func LineFromLogProto(raw *lpb.LogEntry_Raw) (*Line, error)

LineFromLogProto converts a Logging-specific protobuf message back into a Line.

func (*Line) ProtoLog

func (l *Line) ProtoLog() *lpb.LogEntry_Raw

ProtoLog returns a Logging-specific protobuf structure.

func (*Line) String

func (l *Line) String() string

String returns the line with an ellipsis at the end (...) if the line has been truncated, or the original line otherwise.

func (*Line) Truncated

func (l *Line) Truncated() bool

Truncated returns whether this line has been truncated to fit limits.

type LineBuffer

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

LineBuffer is a io.WriteCloser that will call a given callback every time a line is completed.

func NewLineBuffer

func NewLineBuffer(maxLineLength int, cb LineBufferCallback) *LineBuffer

NewLineBuffer creates a new LineBuffer with a given line length limit and callback.

func (*LineBuffer) Close

func (l *LineBuffer) Close() error

Close will emit any leftover data in the buffer to the callback. Subsequent calls to Write will fail. Subsequent calls to Close will also fail.

func (*LineBuffer) Sync

func (l *LineBuffer) Sync() error

func (*LineBuffer) Write

func (l *LineBuffer) Write(data []byte) (int, error)

type LineBufferCallback

type LineBufferCallback func(*Line)

LineBufferCallback is a callback that will get called any time the line is completed. The function must not cause another write to the LineBuffer, or the program will deadlock.

type LogBuffer

type LogBuffer struct {
	*LineBuffer
	// contains filtered or unexported fields
}

LogBuffer implements a fixed-size in-memory ring buffer for line-separated logs

func New

func New(size, maxLineLength int) *LogBuffer

New creates a new LogBuffer with a given ringbuffer size and maximum line length.

func (*LogBuffer) ReadLines

func (b *LogBuffer) ReadLines(n int) []Line

ReadLines reads the last n lines from the buffer in chronological order. If n is bigger than the ring buffer or the number of available lines only the number of stored lines are returned.

func (*LogBuffer) ReadLinesTruncated

func (b *LogBuffer) ReadLinesTruncated(n int, ellipsis string) []string

ReadLinesTruncated works exactly the same as ReadLines, but adds an ellipsis at the end of every line that was truncated because it was over MaxLineLength

Jump to

Keyboard shortcuts

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