httplog

package module
v0.0.0-...-84c7cc8 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2024 License: MIT Imports: 15 Imported by: 0

README

httplog

CI Status Go Report Card codecov PkgGoDev

Http middleware (inbound) and RoundTripper (outbound) using slog.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewTeeReadCloser

func NewTeeReadCloser(r io.Reader, teeBuffer *bytes.Buffer, cb TeeCallBack) io.ReadCloser

func NewTeeReadCloserPooled

func NewTeeReadCloserPooled(r io.Reader, pool *BytesBufferPool, cb TeeCallBack) io.ReadCloser

Types

type BytesBuffer

type BytesBuffer interface {
	Bytes() []byte
}

type BytesBufferPool

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

func NewBytesBufferPool

func NewBytesBufferPool(defaultCap int) *BytesBufferPool

func (*BytesBufferPool) Get

func (b *BytesBufferPool) Get() *bytes.Buffer

func (*BytesBufferPool) Put

func (b *BytesBufferPool) Put(buf *bytes.Buffer)

type HTTPLogger

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

func NewHTTPLogger

func NewHTTPLogger(ops ...HTTPLoggerOp) *HTTPLogger

func (*HTTPLogger) Handler

func (il *HTTPLogger) Handler(next http.Handler) http.Handler

func (*HTTPLogger) LoggerRoundTripper

func (il *HTTPLogger) LoggerRoundTripper(next http.RoundTripper) RoundTripperFunc

type HTTPLoggerOp

type HTTPLoggerOp func(*HTTPLogger)

func WithLogInLevel

func WithLogInLevel(lvl slog.Leveler) HTTPLoggerOp

func WithLogPolicy

func WithLogPolicy(lp LogPolicy) HTTPLoggerOp

func WithLogger

func WithLogger(logger *slog.Logger) HTTPLoggerOp

func WithMode

func WithMode(m Mode) HTTPLoggerOp

type HTTPSLogAttrsConverter

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

func (HTTPSLogAttrsConverter) AttrsHTTPRequestBodyDrain

func (a HTTPSLogAttrsConverter) AttrsHTTPRequestBodyDrain(r *http.Request) []slog.Attr

func (HTTPSLogAttrsConverter) AttrsHTTPRequestExcludeBody

func (a HTTPSLogAttrsConverter) AttrsHTTPRequestExcludeBody(r *http.Request) []slog.Attr

func (HTTPSLogAttrsConverter) AttrsHTTPResponseDrainBody

func (a HTTPSLogAttrsConverter) AttrsHTTPResponseDrainBody(r *http.Response) []slog.Attr

func (HTTPSLogAttrsConverter) AttrsHTTPResponseExcludeBody

func (a HTTPSLogAttrsConverter) AttrsHTTPResponseExcludeBody(r *http.Response) []slog.Attr

func (HTTPSLogAttrsConverter) GroupAttrsAsHTTPRequest

func (a HTTPSLogAttrsConverter) GroupAttrsAsHTTPRequest(s []slog.Attr) slog.Attr

func (HTTPSLogAttrsConverter) GroupAttrsAsHTTPResponse

func (a HTTPSLogAttrsConverter) GroupAttrsAsHTTPResponse(s []slog.Attr) slog.Attr

func (HTTPSLogAttrsConverter) HTTPRequest

func (a HTTPSLogAttrsConverter) HTTPRequest(r *http.Request) slog.Attr

func (HTTPSLogAttrsConverter) HTTPResponse

func (a HTTPSLogAttrsConverter) HTTPResponse(r *http.Response) slog.Attr

func (HTTPSLogAttrsConverter) HTTPResponseWriter

func (a HTTPSLogAttrsConverter) HTTPResponseWriter(headers http.Header, statusCode int, body []byte) slog.Attr

func (HTTPSLogAttrsConverter) Headers

func (a HTTPSLogAttrsConverter) Headers(key string, h http.Header) slog.Attr

func (HTTPSLogAttrsConverter) URL

type HeaderMatcher

type HeaderMatcher interface {
	Match(key string, values []string) bool
}

type LogPolicy

type LogPolicy struct {
	RequestBodyLogPolicy        RequestBodyLogPolicy
	ResponseBodyLogPolicy       ResponseBodyLogPolicy
	ResponseWriterBodyLogPolicy ResponseWriterBodyLogPolicy
	OmitHeaders                 HeaderMatcher
	MaskedValueHeaders          HeaderMatcher
}

func (LogPolicy) ShouldLogRequestBody

func (l LogPolicy) ShouldLogRequestBody(r *http.Request) bool

func (LogPolicy) ShouldLogResponseBody

func (l LogPolicy) ShouldLogResponseBody(r *http.Response) bool

func (LogPolicy) ShouldLogResponseWriterBody

func (l LogPolicy) ShouldLogResponseWriterBody(headers http.Header, statusCode int, body []byte) bool

func (LogPolicy) ShouldMaskHeader

func (l LogPolicy) ShouldMaskHeader(key string, values []string) bool

func (LogPolicy) ShouldOmitHeader

func (l LogPolicy) ShouldOmitHeader(key string, values []string) bool

type Mode

type Mode int
const (
	Drain Mode = iota
	Tee
)

type RequestBodyLogPolicy

type RequestBodyLogPolicy func(r *http.Request) bool
var DefaultRequestBodyLogPolicy RequestBodyLogPolicy = func(r *http.Request) bool {
	contentType := r.Header.Get("Content-Type")
	contentEncoding := r.Header.Get("Content-Encoding")
	return len(contentEncoding) == 0 && (strings.Contains(contentType, "application/json") ||
		strings.Contains(contentType, "text/html"))
}

type ResponseBodyLogPolicy

type ResponseBodyLogPolicy func(r *http.Response) bool
var DefaultResponseBodyLogPolicy ResponseBodyLogPolicy = func(r *http.Response) bool {
	h := r.Header
	contentType := h.Get("Content-Type")
	return strings.Contains(contentType, "application/json") ||
		strings.Contains(contentType, "text/html")
}

type ResponseWriterBodyLogPolicy

type ResponseWriterBodyLogPolicy func(headers http.Header, statusCode int, body []byte) bool
var DefaultResponseWriterBodyLogPolicy ResponseWriterBodyLogPolicy = func(headers http.Header, _ int, _ []byte) bool {
	contentType := headers.Get("Content-Type")
	return strings.Contains(contentType, "application/json") ||
		strings.Contains(contentType, "text/html")
}

type ResponseWriterWrapper

type ResponseWriterWrapper interface {
	http.ResponseWriter
	Header() http.Header
	Buffer() *bytes.Buffer
	Status() int
	BytesWritten() int
}

func NewResponseWriterWrapper

func NewResponseWriterWrapper(w http.ResponseWriter) ResponseWriterWrapper

type RoundTripperFunc

type RoundTripperFunc func(req *http.Request) (*http.Response, error)

RoundTripperFunc is an http.RoundTripper signature alias.

func (RoundTripperFunc) RoundTrip

func (rt RoundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error)

RoundTrip implements the RoundTripper interface.

type TeeCallBack

type TeeCallBack func(readErr, closeErr error, buf *bytes.Buffer)

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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