Go API Documentation

github.com/caddyserver/caddy/v2/modules/caddyhttp/logging

No package summary is available.

Package

Files: 2. Third party imports: 1. Imports from organisation: 0. Tests: 0. Benchmarks: 0.

Vars

Interface guards

var (
	_ caddyfile.Unmarshaler = (*LogAppend)(nil)
)

Types

LogAppend

LogAppend implements a middleware that takes a key and value, where the key is the name of a log field and the value is a placeholder, or variable key, or constant value to use for that field.

type LogAppend struct {
	// Key is the name of the log field.
	Key	string	`json:"key,omitempty"`

	// Value is the value to use for the log field.
	// If it is a placeholder (with surrounding `{}`),
	// it will be evaluated when the log is written.
	// If the value is a key that exists in the `vars`
	// map, the value of that key will be used. Otherwise
	// the value will be used as-is as a constant string.
	Value	string	`json:"value,omitempty"`
}

Functions

func (*LogAppend) UnmarshalCaddyfile

UnmarshalCaddyfile implements caddyfile.Unmarshaler.

func (h *LogAppend) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
	d.Next()	// consume directive name
	if !d.NextArg() {
		return d.ArgErr()
	}
	h.Key = d.Val()
	if !d.NextArg() {
		return d.ArgErr()
	}
	h.Value = d.Val()
	return nil
}

Cognitive complexity: 4, Cyclomatic complexity: 3

func (LogAppend) CaddyModule

CaddyModule returns the Caddy module information.

func (LogAppend) CaddyModule() caddy.ModuleInfo {
	return caddy.ModuleInfo{
		ID:	"http.handlers.log_append",
		New:	func() caddy.Module { return new(LogAppend) },
	}
}

Cognitive complexity: 2, Cyclomatic complexity: 1

func (LogAppend) ServeHTTP

func (h LogAppend) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
	// Run the next handler in the chain first.
	// If an error occurs, we still want to add
	// any extra log fields that we can, so we
	// hold onto the error and return it later.
	handlerErr := next.ServeHTTP(w, r)

	// On the way back up the chain, add the extra log field
	ctx := r.Context()

	vars := ctx.Value(caddyhttp.VarsCtxKey).(map[string]any)
	repl := ctx.Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
	extra := ctx.Value(caddyhttp.ExtraLogFieldsCtxKey).(*caddyhttp.ExtraLogFields)

	var varValue any
	if strings.HasPrefix(h.Value, "{") &&
		strings.HasSuffix(h.Value, "}") &&
		strings.Count(h.Value, "{") == 1 {
		// the value looks like a placeholder, so get its value
		varValue, _ = repl.Get(strings.Trim(h.Value, "{}"))
	} else if val, ok := vars[h.Value]; ok {
		// the value is a key in the vars map
		varValue = val
	} else {
		// the value is a constant string
		varValue = h.Value
	}

	// Add the field to the extra log fields.
	// We use zap.Any because it will reflect
	// to the correct type for us.
	extra.Add(zap.Any(h.Key, varValue))

	return handlerErr
}

Cognitive complexity: 6, Cyclomatic complexity: 5

Uses: caddyhttp.ExtraLogFields, caddyhttp.ExtraLogFieldsCtxKey, caddyhttp.VarsCtxKey, strings.Count, strings.HasPrefix, strings.HasSuffix, strings.Trim, zap.Any.

Private functions

func init

init ()
References: httpcaddyfile.RegisterHandlerDirective.

func parseCaddyfile

parseCaddyfile sets up the log_append handler from Caddyfile tokens. Syntax:

log_append [<matcher>] <key> <value>

parseCaddyfile (h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error)