logtemplate

package
v3.5.0 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrUnknownTag = errors.New("logtemplate: unknown tag")

ErrUnknownTag indicates that the template references a tag that has no registered renderer. The format may be a bare ${tag} or a parametric ${tag:param}; in both cases the unmatched name is reported via UnknownTagError so callers can extract it programmatically.

Functions

func ExecuteChains

func ExecuteChains[C, D any](output Buffer, ctx C, data *D, fixedParts [][]byte, funcChain []Func[C, D]) error

ExecuteChains renders precompiled template chains into output.

func IndexControlByte added in v3.5.0

func IndexControlByte[S ~string | ~[]byte](s S) int

IndexControlByte returns the index of the first byte IsControlByte matches, or -1 if none is present. It scans eight bytes at a time; inputs of 8+ bytes finish with one overlapping word, shorter ones byte-wise.

func IsControlByte added in v3.5.0

func IsControlByte(b byte) bool

IsControlByte reports whether b is an ASCII control byte that must not pass through to a log line. Tab is preserved because operators frequently use it for delimiting structured fields. CR, LF, NUL, and the other C0/DEL bytes are replaced — they are the bytes attackers use to forge log lines or corrupt terminal output via ANSI escape sequences.

func ScrubControls added in v3.5.0

func ScrubControls[S ~string | ~[]byte](s S, idx int) []byte

ScrubControls returns a copy of s with every byte IsControlByte matches replaced by a space. idx is the index of the first such byte, so the scan starts there and the clean prefix is copied untouched.

A negative idx — what IndexControlByte returns for clean input — is clamped to 0 rather than panicking, so ScrubControls(s, IndexControlByte(s)) is safe even though the callers here take the clean fast path instead.

func WriteSanitized added in v3.5.0

func WriteSanitized(output Buffer, p []byte) (int, error)

WriteSanitized writes p to output with ASCII control bytes replaced by spaces. Tabs are preserved. Clean inputs (the common case) forward directly to output.Write with no allocation; dirty inputs are scrubbed into a copy starting at the first control byte.

func WriteSanitizedString added in v3.5.0

func WriteSanitizedString(output Buffer, s string) (int, error)

WriteSanitizedString is WriteSanitized for strings, keeping the clean fast path on output.WriteString.

Types

type Buffer

type Buffer interface {
	Len() int
	ReadFrom(r io.Reader) (int64, error)
	WriteTo(w io.Writer) (int64, error)
	Bytes() []byte
	Write(p []byte) (int, error)
	WriteByte(c byte) error
	WriteString(s string) (int, error)
	Set(p []byte)
	SetString(s string)
	String() string
}

Buffer is the render buffer exposed to template functions. The template renderer only requires Write, WriteByte, and WriteString, but the wider bytebufferpool-compatible surface is kept so existing custom logger tags that use methods such as Len, Bytes, Set, or String continue to compile when switching to the shared template renderer.

type Func

type Func[C, D any] func(output Buffer, ctx C, data *D, extraParam string) (int, error)

Func renders one dynamic template tag.

type Template

type Template[C, D any] struct {
	// contains filtered or unexported fields
}

Template is a precompiled log template.

func Build

func Build[C, D any](format string, tagFunctions map[string]Func[C, D]) (*Template[C, D], error)

Build parses format once and returns a reusable template.

func (*Template[C, D]) Chains

func (t *Template[C, D]) Chains() ([][]byte, []Func[C, D])

Chains returns the fixed template parts and functions used by Execute.

func (*Template[C, D]) Execute

func (t *Template[C, D]) Execute(output Buffer, ctx C, data *D) error

Execute renders the template into output.

type UnknownTagError

type UnknownTagError struct {
	Tag   string
	Param string
	Hint  string
}

UnknownTagError is the typed error returned when a template references an unknown tag. Tag is the offending tag including any parametric suffix (without the surrounding "${" / "}"). Param is the parameter portion when the tag was parametric, or the empty string for bare tags. Hint is an optional human-readable suggestion — currently set when a bare tag was referenced but a parametric base of the same name is registered.

func (*UnknownTagError) Error

func (e *UnknownTagError) Error() string

func (*UnknownTagError) Unwrap

func (*UnknownTagError) Unwrap() error

Jump to

Keyboard shortcuts

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