Documentation ¶
Overview ¶
Package logger contains a service that provides methods to log HTTP requests for both server and client sides.
Index ¶
- Variables
- func Log2Slog(ctx context.Context, parts *LogParts, logger *slog.Logger)
- func WithBody(maxBodySize int) func(l *Logger)
- type LogParts
- type Logger
- type Option
- func WithLogFn(fn func(context.Context, *LogParts)) Option
- func WithLogger(logger *slog.Logger) Option
- func WithMaskIP(fn func(string) string) Option
- func WithSanitizeHeaders(fn func(http.Header) map[string]string) Option
- func WithSanitizeQuery(fn func(string) string) Option
- func WithUser(fn func(*http.Request) (string, error)) Option
- type RequestInfo
- type ResponseInfo
Constants ¶
This section is empty.
Variables ¶
var ErrNotHijacker = errors.New("ResponseWriter is not a Hijacker")
ErrNotHijacker is returned when the underlying ResponseWriter does not implement http.Hijacker interface.
Functions ¶
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 (*Logger) HTTPClientRoundTripper ¶
func (l *Logger) HTTPClientRoundTripper(next http.RoundTripper) http.RoundTripper
HTTPClientRoundTripper returns a RoundTripper that logs HTTP requests.
type Option ¶
type Option func(*Logger)
Option is a function that configures a Logger.
func WithLogger ¶
WithLogger is a shortcut that sets Log2Slog as the log function to log to slog.
func WithMaskIP ¶ added in v1.4.0
WithMaskIP sets a custom function to mask IP addresses.
func WithSanitizeHeaders ¶ added in v1.4.0
WithSanitizeHeaders sets a custom function to sanitize headers.
func WithSanitizeQuery ¶ added in v1.4.0
WithSanitizeQuery sets a custom function to sanitize query parameters.
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.