logger

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2024 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package logger contains a service that provides methods to log HTTP requests for both server and client sides.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotHijacker = errors.New("ResponseWriter is not a Hijacker")

ErrNotHijacker is returned when the underlying ResponseWriter does not implement http.Hijacker interface.

Functions

func Log2Slog

func Log2Slog(ctx context.Context, parts *LogParts, logger *slog.Logger)

Log2Slog is the default log function that logs to slog.

func WithBody

func WithBody(maxBodySize int) func(l *Logger)

WithBody sets the maximum request & response body length to be logged. Zero and negative values mean to not log the body at all.

Types

type LogParts

type LogParts struct {
	// Client is true if the logger is used as round tripper.
	Client bool `json:"-"`

	Duration time.Duration `json:"duration"`
	StartAt  time.Time     `json:"start_at"`
	Request  *RequestInfo  `json:"request"`
	Response *ResponseInfo `json:"response"`
}

LogParts contains the information to be logged.

type Logger

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

Logger provides methods to log HTTP requests for both server and client sides.

func New

func New(opts ...Option) *Logger

New returns a new Logger.

func (*Logger) HTTPClientRoundTripper

func (l *Logger) HTTPClientRoundTripper(next http.RoundTripper) http.RoundTripper

HTTPClientRoundTripper returns a RoundTripper that logs HTTP requests.

func (*Logger) HTTPServerMiddleware

func (l *Logger) HTTPServerMiddleware(next http.Handler) http.Handler

HTTPServerMiddleware returns a middleware that logs HTTP requests.

type Option

type Option func(*Logger)

Option is a function that configures a Logger.

func WithLogFn

func WithLogFn(fn func(context.Context, *LogParts)) Option

WithLogFn sets a custom log function.

func WithLogger

func WithLogger(logger *slog.Logger) Option

WithLogger is a shortcut that sets Log2Slog as the log function to log to slog.

func WithMaskIP added in v1.4.0

func WithMaskIP(fn func(string) string) Option

WithMaskIP sets a custom function to mask IP addresses.

func WithSanitizeHeaders added in v1.4.0

func WithSanitizeHeaders(fn func(http.Header) map[string]string) Option

WithSanitizeHeaders sets a custom function to sanitize headers.

func WithSanitizeQuery added in v1.4.0

func WithSanitizeQuery(fn func(string) string) Option

WithSanitizeQuery sets a custom function to sanitize query parameters.

func WithUser

func WithUser(fn func(*http.Request) (string, error)) Option

WithUser sets a custom user function.

type RequestInfo

type RequestInfo struct {
	Method   string `json:"method"`
	URL      string `json:"url"`
	RemoteIP string `json:"remote_ip"`
	Host     string `json:"host"`
	User     string `json:"user"`

	Headers map[string]string `json:"headers"`
	Body    string            `json:"body"`
}

RequestInfo contains the request information to be logged.

type ResponseInfo

type ResponseInfo struct {
	Status int   `json:"status"`
	Size   int64 `json:"size"`
	Error  error `json:"error"`

	Headers map[string]string `json:"headers"`
	Body    string            `json:"body"`
}

ResponseInfo contains the response information to be logged.

Jump to

Keyboard shortcuts

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