log
import "github.com/gofiber/fiber/v3/log"
Constants
Tag names registered by Fiber's built-in middlewares. Treat them as the canonical identifiers for the values produced by requestid, basicauth, keyauth, csrf, and session — keeping format strings derived from these constants means renaming a tag here cascades automatically.
const (
TagRequestID = "requestid"
TagRequestIDDashed = "request-id"
TagUsername = "username"
TagAPIKey = "api-key"
TagCSRFToken = "csrf-token"
TagSessionID = "session-id"
)const (
// DefaultFormat disables contextual fields for the default logger.
DefaultFormat = ""
// RequestIDFormat renders the request ID registered by the requestid middleware.
RequestIDFormat = "[${" + TagRequestID + "}] "
// KeyValueFormat renders commonly registered middleware context values as
// key/value fields. Sensitive values (api-key, csrf-token, session-id) are
// redacted by the registering middleware before reaching the log line. A
// value holding a space, a tab, '=' or '"' is quoted, see SetContextTemplate.
KeyValueFormat = "request-id=${" + TagRequestIDDashed + "} " +
"username=${" + TagUsername + "} " +
"api-key=${" + TagAPIKey + "} " +
"csrf-token=${" + TagCSRFToken + "} " +
"session-id=${" + TagSessionID + "} "
)TagContextValue reads a value from the bound context-like value using the tag parameter as the key. Use it inside a format string as ${value:KEY}. The trailing colon is required: it marks the tag as parametric. Registering a tag named "value:" via RegisterContextTag is rejected — the renderer is reserved for context-value lookups.
const TagContextValue = "value:"The levels of logs.
const (
LevelTrace Level = iota
LevelDebug
LevelInfo
LevelWarn
LevelError
LevelFatal
LevelPanic
)Variables
var (
// ErrContextTagInvalid is returned by RegisterContextTag when the supplied tag
// name is empty or the renderer is nil. SetContextTemplate does not validate
// ContextConfig.CustomTags and never returns it.
ErrContextTagInvalid = errors.New("log: context tag name and function are required")
// ErrContextTagReserved is returned by RegisterContextTag and SetContextTemplate
// when the caller attempts to override the reserved TagContextValue ("value:") tag.
ErrContextTagReserved = errors.New("log: context tag is reserved")
)Functions
func Debug(v ...any)
Debug calls the default logger's Debug method.
func Debugf(format string, v ...any)
Debugf calls the default logger's Debugf method.
func Debugw(msg string, keysAndValues ...any)
Debugw logs a message with some additional context. The variadic key-value pairs are treated as they are privateLog With.
func DefaultLogger[T any]() AllLogger[T]
DefaultLogger returns the default logger.
func Error(v ...any)
Error calls the default logger's Error method.
func Errorf(format string, v ...any)
Errorf calls the default logger's Errorf method.
func Errorw(msg string, keysAndValues ...any)
Errorw logs a message with some additional context. The variadic key-value pairs are treated as they are privateLog With.
func Fatal(v ...any)
Fatal calls the default logger's Fatal method and then os.Exit(1).
func Fatalf(format string, v ...any)
Fatalf calls the default logger's Fatalf method and then os.Exit(1).
func Fatalw(msg string, keysAndValues ...any)
Fatalw logs a message with some additional context. The variadic key-value pairs are treated as they are privateLog With.
func Info(v ...any)
Info calls the default logger's Info method.
func Infof(format string, v ...any)
Infof calls the default logger's Infof method.
func Infow(msg string, keysAndValues ...any)
Infow logs a message with some additional context. The variadic key-value pairs are treated as they are privateLog With.
func MustRegisterContextTag(tag string, fn ContextTagFunc)
MustRegisterContextTag registers a contextual tag and panics if registration fails.
func MustSetContextTemplate(config ContextConfig)
MustSetContextTemplate configures contextual fields and panics if the format cannot be parsed.
func Panic(v ...any)
Panic calls the default logger's Panic method.
func Panicf(format string, v ...any)
Panicf calls the default logger's Panicf method.
func Panicw(msg string, keysAndValues ...any)
Panicw logs a message with some additional context. The variadic key-value pairs are treated as they are privateLog With.
func RegisterContextTag(tag string, fn ContextTagFunc) error
RegisterContextTag registers a contextual tag that can be used by SetContextTemplate. Re-registering a tag replaces the existing tag function. Registration is package-global; prefer ContextConfig.CustomTags for per-application overrides. The reserved TagContextValue tag cannot be registered.
func SetContextTemplate(config ContextConfig) error
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 the reserved TagContextValue tag. A tag right behind a '=' is the value of a key=value field: when it holds a space, a tab, '=' or '"' it is written as a Go quoted string.
func SetLevel(lv Level)
SetLevel sets the level of logs below which logs will not be output. The default logger is LevelTrace. Note that this method is not concurrent-safe.
func SetLogger[T any](v AllLogger[T])
SetLogger sets the default logger and the system logger. Note that this method is not concurrent-safe and must not be called after the use of DefaultLogger and global functions from this package.
func SetOutput(w io.Writer)
SetOutput sets the output of default logger and system logger. By default, it is stderr.
func Trace(v ...any)
Trace calls the default logger's Trace method.
func Tracef(format string, v ...any)
Tracef calls the default logger's Tracef method.
func Tracew(msg string, keysAndValues ...any)
Tracew logs a message with some additional context. The variadic key-value pairs are treated as they are privateLog With.
func Warn(v ...any)
Warn calls the default logger's Warn method.
func Warnf(format string, v ...any)
Warnf calls the default logger's Warnf method.
func Warnw(msg string, keysAndValues ...any)
Warnw logs a message with some additional context. The variadic key-value pairs are treated as they are privateLog With.
func WithContext(ctx any) CommonLogger
WithContext binds the default logger to the provided context and returns the contextualized logger.
Types
type AllLogger
AllLogger is the combination of CommonLogger, context binding, and ConfigurableLogger. Custom extensions can be made through AllLogger
type AllLogger[T any] interface {
CommonLogger
ConfigurableLogger[T]
// WithContext returns a new logger with the given context.
WithContext(ctx any) CommonLogger
}Methods
CommonLoggerConfigurableLogger[T]WithContext func(ctx any) CommonLoggerWithContext returns a new logger with the given context.
type Buffer
Buffer abstracts the buffer operations used when rendering contextual log fields.
type Buffer = logtemplate.Buffertype CommonLogger
CommonLogger is the set of logging operations available across Fiber's logging implementations.
type CommonLogger interface {
Logger
FormatLogger
WithLogger
}Methods
LoggerFormatLoggerWithLogger
type ConfigurableLogger
ConfigurableLogger provides methods to config a logger.
type ConfigurableLogger[T any] interface {
// SetLevel sets logging level.
//
// Available levels: Trace, Debug, Info, Warn, Error, Fatal, Panic.
SetLevel(level Level)
// SetOutput sets the logger output.
SetOutput(w io.Writer)
// Logger returns the logger instance. It can be used to adjust the logger configurations in case of need.
Logger() T
}Methods
SetLevel func(level Level)SetLevel sets logging level.
Available levels: Trace, Debug, Info, Warn, Error, Fatal, Panic.
SetOutput func(w io.Writer)SetOutput sets the logger output.
Logger func() TLogger returns the logger instance. It can be used to adjust the logger configurations in case of need.
type ContextConfig
ContextConfig defines how WithContext enriches logs emitted by Fiber's default logger.
type ContextConfig struct {
// CustomTags defines additional contextual tags available to Format.
// The built-in TagContextValue ("value:") tag cannot be overridden.
CustomTags map[string]ContextTagFunc
// Format defines the contextual prefix rendered before the log message.
// Use CustomTags to expose package-specific values such as request IDs.
Format string
}Fields
CustomTags map[string]ContextTagFuncCustomTags defines additional contextual tags available to Format. The built-in TagContextValue ("value:") tag cannot be overridden.
Format stringFormat defines the contextual prefix rendered before the log message. Use CustomTags to expose package-specific values such as request IDs.
type ContextData
ContextData is reserved for data shared by contextual log tags. It currently has no fields; the type exists so the ContextTagFunc signature can evolve without breaking custom-tag implementations.
type ContextData struct{}type ContextTagFunc
ContextTagFunc renders one contextual log tag.
type ContextTagFunc = logtemplate.Func[any, ContextData]type FormatLogger
FormatLogger is a logger interface that output logs with a format.
type FormatLogger interface {
Tracef(format string, v ...any)
Debugf(format string, v ...any)
Infof(format string, v ...any)
Warnf(format string, v ...any)
Errorf(format string, v ...any)
Fatalf(format string, v ...any)
Panicf(format string, v ...any)
}Methods
Tracef func(format string, v ...any)Debugf func(format string, v ...any)Infof func(format string, v ...any)Warnf func(format string, v ...any)Errorf func(format string, v ...any)Fatalf func(format string, v ...any)Panicf func(format string, v ...any)
type Level
Level defines the priority of a log message. When a logger is configured with a level, any log message with a lower log level (smaller by integer comparison) will not be output.
type Level inttype Logger
Logger is a logger interface that provides logging function with levels.
type Logger interface {
Trace(v ...any)
Debug(v ...any)
Info(v ...any)
Warn(v ...any)
Error(v ...any)
Fatal(v ...any)
Panic(v ...any)
}Methods
Trace func(v ...any)Debug func(v ...any)Info func(v ...any)Warn func(v ...any)Error func(v ...any)Fatal func(v ...any)Panic func(v ...any)
type WithLogger
WithLogger is a logger interface that output logs with a message and key-value pairs.
type WithLogger interface {
Tracew(msg string, keysAndValues ...any)
Debugw(msg string, keysAndValues ...any)
Infow(msg string, keysAndValues ...any)
Warnw(msg string, keysAndValues ...any)
Errorw(msg string, keysAndValues ...any)
Fatalw(msg string, keysAndValues ...any)
Panicw(msg string, keysAndValues ...any)
}Methods
Tracew func(msg string, keysAndValues ...any)Debugw func(msg string, keysAndValues ...any)Infow func(msg string, keysAndValues ...any)Warnw func(msg string, keysAndValues ...any)Errorw func(msg string, keysAndValues ...any)Fatalw func(msg string, keysAndValues ...any)Panicw func(msg string, keysAndValues ...any)