{"id":"a1d93af2a0d179c7","repo":"gofiber/fiber","slug":"logger-registercontexttag-requires-a-non-empty-na","errorCode":null,"errorMessage":"logger: RegisterContextTag requires a non-empty name and extractor","messagePattern":"logger: RegisterContextTag requires a non-empty name and extractor","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"middleware/logger/context_tag.go","lineNumber":49,"sourceCode":"\t}\n}\n\nfunc emptyLogTag(_ Buffer, _ fiber.Ctx, _ *Data, _ string) (int, error) {\n\treturn 0, nil\n}\n\n// RegisterContextTag registers a string-valued tag in both the logger\n// middleware tag registry and the package-level fiberlog context tag\n// registry, so that the same name can be used in a logger.Config.Format and\n// in fiberlog.SetContextTemplate. extract receives the raw context value\n// (fiber.Ctx, *fasthttp.RequestCtx, or context.Context) and returns the\n// rendered string; an empty return renders nothing.\n//\n// Re-registering a name replaces both renderers. Panics if name is empty or\n// extract is nil — registration is expected at init time.\nfunc RegisterContextTag(name string, extract func(ctx any) string) {\n\tif name == \"\" || extract == nil {\n\t\tpanic(\"logger: RegisterContextTag requires a non-empty name and extractor\")\n\t}\n\n\tfiberlog.MustRegisterContextTag(name, func(output fiberlog.Buffer, ctx any, _ *fiberlog.ContextData, _ string) (int, error) {\n\t\tv := extract(ctx)\n\t\tif v == \"\" {\n\t\t\treturn 0, nil\n\t\t}\n\t\treturn output.WriteString(v)\n\t})\n\n\tMustRegisterTag(name, func(output Buffer, c fiber.Ctx, _ *Data, _ string) (int, error) {\n\t\tv := extract(c)\n\t\tif v == \"\" {\n\t\t\treturn 0, nil\n\t\t}\n\t\treturn output.WriteString(v)\n\t})\n}","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/gofiber/fiber/blob/9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c/middleware/logger/context_tag.go#L31-L67","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Ensure the name argument is a non-empty string and the extract function is non-nil before calling RegisterContextTag.","Skip or log a warning for empty/nil entries when registering from dynamic config.","Register tags from a typed slice of structs with both fields validated at construction."],"exampleFix":"// before\nlogger.RegisterContextTag(\"\", func(ctx any) string { return \"\" })\n// after\nlogger.RegisterContextTag(\"user-id\", func(ctx any) string {\n    if c, ok := ctx.(fiber.Ctx); ok {\n        return c.Locals(\"userID\").(string)\n    }\n    return \"\"\n})","handlingStrategy":"validation","validationCode":"func registerTag(name string, extract func(ctx any) string) {\n    if name == \"\" || extract == nil {\n        log.Printf(\"skipping context tag registration: name=%q extract-nil=%v\", name, extract == nil)\n        return\n    }\n    logger.RegisterContextTag(name, extract)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Register tags from a typed slice of {Name string; Extract func} structs built with both fields.","Skip and log empty/nil entries when registering from dynamic config instead of passing them through."],"tags":["logger","config","registration","panic"],"analyzedSha":"9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c","analyzedAt":"2026-08-04T21:44:03.395Z","schemaVersion":2}