Log

log

github.com/gofiber/fiber/v3/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.

Source: log/context.go:30

const (
	TagRequestID       = "requestid"
	TagRequestIDDashed = "request-id"
	TagUsername        = "username"
	TagAPIKey          = "api-key"
	TagCSRFToken       = "csrf-token"
	TagSessionID       = "session-id"
)

Source: log/context.go:40

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.

Source: log/context.go:23

const TagContextValue = "value:"

The levels of logs.

Source: log/log.go:96

const (
	LevelTrace Level = iota
	LevelDebug
	LevelInfo
	LevelWarn
	LevelError
	LevelFatal
	LevelPanic
)

Variables

Source: log/context.go:91

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)

Source: log/fiberlog.go:28

Debug calls the default logger's Debug method.

func Debugf(format string, v ...any)

Source: log/fiberlog.go:63

Debugf calls the default logger's Debugf method.

func Debugw(msg string, keysAndValues ...any)

Source: log/fiberlog.go:85

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]

Source: log/default.go:344

DefaultLogger returns the default logger.

func Error(v ...any)

Source: log/fiberlog.go:13

Error calls the default logger's Error method.

func Errorf(format string, v ...any)

Source: log/fiberlog.go:48

Errorf calls the default logger's Errorf method.

func Errorw(msg string, keysAndValues ...any)

Source: log/fiberlog.go:103

Errorw logs a message with some additional context. The variadic key-value pairs are treated as they are privateLog With.

func Fatal(v ...any)

Source: log/fiberlog.go:8

Fatal calls the default logger's Fatal method and then os.Exit(1).

func Fatalf(format string, v ...any)

Source: log/fiberlog.go:43

Fatalf calls the default logger's Fatalf method and then os.Exit(1).

func Fatalw(msg string, keysAndValues ...any)

Source: log/fiberlog.go:109

Fatalw logs a message with some additional context. The variadic key-value pairs are treated as they are privateLog With.

func Info(v ...any)

Source: log/fiberlog.go:23

Info calls the default logger's Info method.

func Infof(format string, v ...any)

Source: log/fiberlog.go:58

Infof calls the default logger's Infof method.

func Infow(msg string, keysAndValues ...any)

Source: log/fiberlog.go:91

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)

Source: log/context.go:172

MustRegisterContextTag registers a contextual tag and panics if registration fails.

func MustSetContextTemplate(config ContextConfig)

Source: log/context.go:133

MustSetContextTemplate configures contextual fields and panics if the format cannot be parsed.

func Panic(v ...any)

Source: log/fiberlog.go:38

Panic calls the default logger's Panic method.

func Panicf(format string, v ...any)

Source: log/fiberlog.go:73

Panicf calls the default logger's Panicf method.

func Panicw(msg string, keysAndValues ...any)

Source: log/fiberlog.go:115

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

Source: log/context.go:143

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

Source: log/context.go:102

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)

Source: log/fiberlog.go:140

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])

Source: log/fiberlog.go:128

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)

Source: log/fiberlog.go:133

SetOutput sets the output of default logger and system logger. By default, it is stderr.

func Trace(v ...any)

Source: log/fiberlog.go:33

Trace calls the default logger's Trace method.

func Tracef(format string, v ...any)

Source: log/fiberlog.go:68

Tracef calls the default logger's Tracef method.

func Tracew(msg string, keysAndValues ...any)

Source: log/fiberlog.go:79

Tracew logs a message with some additional context. The variadic key-value pairs are treated as they are privateLog With.

func Warn(v ...any)

Source: log/fiberlog.go:18

Warn calls the default logger's Warn method.

func Warnf(format string, v ...any)

Source: log/fiberlog.go:53

Warnf calls the default logger's Warnf method.

func Warnw(msg string, keysAndValues ...any)

Source: log/fiberlog.go:97

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

Source: log/fiberlog.go:121

WithContext binds the default logger to the provided context and returns the contextualized logger.

Types

type AllLogger

Source: log/log.go:81

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
  • CommonLogger
  • ConfigurableLogger[T]
  • WithContext func(ctx any) CommonLogger

    WithContext returns a new logger with the given context.

type Buffer

Source: log/context.go:55

Buffer abstracts the buffer operations used when rendering contextual log fields.

type Buffer = logtemplate.Buffer

type CommonLogger

Source: log/log.go:59

CommonLogger is the set of logging operations available across Fiber's logging implementations.

type CommonLogger interface {
	Logger
	FormatLogger
	WithLogger
}
Methods
  • Logger
  • FormatLogger
  • WithLogger

type ConfigurableLogger

Source: log/log.go:66

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() T

    Logger returns the logger instance. It can be used to adjust the logger configurations in case of need.

type ContextConfig

Source: log/context.go:66

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]ContextTagFunc

    CustomTags defines additional contextual tags available to Format. The built-in TagContextValue ("value:") tag cannot be overridden.

  • Format string

    Format defines the contextual prefix rendered before the log message. Use CustomTags to expose package-specific values such as request IDs.

type ContextData

Source: log/context.go:60

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

Source: log/context.go:63

ContextTagFunc renders one contextual log tag.

type ContextTagFunc = logtemplate.Func[any, ContextData]

type FormatLogger

Source: log/log.go:36

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

Source: log/log.go:92

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 int

type Logger

Source: log/log.go:25

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

Source: log/log.go:47

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)