gofiber/fiber · error

logger: RegisterContextTag requires a non-empty name and ext

Error message

logger: RegisterContextTag requires a non-empty name and extractor

What it means

logger.RegisterContextTag registers a custom log format tag in both the logger middleware and the package-level fiberlog registry. It panics if the name is empty or the extract callback is nil, because a tag with no name or no way to produce a value is unusable. Registration is expected at init() time, so the panic surfaces during startup.

Source

Thrown at middleware/logger/context_tag.go:49

	}
}

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")
	}

	fiberlog.MustRegisterContextTag(name, func(output fiberlog.Buffer, ctx any, _ *fiberlog.ContextData, _ string) (int, error) {
		v := extract(ctx)
		if v == "" {
			return 0, nil
		}
		return output.WriteString(v)
	})

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

View on GitHub (pinned to 9a4c7e57fe)

Solutions

  1. Ensure the name argument is a non-empty string and the extract function is non-nil before calling RegisterContextTag.
  2. Skip or log a warning for empty/nil entries when registering from dynamic config.
  3. Register tags from a typed slice of structs with both fields validated at construction.

Example fix

// before
logger.RegisterContextTag("", func(ctx any) string { return "" })
// after
logger.RegisterContextTag("user-id", func(ctx any) string {
    if c, ok := ctx.(fiber.Ctx); ok {
        return c.Locals("userID").(string)
    }
    return ""
})
Defensive patterns

Strategy: validation

Validate before calling

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

Prevention

When it happens

Trigger: Calling logger.RegisterContextTag("", someFunc) or RegisterContextTag("mytag", nil), or a variable-driven call where the name or function came back empty (e.g. from a config map with a missing key).

Common situations: Looping over a config map to register tags and one entry has an empty key or nil function. Copying a registration call and forgetting to fill in the callback. Registering tags conditionally where a branch leaves extract unset.

Related errors


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