train: github.com/f2prateek/train/log Index | Examples | Files

package log

import "github.com/f2prateek/train/log"

Index

Examples

Package Files

log.go

func New Uses

func New(out io.Writer, level Level) train.Interceptor

New returns a logging interceptor with the given level that writes to the given writer.

Code:

var buf bytes.Buffer
client := &http.Client{
    Transport: train.Transport(log.New(&buf, log.Body)),
}

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
    w.Header().Set("Date", time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Format(time.RFC1123))
    response.OK(w, "Hello World!")
}))
defer ts.Close()

req, err := http.NewRequest("GET", ts.URL, nil)
if err != nil {
    panic(err)
}
req.Host = "127.0.0.1:54709"
client.Do(req)

fmt.Println(strings.Replace(buf.String(), "\r", "", -1))

Output:

GET / HTTP/1.1
Host: 127.0.0.1:54709
User-Agent: Go-http-client/1.1
Accept-Encoding: gzip

HTTP/1.1 200 OK
Content-Length: 13
Content-Type: text/plain; charset=utf-8
Date: Tue, 10 Nov 2009 23:00:00 UTC

Hello World!

type Level Uses

type Level uint8
const (
    // Logs nothing.
    None Level = iota
    // Logs request and response lines and their respective headers.
    Basic
    // Logs request and response lines and their respective headers and bodies (if present).
    Body
)

Package log imports 7 packages (graph). Updated 2016-08-01. Refresh now. Tools for package owners.