ginlog

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	RequestIDKey     = logging.RequestIDKey
	RequestIDHeader  = logging.RequestIDHeader
	RequestLoggerKey = logging.RequestLoggerKey
)

Variables

This section is empty.

Functions

func GetOrCreateRequestID

func GetOrCreateRequestID(ctx *gin.Context) string

GetOrCreateRequestID returns the request ID of the supplied Gin context. If a request ID doesn't exist, then it creates and sets one.

func GetRequestLogger

func GetRequestLogger(ctx *gin.Context) *zap.Logger

GetRequestLogger returns a logger for the current request context. By default, this only includes the request ID.

func GetSugaredRequestLogger

func GetSugaredRequestLogger(ctx *gin.Context) *zap.SugaredLogger

GetSugaredRequestLogger is a shortcut for `GetRequestLogger(c).Sugar()`.

func RequestLogger

func RequestLogger(logger *zap.Logger, opts ...RequestLoggerOption) gin.HandlerFunc

RequestLogger returns a Gin middleware for logging using Zap.

Types

type RequestLoggerConfig

type RequestLoggerConfig struct {
	// ExcludeQueryParameters controls whether query params are stripped off when logging.
	ExcludeQueryParameters bool `mapstructure:"exclude_query_parameters"`

	// LevelByPath sets a custom logging level by path.
	// e.g. "/api/v1/user/keys.pub" -> "debug".
	// Any other paths will be using default Info logging level
	LevelByPath map[string]string `mapstructure:"level_by_path"`

	// LevelByRegexPath sets a custom logging level by regex path.
	// e.g. "\\/api\\/v1\\/user/\\[a-zA-Z0-9_-]+\\/info" -> "debug".
	// Any other paths will be using default Info logging level
	LevelByRegexPath map[string]string `mapstructure:"level_by_regex_path"`
}

RequestLoggerConfig is a config for RequestLogger.

func (RequestLoggerConfig) Opts

Opts returns a set of opts to apply to RequestLogger constructor using this config.

func (RequestLoggerConfig) Validate

func (rec RequestLoggerConfig) Validate() error

Validate RequestLoggerConfig.

type RequestLoggerOption

type RequestLoggerOption func(*requestLogger)

RequestLoggerOption is a functional option pattern applied to configuring RequestLogger.

func WithRequestLoggerExcludeQueryParameters

func WithRequestLoggerExcludeQueryParameters(value bool) RequestLoggerOption

WithRequestLoggerExcludeQueryParameters controls whether to exclude query parameters from logging.

By default, the query parameters are always logged.

func WithRequestLoggerLevelByPath

func WithRequestLoggerLevelByPath(levelByPath map[string]zapcore.Level) RequestLoggerOption

WithRequestLoggerLevelByPath sets a custom logging level depending on path.

func WithRequestLoggerLevelByRegexPath

func WithRequestLoggerLevelByRegexPath(levelByRegexPath map[*regexp.Regexp]zapcore.Level) RequestLoggerOption

WithRequestLoggerLevelByRegexPath sets a custom logging level depending on regex path.

Jump to

Keyboard shortcuts

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