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 t
(config ContextConfig)
| 94 | // It returns an error if config.Format cannot be parsed or if config.CustomTags attempts to |
| 95 | // override the reserved TagContextValue tag. |
| 96 | func SetContextTemplate(config ContextConfig) error { |
| 97 | if _, ok := config.CustomTags[TagContextValue]; ok { |
| 98 | return ErrContextTagReserved |
| 99 | } |
| 100 | |
| 101 | contextMu.Lock() |
| 102 | defer contextMu.Unlock() |
| 103 | |
| 104 | // Cloning the live tag map preserves prior RegisterContextTag entries — |
| 105 | // callers that interleave RegisterContextTag with SetContextTemplate |
| 106 | // expect the registration to remain visible. CustomTags layer on top. |
| 107 | tags := maps.Clone(contextTags) |
| 108 | maps.Copy(tags, config.CustomTags) |
| 109 | tags[TagContextValue] = defaultContextValueTag |
| 110 | |
| 111 | var tmpl *logtemplate.Template[any, ContextData] |
| 112 | if config.Format != "" { |
| 113 | var err error |
| 114 | tmpl, err = logtemplate.Build[any, ContextData](config.Format, tags) |
| 115 | if err != nil { |
| 116 | return err |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | contextFormat = config.Format |
| 121 | contextTags = tags |
| 122 | contextTemplate.Store(tmpl) |
| 123 | return nil |
| 124 | } |
| 125 | |
| 126 | // MustSetContextTemplate configures contextual fields and panics if the format cannot be parsed. |
| 127 | func MustSetContextTemplate(config ContextConfig) { |