gzap

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2024 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package gzap provides log handling using zap package. Code structure based on ginrus package. see github.com/gin-contrib/zap

Index

Constants

View Source
const (
	FieldStatus = iota
	FieldMethod
	FieldPath
	FieldRoute
	FieldQuery
	FieldIP
	FieldUserAgent
	FieldLatency
	FieldRequestBody
	FieldResponseBody
)

Indices for renaming field.

Variables

This section is empty.

Functions

func Any

func Any(key string, value any) func(c *gin.Context) zap.Field

Any custom immutable any field

func Float64

func Float64(key string, value float64) func(c *gin.Context) zap.Field

Float64 custom immutable float32 field

func Int64

func Int64(key string, value int64) func(c *gin.Context) zap.Field

Int64 custom immutable int64 field

func Logger

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

Logger returns a gin.HandlerFunc (middleware) that logs requests using uber-go/zap.

Requests with errors are logged using zap.Error(). Requests without errors are logged using zap.Info().

func Recovery

func Recovery(logger *zap.Logger, stack bool, opts ...Option) gin.HandlerFunc

Recovery returns a gin.HandlerFunc (middleware) that recovers from any panics and logs requests using uber-go/zap. All errors are logged using zap.Error(). stack means whether output the stack info. The stack info is easy to find where the error occurs but the stack info is too large.

func String

func String(key, value string) func(c *gin.Context) zap.Field

String custom immutable string field

func Uint64

func Uint64(key string, value uint64) func(c *gin.Context) zap.Field

Uint64 custom immutable uint64 field

Types

type Config

type Config struct {
	// contains filtered or unexported fields
}

Config logger/recover config

type Option

type Option func(c *Config)

Option logger/recover option

func WithBodyLimit

func WithBodyLimit(limit int) Option

WithBodyLimit optional custom request/response body limit. default: <=0, mean not limit

func WithCustomFields

func WithCustomFields(fields ...func(c *gin.Context) zap.Field) Option

WithCustomFields optional custom field

func WithEnableBody

func WithEnableBody(b bool) Option

WithEnableBody optional custom enable request/response body.

func WithFieldName

func WithFieldName(index int, name string) Option

WithFieldName optionally renames a log field. Example: `WithFieldName(gzap.FieldStatus, "httpStatusCode")`

func WithSkipLogging

func WithSkipLogging(f func(c *gin.Context) bool) Option

WithSkipLogging optional custom skip logging option.

func WithSkipRequestBody

func WithSkipRequestBody(f func(c *gin.Context) bool) Option

WithSkipRequestBody optional custom skip request body logging option.

func WithSkipResponseBody

func WithSkipResponseBody(f func(c *gin.Context) bool) Option

WithSkipResponseBody optional custom skip response body logging option.

func WithUseLoggerLevel

func WithUseLoggerLevel(f func(c *gin.Context) zapcore.Level) Option

WithUseLoggerLevel optional use logging level.

Jump to

Keyboard shortcuts

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