accesslog

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: May 23, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FilterHeaders

func FilterHeaders(h http.Header) string

FilterHeaders filters sensitive request headers and returns a JSON string.

func FilterHeadersWithAdditionalSensitiveHeaders

func FilterHeadersWithAdditionalSensitiveHeaders(h http.Header, headers ...string) string

FilterHeadersWithAdditionalSensitiveHeaders filters default sensitive headers plus custom headers.

func FilterHeadersWithSensitiveHeaders

func FilterHeadersWithSensitiveHeaders(h http.Header, headers ...string) string

FilterHeadersWithSensitiveHeaders filters the specified sensitive headers.

func GetRequestBody

func GetRequestBody(c *gin.Context) (map[string]interface{}, error)

GetRequestBody 提取请求体,按 Content-Type 解析为 map

func GetRequestHeaders

func GetRequestHeaders(c *gin.Context) map[string]interface{}

GetRequestHeaders 提取请求头为 map

func GetRequestQuery

func GetRequestQuery(c *gin.Context) map[string]interface{}

GetRequestQuery 提取 URL 查询参数为 map

func IsSkipped

func IsSkipped(c *gin.Context) bool

IsSkipped reports whether current Gin request is marked to skip access logging.

func Middleware

func Middleware(source string, opts ...MiddlewareOption) gin.HandlerFunc

Middleware records HTTP request and response metadata. The logger is optional so services can opt in with WithLogger.

func RequestInputs

func RequestInputs(c *gin.Context) (map[string]interface{}, error)

RequestInputs 分离 GET 查询参数和 POST 请求体

func Skip

func Skip(c *gin.Context)

Skip marks current Gin request as skipped by accesslog middleware.

Types

type Actor

type Actor struct {
	ID   string
	Type string
	Name string
}

type ActorResolver

type ActorResolver func(*gin.Context) Actor

type Config

type Config struct {
	QueueSize     int
	BatchSize     int
	FlushInterval time.Duration
}

type Entry

type Entry struct {
	Source     string
	TrackID    string
	TraceID    string
	RequestID  string
	UID        string
	Method     string
	Path       string
	Query      string
	IP         string
	UserAgent  string
	Headers    []byte
	ReqBody    []byte
	StatusCode int
	ErrCode    int
	ErrMsg     string
	RespBody   []byte
	Latency    int64
	CreatedAt  int64
}

Entry is a generic HTTP access log record. It intentionally has no dependency on any concrete database model.

type Logger

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

Logger asynchronously buffers access log entries and writes them in batches.

func NewLogger

func NewLogger(writer Writer, cfg Config) *Logger

func (*Logger) Push

func (l *Logger) Push(entry *Entry)

Push enqueues an entry without blocking the request path. If the queue is full, the entry is dropped.

func (*Logger) Start

func (l *Logger) Start(ctx context.Context)

Start consumes queued entries until ctx is done, then flushes remaining logs.

type MiddlewareOption

type MiddlewareOption func(*middlewareConfig)

func WithActorResolver

func WithActorResolver(resolver ActorResolver) MiddlewareOption

func WithAdditionalSensitiveFields

func WithAdditionalSensitiveFields(fields ...string) MiddlewareOption

WithAdditionalSensitiveFields appends body field keywords to the default redaction list.

func WithAdditionalSensitiveHeaders

func WithAdditionalSensitiveHeaders(headers ...string) MiddlewareOption

WithAdditionalSensitiveHeaders appends request header names to the default filter list.

func WithLogger

func WithLogger(logger *Logger) MiddlewareOption

func WithSensitiveFields

func WithSensitiveFields(fields ...string) MiddlewareOption

WithSensitiveFields replaces body field redaction keywords. Passing no fields disables body field redaction for this middleware.

func WithSensitiveHeaders

func WithSensitiveHeaders(headers ...string) MiddlewareOption

WithSensitiveHeaders replaces request header filter names. Passing no headers disables header filtering for this middleware.

func WithSkipIPs

func WithSkipIPs(values ...string) MiddlewareOption

WithSkipIPs skips access logging for matched client IPs. Values support exact IPs such as "127.0.0.1" and CIDR ranges such as "10.0.0.0/8".

func WithSkipMethods

func WithSkipMethods(methods ...string) MiddlewareOption

WithSkipMethods skips access logging for matched HTTP methods.

func WithSkipper

func WithSkipper(skipper Skipper) MiddlewareOption

type Skipper

type Skipper func(*gin.Context) bool

type Writer

type Writer interface {
	WriteBatch(ctx context.Context, entries []*Entry) error
}

Writer persists access log entries in batches.

Jump to

Keyboard shortcuts

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