go-loghttp: github.com/motemen/go-loghttp Index | Files | Directories

package loghttp

import "github.com/motemen/go-loghttp"

Package loghttp provides automatic logging functionalities to http.Client.

Index

Package Files

loghttp.go

Variables

var ContextKeyRequestStart = &contextKey{"RequestStart"}
var DefaultLogRequest = func(req *http.Request) {
    log.Printf("--> %s %s", req.Method, req.URL)
}

Used if transport.LogRequest is not set.

var DefaultLogResponse = func(resp *http.Response) {
    ctx := resp.Request.Context()
    if start, ok := ctx.Value(ContextKeyRequestStart).(time.Time); ok {
        log.Printf("<-- %d %s (%s)", resp.StatusCode, resp.Request.URL, roundtime.Duration(time.Now().Sub(start), 2))
    } else {
        log.Printf("<-- %d %s", resp.StatusCode, resp.Request.URL)
    }
}

Used if transport.LogResponse is not set.

var DefaultTransport = &Transport{
    Transport: http.DefaultTransport,
}

THe default logging transport that wraps http.DefaultTransport.

type Transport Uses

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 Uses

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.

Directories

PathSynopsis
globalPackage global automatically sets http.DefaultTransport to loghttp.DefaultTransport when loaded.

Package loghttp imports 5 packages (graph) and is imported by 3 packages. Updated 2017-08-09. Refresh now. Tools for package owners.