loghttp

package
v0.0.0-...-41edef4 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2021 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ContextKeyRequestStart = &contextKey{"RequestStart"}
View Source
var DefaultLogRequest = func(req *http.Request) string {
	header, _ := json.Marshal(req.Header)

	body := []byte("")
	if req.Body != nil {
		body, _ = ioutil.ReadAll(req.Body)
		req.Body = ioutil.NopCloser(bytes.NewBuffer(body))
	}
	return fmt.Sprintf("\n--> \n%s %s\n%s\n%s", req.Method, req.URL, string(header), string(body))
}

Used if transport.LogRequest is not set.

View Source
var DefaultLogResponse = func(resp *http.Response) string {
	ctx := resp.Request.Context()

	header, _ := json.Marshal(resp.Header)

	body, _ := ioutil.ReadAll(resp.Body)
	resp.Body = ioutil.NopCloser(bytes.NewBuffer(body))
	if start, ok := ctx.Value(ContextKeyRequestStart).(time.Time); ok {
		return fmt.Sprintf("\n<--\n %d (%s)\n%s\n%s", resp.StatusCode, time.Since(start), string(header), string(body))
	} else {
		return fmt.Sprintf("\n<-- \n%d\n%s\n%s", resp.StatusCode, string(header), string(body))
	}
}

Used if transport.LogResponse is not set.

View Source
var DefaultTransport = &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) string
	LogResponse func(resp *http.Response) string
	Logger      *logrus.Logger
}

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