gofiber/fiber · error · ErrContextTagInvalid

log: context tag name and function are required

Error message

log: context tag name and function are required

What it means

ErrContextTagInvalid ('log: context tag name and function are required', log/context.go:84) is returned by log.RegisterContextTag (log/context.go:137) and SetContextTemplate when the tag name is empty or the renderer function is nil. A context tag needs both a non-empty name to look it up and a non-nil function to render it, so either missing makes the registration meaningless and is rejected up front.

Source

Thrown at log/context.go:84

	Format string
}

// contextTemplate holds the precompiled format. Loads on the log hot path
// happen lock-free; rebuilds (write side) hold contextMu.
var contextTemplate atomic.Pointer[logtemplate.Template[any, ContextData]]

var (
	// contextMu guards rebuilds of contextFormat / contextTags. Readers of
	// the compiled template (writeContext) use contextTemplate.Load directly.
	contextMu     sync.RWMutex
	contextFormat = DefaultFormat
	contextTags   = defaultContextTagMap()
)

var (
	// ErrContextTagInvalid is returned by RegisterContextTag and SetContextTemplate
	// when the supplied tag name or renderer is empty.
	ErrContextTagInvalid = errors.New("log: context tag name and function are required")
	// ErrContextTagReserved is returned by RegisterContextTag and SetContextTemplate
	// when the caller attempts to override the reserved TagContextValue ("value:") tag.
	ErrContextTagReserved = errors.New("log: context tag is reserved")
)

// SetContextTemplate configures contextual fields rendered by WithContext for Fiber's default logger.
// Pass an empty ContextConfig (or ContextConfig{Format: DefaultFormat}) to disable contextual fields.
// It returns an error if config.Format cannot be parsed or if config.CustomTags attempts to
// override the reserved TagContextValue tag.
func SetContextTemplate(config ContextConfig) error {
	if _, ok := config.CustomTags[TagContextValue]; ok {
		return ErrContextTagReserved
	}

	contextMu.Lock()
	defer contextMu.Unlock()

	// Cloning the live tag map preserves prior RegisterContextTag entries —

View on GitHub (pinned to 9a4c7e57fe)

Solutions

  1. Validate that the tag name is non-empty and the function is non-nil before registering.
  2. Derive tag names from non-empty constants rather than dynamic strings that may be empty.
  3. Use the non-Must variants in code that loads config, so an invalid tag returns an error instead of panicking.

Example fix

// before
log.MustRegisterContextTag(name, fn) // panics if name=="" or fn==nil

// after
if name != "" && fn != nil {
    if err := log.RegisterContextTag(name, fn); err != nil {
        log.Printf("skip tag %q: %v", name, err)
    }
}
Defensive patterns

Strategy: validation

Validate before calling

// Skip registrations that lack a name or function.
if name != "" && fn != nil {
    if err := log.RegisterContextTag(name, fn); err != nil {
        log.Printf("skip tag %q: %v", name, err)
    }
}

Try / catch

if err := log.RegisterContextTag(name, fn); err != nil {
    if errors.Is(err, log.ErrContextTagInvalid) {
        log.Printf("ignoring invalid tag registration for %q", name)
        return
    }
    return err
}

Prevention

When it happens

Trigger: Calling RegisterContextTag("", fn) or RegisterContextTag("foo", nil). Also SetContextTemplate with a CustomTags entry whose key is empty or whose ContextTagFunc is nil. MustRegisterContextTag/MustSetContextTemplate panic with this error instead of returning it.

Common situations: Building tag names from configuration that can be empty, refactoring that leaves a nil function placeholder, or copy-pasting a registration and dropping the function.

Related errors


AI-assisted analysis of gofiber/fiber@9a4c7e57fe (2026-08-04). Data as JSON: /data/errors/4c14afd0ae3c64cb.json. Report an issue: GitHub.