log

package
v3.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 22, 2026 License: MIT Imports: 15 Imported by: 90

Documentation

Index

Constants

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

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.

View Source
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.
	KeyValueFormat = "request-id=${" + TagRequestIDDashed + "} " +
		"username=${" + TagUsername + "} " +
		"api-key=${" + TagAPIKey + "} " +
		"csrf-token=${" + TagCSRFToken + "} " +
		"session-id=${" + TagSessionID + "} "
)
View Source
const TagContextValue = "value:"

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.

Variables

View Source
var (
	// ErrContextTagInvalid is returned by RegisterContextTag and SetContextTemplate
	// when the supplied tag name or renderer is empty.
	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

func Debug(v ...any)

Debug calls the default logger's Debug method.

func Debugf

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

Debugf calls the default logger's Debugf method.

func Debugw

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 Error

func Error(v ...any)

Error calls the default logger's Error method.

func Errorf

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

Errorf calls the default logger's Errorf method.

func Errorw

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

func Fatal(v ...any)

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

func Fatalf

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

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

func Fatalw

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

func Info(v ...any)

Info calls the default logger's Info method.

func Infof

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

Infof calls the default logger's Infof method.

func Infow

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 added in v3.3.0

func MustRegisterContextTag(tag string, fn ContextTagFunc)

MustRegisterContextTag registers a contextual tag and panics if registration fails.

func MustSetContextTemplate added in v3.3.0

func MustSetContextTemplate(config ContextConfig)

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

func Panic

func Panic(v ...any)

Panic calls the default logger's Panic method.

func Panicf

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

Panicf calls the default logger's Tracef method.

func Panicw

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 added in v3.3.0

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 added in v3.3.0

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.

func SetLevel

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

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

func SetOutput(w io.Writer)

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

func Trace

func Trace(v ...any)

Trace calls the default logger's Trace method.

func Tracef

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

Tracef calls the default logger's Tracef method.

func Tracew

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

func Warn(v ...any)

Warn calls the default logger's Warn method.

func Warnf

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

Warnf calls the default logger's Warnf method.

func Warnw

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.

Types

type AllLogger

type AllLogger[T any] interface {
	CommonLogger
	ConfigurableLogger[T]

	// WithContext returns a new logger with the given context.
	WithContext(ctx any) CommonLogger
}

AllLogger is the combination of CommonLogger, context binding, and ConfigurableLogger. Custom extensions can be made through AllLogger

func DefaultLogger

func DefaultLogger[T any]() AllLogger[T]

DefaultLogger returns the default logger.

type Buffer added in v3.3.0

type Buffer = logtemplate.Buffer

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

type CommonLogger

type CommonLogger interface {
	Logger
	FormatLogger
	WithLogger
}

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

func WithContext

func WithContext(ctx any) CommonLogger

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

type ConfigurableLogger

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
}

ConfigurableLogger provides methods to config a logger.

type ContextConfig added in v3.3.0

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
}

ContextConfig defines how WithContext enriches logs emitted by Fiber's default logger.

type ContextData added in v3.3.0

type ContextData struct{}

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 ContextTagFunc added in v3.3.0

type ContextTagFunc = logtemplate.Func[any, ContextData]

ContextTagFunc renders one contextual log tag.

type FormatLogger

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

FormatLogger is a logger interface that output logs with a format.

type Level

type Level int

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.

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

The levels of logs.

type Logger

type Logger interface {
	Trace(v ...any)
	Debug(v ...any)
	Info(v ...any)
	Warn(v ...any)
	Error(v ...any)
	Fatal(v ...any)
	Panic(v ...any)
}

Logger is a logger interface that provides logging function with levels.

type WithLogger

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

WithLogger is a logger interface that output logs with a message and key-value pairs.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL