{"id":"4c14afd0ae3c64cb","repo":"gofiber/fiber","slug":"log-context-tag-name-and-function-are-required","errorCode":null,"errorMessage":"log: context tag name and function are required","messagePattern":"log: context tag name and function are required","errorType":"validation","errorClass":"ErrContextTagInvalid","httpStatus":null,"severity":"error","filePath":"log/context.go","lineNumber":84,"sourceCode":"\tFormat string\n}\n\n// contextTemplate holds the precompiled format. Loads on the log hot path\n// happen lock-free; rebuilds (write side) hold contextMu.\nvar contextTemplate atomic.Pointer[logtemplate.Template[any, ContextData]]\n\nvar (\n\t// contextMu guards rebuilds of contextFormat / contextTags. Readers of\n\t// the compiled template (writeContext) use contextTemplate.Load directly.\n\tcontextMu     sync.RWMutex\n\tcontextFormat = DefaultFormat\n\tcontextTags   = defaultContextTagMap()\n)\n\nvar (\n\t// ErrContextTagInvalid is returned by RegisterContextTag and SetContextTemplate\n\t// when the supplied tag name or renderer is empty.\n\tErrContextTagInvalid = errors.New(\"log: context tag name and function are required\")\n\t// ErrContextTagReserved is returned by RegisterContextTag and SetContextTemplate\n\t// when the caller attempts to override the reserved TagContextValue (\"value:\") tag.\n\tErrContextTagReserved = errors.New(\"log: context tag is reserved\")\n)\n\n// SetContextTemplate configures contextual fields rendered by WithContext for Fiber's default logger.\n// Pass an empty ContextConfig (or ContextConfig{Format: DefaultFormat}) to disable contextual fields.\n// It returns an error if config.Format cannot be parsed or if config.CustomTags attempts to\n// override the reserved TagContextValue tag.\nfunc SetContextTemplate(config ContextConfig) error {\n\tif _, ok := config.CustomTags[TagContextValue]; ok {\n\t\treturn ErrContextTagReserved\n\t}\n\n\tcontextMu.Lock()\n\tdefer contextMu.Unlock()\n\n\t// Cloning the live tag map preserves prior RegisterContextTag entries —","sourceCodeStart":66,"sourceCodeEnd":102,"githubUrl":"https://github.com/gofiber/fiber/blob/9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c/log/context.go#L66-L102","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Validate that the tag name is non-empty and the function is non-nil before registering.","Derive tag names from non-empty constants rather than dynamic strings that may be empty.","Use the non-Must variants in code that loads config, so an invalid tag returns an error instead of panicking."],"exampleFix":"// before\nlog.MustRegisterContextTag(name, fn) // panics if name==\"\" or fn==nil\n\n// after\nif name != \"\" && fn != nil {\n    if err := log.RegisterContextTag(name, fn); err != nil {\n        log.Printf(\"skip tag %q: %v\", name, err)\n    }\n}","handlingStrategy":"validation","validationCode":"// Skip registrations that lack a name or function.\nif name != \"\" && fn != nil {\n    if err := log.RegisterContextTag(name, fn); err != nil {\n        log.Printf(\"skip tag %q: %v\", name, err)\n    }\n}","typeGuard":null,"tryCatchPattern":"if err := log.RegisterContextTag(name, fn); err != nil {\n    if errors.Is(err, log.ErrContextTagInvalid) {\n        log.Printf(\"ignoring invalid tag registration for %q\", name)\n        return\n    }\n    return err\n}","preventionTips":["Always supply a non-empty name and non-nil function when registering a tag.","Derive tag names from non-empty constants rather than dynamic strings.","Prefer the non-Must APIs when loading config so invalid tags error instead of panic."],"tags":["logging","context","config","validation","fiber"],"analyzedSha":"9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c","analyzedAt":"2026-08-04T21:44:03.395Z","schemaVersion":2}