gofiber/fiber · error

logger: RegisterContextTag requires a non-empty name and…

Error message

logger: RegisterContextTag requires a non-empty name and extractor

What it means

logger.RegisterContextTag panics when name is empty or the extract callback is nil. Registration is expected to run at init/startup time (MustRegisterTag underneath also panics on bad input), so a malformed call would otherwise silently register nothing and the tag would render empty in logs. The check makes the failure loud.

Solutions

  1. Pass a non-empty name and a non-nil extractor: logger.RegisterContextTag("tenant", func(ctx any) string { ... }).
  2. Skip registration conditionally: if name != "" && extract != nil { logger.RegisterContextTag(name, extract) }.
  3. Register tags from an init() guarded by a build/runtime check so empty values never reach the call.

Example fix

// before
logger.RegisterContextTag("", func(ctx any) string {
    return ctxValue(ctx)
})

// after
logger.RegisterContextTag("tenant", func(ctx any) string {
    return ctxValue(ctx)
})
Defensive patterns

Strategy: validation

Validate before calling

func registerCtxTag(name string, extract func(any) string) {
    if name == "" || extract == nil {
        log.Printf("skipping context tag registration: name=%q extractNil=%v", name, extract == nil)
        return
    }
    logger.RegisterContextTag(name, extract)
}

Type guard

func isValidTagRegistration(name string, extract func(any) string) bool {
    return name != "" && extract != nil
}

Prevention

When it happens

Trigger: logger.RegisterContextTag("", extract), logger.RegisterContextTag("tenant", nil), or a dynamically-built name/extractor that evaluates to empty/nil on some code path.

Common situations: Looping over a config map to register tags where one entry has an empty name; a helper that returns nil extract when a feature flag is off; copy-paste that drops the extractor argument.

Related errors


AI-assisted analysis of gofiber/fiber@a105acad6c (2026-08-11). Data as JSON: /api/errors/a1d93af2a0d179c7. Report an issue: GitHub.

Appendix: source

Thrown at middleware/logger/context_tag.go:50

	}
}

func emptyLogTag(_ Buffer, _ fiber.Ctx, _ *Data, _ string) (int, error) {
	return 0, nil
}

// RegisterContextTag registers a string-valued tag in both the logger
// middleware tag registry and the package-level fiberlog context tag
// registry, so that the same name can be used in a logger.Config.Format and
// in fiberlog.SetContextTemplate. extract receives the raw context value
// (fiber.Ctx, *fasthttp.RequestCtx, or context.Context) and returns the
// rendered string; an empty return renders nothing.
//
// Re-registering a name replaces both renderers. Panics if name is empty or
// extract is nil — registration is expected at init time.
func RegisterContextTag(name string, extract func(ctx any) string) {
	if name == "" || extract == nil {
		panic("logger: RegisterContextTag requires a non-empty name and extractor")
	}

	// Sanitized like every built-in tag: what extract returns is whatever the
	// application pulled out of the context, in practice request data, so writing
	// it raw lets a CR or LF close the line and start another.
	fiberlog.MustRegisterContextTag(name, func(output fiberlog.Buffer, ctx any, _ *fiberlog.ContextData, _ string) (int, error) {
		v := extract(ctx)
		if v == "" {
			return 0, nil
		}
		return logtemplate.WriteSanitizedString(output, v)
	})

	MustRegisterTag(name, func(output Buffer, c fiber.Ctx, _ *Data, _ string) (int, error) {
		v := extract(c)
		if v == "" {
			return 0, nil
		}

View on GitHub (pinned to a105acad6c)