http

package
v1.8.25 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2023 License: GPL-3.0 Imports: 4 Imported by: 8

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultLogRequest = func(req *http.Request) {
	ctx := req.Context()
	fields := []zap.Field{
		zap.String("host", req.Host),
		zap.String("method", req.Method),
		zap.String("url", req.URL.String()),
	}

	if reqID := log.GetReqID(ctx); len(reqID) > 0 {
		fields = append(fields, zap.String("req_id", reqID))
	}

	l := log.Logger
	if l.Core().Enabled(zap.DebugLevel) {
		fields = append(fields, log.DecodeBodyFromRequest(req))
		fields = append(fields, log.DecodeHeaderFromRequest(req))
	}

	l.Info("Outgoing HTTP Request", fields...)
}

Used if transport.LogRequest is not set.

View Source
var DefaultLogResponse = func(resp *http.Response) {
	ctx := resp.Request.Context()
	l := log.Logger
	fields := []zap.Field{
		zap.Int("status", resp.StatusCode),
		zap.String("uri", resp.Request.URL.String()),
		zap.String("host", resp.Request.Host),
	}

	if reqID := log.GetReqID(ctx); len(reqID) > 0 {
		fields = append(fields, zap.String("req_id", reqID))
	}

	if l.Core().Enabled(zap.DebugLevel) {
		fields = append(fields, log.DecodeBodyFromResponse(resp))
		fields = append(fields, log.DecodeHeaderFromResponse(resp))
	}

	l.Info("Incoming HTTP Response", fields...)
}

Used if transport.LogResponse is not set.

View Source
var LoggingTransport = &Transport{
	Transport: http.DefaultTransport,
}

THe default logging transport that wraps http.DefaultTransport.

Functions

This section is empty.

Types

type Transport

type Transport struct {
	Transport   http.RoundTripper
	LogRequest  func(req *http.Request)
	LogResponse func(resp *http.Response)
}

Transport implements http.RoundTripper. When set as Transport of http.Client, it executes HTTP requests with logging. No field is mandatory.

func (*Transport) RoundTrip

func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip is the core part of this module and implements http.RoundTripper. Executes HTTP request with request/response logging.

Jump to

Keyboard shortcuts

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