Documentation
¶
Overview ¶
Package httplog provides a standard http.RoundTripper transport that can be used with standard HTTP clients to log the raw (outgoing) HTTP request and response.
Example:
// _example/example.go
package main
import (
"log"
"net/http"
"os"
"github.com/kenshaw/httplog"
)
func main() {
cl := &http.Client{
Transport: httplog.NewPrefixedRoundTripLogger(nil, os.Stdout),
// without request or response body
// Transport: httplog.NewPrefixedRoundTripLogger(nil, os.Stdout, httplog.WithReqResBody(false, false)),
}
req, err := http.NewRequest("GET", "https://google.com", nil)
if err != nil {
log.Fatal(err)
}
res, err := cl.Do(req)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
}
NewPrefixedTripLogger provides a convenient wrapper around using the standard library's io.Writer, and standard output func signatures for fmt.Printf and log.Printf.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultTransport = http.DefaultTransport
DefaultTransport is the default transport used by the HTTP logger.
Functions ¶
This section is empty.
Types ¶
type Option ¶ added in v0.2.0
type Option func(*RoundTripLogger)
Option is a roundtrip logger option.
func WithReqResBody ¶ added in v0.3.0
WithReqResBody is a roundtrip logger option to set whether or not to log the request and response body. Useful when body content is binary.
type RoundTripLogger ¶
type RoundTripLogger struct {
// contains filtered or unexported fields
}
RoundTripLogger provides a standard http.RoundTripper transport that can be used with standard HTTP clients to log the raw (outgoing) HTTP request and response.
func NewPrefixedRoundTripLogger ¶
func NewPrefixedRoundTripLogger(transport http.RoundTripper, logger interface{}, opts ...Option) *RoundTripLogger
NewPrefixedRoundTripLogger creates a new HTTP transport that logs the raw (outgoing) HTTP request and response to the provided logger.
Prefixes requests and responses with "-> " and "<-", respectively. Adds an additional blank line ("\n\n") to the output of requests and responses.
Valid types for logger:
io.Writer
func(string, ...interface{}) (int, error) // fmt.Printf
func(string, ...interface{}) // log.Printf
Note: will panic() when an unknown logger type is passed.
func NewRoundTripLogger ¶
func NewRoundTripLogger(transport http.RoundTripper, reqf, resf func([]byte), opts ...Option) *RoundTripLogger
NewRoundTripLogger creates a new HTTP transport that logs the raw (outgoing) HTTP request and response.