log

package
v0.0.0-...-523ebca Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2017 License: MIT Imports: 7 Imported by: 7

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

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

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

Example
package main

import (
	"bytes"
	"fmt"
	"net/http"
	"net/http/httptest"
	"strings"
	"time"

	"github.com/f2prateek/train"
	"github.com/f2prateek/train/log"
	"github.com/gohttp/response"
)

func main() {
	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!

Types

type Level

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
)

Jump to

Keyboard shortcuts

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