logger

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package logger provides line-buffered prefix writers and log target forwarding.

Index

Constants

View Source
const DefaultPrefix = "service timestamp"

DefaultPrefix is the default log prefix format: service name followed by timestamp.

Variables

This section is empty.

Functions

This section is empty.

Types

type PrefixWriter

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

PrefixWriter is a line-buffered io.Writer that prefixes each line with configurable components (timestamp and/or service name):

[service-name] 2021-05-13T03:16:51.001Z output line here

The prefix format is controlled by a space-separated token string:

  • "service timestamp" (default): [name] then timestamp
  • "timestamp service": timestamp then [name]
  • "timestamp": timestamp only
  • "service": [name] only
  • "none" or "": no prefix

Partial lines are buffered until a newline. Supports subscribers for live streaming and a ring buffer for recent history.

func NewPrefixWriter

func NewPrefixWriter(dest io.Writer, name, prefix string) *PrefixWriter

NewPrefixWriter creates a new PrefixWriter. The prefix string controls which components appear and in what order. See DefaultPrefix for the default format.

func (*PrefixWriter) AddTarget

func (pw *PrefixWriter) AddTarget(w io.Writer)

AddTarget adds an additional writer that receives the same prefixed output.

func (*PrefixWriter) ClearTargets

func (pw *PrefixWriter) ClearTargets()

ClearTargets removes all additional writers registered via AddTarget, used during reload before re-wiring new ones. Nil (not extra[:0]) releases the backing array so stale writer references become GC-eligible immediately.

func (*PrefixWriter) Flush

func (pw *PrefixWriter) Flush()

Flush writes any remaining buffered content (for shutdown).

func (*PrefixWriter) Recent

func (pw *PrefixWriter) Recent() [][]byte

Recent returns a deep copy of recent prefixed log lines from the ring buffer. Each returned slice is an independent copy so concurrent ring writes cannot overwrite the bytes that callers (e.g. the logs command handler) are reading.

func (*PrefixWriter) Subscribe

func (pw *PrefixWriter) Subscribe() (<-chan []byte, func())

Subscribe returns a channel that receives new prefixed log lines and an unsubscribe function. The channel is buffered to avoid blocking writes.

Received slices alias a reused per-subscription buffer; consumers must consume each synchronously before the next receive. Holding a slice past the next receive risks observing torn writes once the writer reuses the slot.

func (*PrefixWriter) Write

func (pw *PrefixWriter) Write(p []byte) (int, error)

Write implements io.Writer. Peak per-Write memory is bounded to ~maxBufSize: p is consumed in chunks that never grow pw.buf beyond the cap, and a synthetic newline is injected when the cap is reached with no natural newline, so a multi-megabyte newline-free chunk cannot allocate len(p) bytes before the size check fires.

type Target

type Target struct {
	Writer io.WriteCloser
	// contains filtered or unexported fields
}

Target wraps a log forwarding destination.

func NewTarget

func NewTarget(name string, cfg TargetConfig) (*Target, error)

NewTarget creates a new log target from config.

func (*Target) AppliesTo

func (lt *Target) AppliesTo(serviceName string) bool

AppliesTo returns whether this target should receive logs from the given service.

func (*Target) Close

func (lt *Target) Close()

Close closes the target writer.

type TargetConfig

type TargetConfig struct {
	// Labels are prepended to every forwarded line as logfmt-style key=value
	// pairs (sorted by key), so syslog/file consumers can filter or attribute
	// lines by target metadata. Empty = no label prefix.
	Labels   map[string]string
	Type     string // "syslog" or "file"
	Location string // e.g. "udp://logs.example.com:514" or "/var/log/app.log"
	// MaxSize is a human-readable byte size (e.g. "10MiB") at which a file
	// target rotates. Empty = no rotation. File targets only; ignored for syslog.
	MaxSize  string
	Services []string // filter: only these service names
	// MaxFiles is the number of rotated files to keep. Values <= 0 default to
	// defaultMaxFiles when MaxSize is set.
	MaxFiles int
	// Compress enables gzip compression of rotated files. Only meaningful for
	// file targets with MaxSize set.
	Compress bool
}

TargetConfig defines a log forwarding target.

Jump to

Keyboard shortcuts

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