httplog

package
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2024 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultMaxBodySize = 1024

DefaultMaxBodySize holds the maximum body size to include in logged requests when [TransportConfig.MaxBodySize] is <=0.

Variables

This section is empty.

Functions

func ContextWithAllowedURLQueryParams

func ContextWithAllowedURLQueryParams(ctx context.Context, allow func(key string) bool) context.Context

ContextWithAllowedURLQueryParams returns a context that will allow only the URL query parameters for which the given allow function returns true. All others will be redacted from the logs.

func RedactRequestBody

func RedactRequestBody(ctx context.Context, reason string) context.Context

RedactRequestBody returns a context that will cause the logger to redact the request body when logging HTTP requests.

func RedactResponseBody

func RedactResponseBody(ctx context.Context, reason string) context.Context

RedactResponseBody returns a context that will cause Transport to redact the response body when logging HTTP responses. If reason is empty, the body will not be redacted.

func RedactedURL

func RedactedURL(ctx context.Context, u *url.URL) *url.URL

RedactedURL returns u with query parameters redacted according to ContextWithAllowedURLQueryParams. If there is no allow function associated with the context, all query parameters will be redacted.

func Transport

func Transport(cfg0 *TransportConfig) http.RoundTripper

Transport returns an http.RoundTripper implementation that logs HTTP requests. If cfg0 is nil, it's equivalent to a pointer to a zero-valued TransportConfig.

Types

type BodyData

type BodyData struct {
	Body                string `json:"body,omitempty"`
	Body64              []byte `json:"body64,omitempty"`
	BodyRedactedBecause string `json:"bodyRedactedBecause,omitempty"`
	BodyTruncated       bool   `json:"bodyTruncated,omitempty"`
}

BodyData holds information about the body of a request or response.

type EventKind

type EventKind int
const (
	NoEvent EventKind = iota
	KindClientSendRequest
	KindClientRecvResponse
)

func (EventKind) String

func (k EventKind) String() string

type Logger

type Logger interface {
	// Log logs an event of the given kind with the given request
	// or response (either *Request or *Response).
	Log(ctx context.Context, kind EventKind, r RequestOrResponse)
}

type Request

type Request struct {
	ID            int64       `json:"id"`
	Method        string      `json:"method"`
	URL           string      `json:"url"`
	ContentLength int64       `json:"contentLength"`
	Header        http.Header `json:"header"`
	BodyData
}

Request represents an HTTP request.

type RequestOrResponse

type RequestOrResponse interface {
	// contains filtered or unexported methods
}

RequestOrResponse is implemented by *Request and *Response.

type Response

type Response struct {
	ID         int64       `json:"id"`
	Method     string      `json:"method,omitempty"`
	URL        string      `json:"url,omitempty"`
	Error      string      `json:"error,omitempty"`
	StatusCode int         `json:"statusCode,omitempty"`
	Header     http.Header `json:"header,omitempty"`
	BodyData
}

Response represents an HTTP response.

type SlogLogger

type SlogLogger struct {
	Logger *slog.Logger
	Level  slog.Level
}

func (SlogLogger) Log

type TransportConfig

type TransportConfig struct {
	// Logger is used to log the requests. If it is nil,
	// the zero [SlogLogger] will be used.
	Logger Logger

	// Transport is used as the underlying transport for
	// making HTTP requests. If it is nil,
	// [http.DefaultTransport] will be used.
	Transport http.RoundTripper

	// IncludeAllQueryParams causes all URL query parameters to be included
	// rather than redacted using [RedactedURL].
	IncludeAllQueryParams bool

	// MaxBodySize holds the maximum size of body data to include
	// in the logged data. When a body is larger than this, only this
	// amount of body will be included, and the "BodyTruncated"
	// field will be set to true to indicate that this happened.
	//
	// If this is <=0, DefaultMaxBodySize will be used.
	// Use [RedactRequestBody] or [RedactResponseBody]
	// to cause body data to be omitted entirely.
	MaxBodySize int
}

TransportConfig holds configuration for Transport.

Jump to

Keyboard shortcuts

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