Internal

internal/logtemplate

github.com/gofiber/fiber/v3/internal/logtemplate

import "github.com/gofiber/fiber/v3/internal/logtemplate"

Variables

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.

Source: internal/logtemplate/errors.go:12

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

Functions

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

Source: internal/logtemplate/template.go:45

Build parses format once and returns a reusable template.

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

Source: internal/logtemplate/template.go:122

ExecuteChains renders precompiled template chains into output.

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

Source: internal/logtemplate/sanitize.go:59

IndexControlByte returns the index of the first byte IsControlByte matches, or -1 if none is present.

The set it looks for — C0 controls except HTAB, plus DEL, with bytes >= 0x80 never matching — is exactly the one utils.IndexControlExceptTab scans for, so this is a thin alias over that helper rather than a second SWAR loop to keep in step. utils unrolls two words per branch and folds the whole test into one arithmetic mask, which measures ~28% faster on a 12-byte path and ~45% faster on a 120-byte user agent than the single-word loop this replaced.

func IsControlByte(b byte) bool

Source: internal/logtemplate/sanitize.go:68

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[S ~string | ~[]byte](s S, idx int) []byte

Source: internal/logtemplate/sanitize.go:36

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(output Buffer, p []byte) (int, error)

Source: internal/logtemplate/sanitize.go:11

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(output Buffer, s string) (int, error)

Source: internal/logtemplate/sanitize.go:21

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

Types

type Buffer

Source: internal/logtemplate/template.go:22

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 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
}
Methods
  • Len func() int
  • ReadFrom func(r io.Reader) (int64, error)
  • WriteTo func(w io.Writer) (int64, error)
  • Bytes func() []byte
  • Write func(p []byte) (int, error)
  • WriteByte func(c byte) error
  • WriteString func(s string) (int, error)
  • Set func(p []byte)
  • SetString func(s string)
  • String func() string

type Func

Source: internal/logtemplate/template.go:36

Func renders one dynamic template tag.

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

type Template

Source: internal/logtemplate/template.go:39

Template is a precompiled log template.

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

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

Source: internal/logtemplate/template.go:106

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

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

Source: internal/logtemplate/template.go:114

Execute renders the template into output.

type UnknownTagError

Source: internal/logtemplate/errors.go:20

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.

type UnknownTagError struct {
	Tag   string
	Param string
	Hint  string
}
Fields
  • Tag string
  • Param string
  • Hint string

func (e *UnknownTagError) Error() string

Source: internal/logtemplate/errors.go:26

func (*UnknownTagError) Unwrap() error

Source: internal/logtemplate/errors.go:34