log

package
v2.0.0-beta+incompatible Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2015 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package log provides a Martian modifier that logs the request and response.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

Logger is a modifier that logs requests and responses.

Example
l := NewLogger()
l.IncludeBody(true)
l.SetLogFunc(func(line string) {
	// Remove \r to make it easier to test with examples.
	fmt.Print(strings.Replace(line, "\r", "", -1))
})

req, err := http.NewRequest("GET", "http://example.com/path?querystring", strings.NewReader("request content"))
if err != nil {
	fmt.Println(err)
	return
}
req.RequestURI = req.URL.RequestURI()
req.Header.Set("Other-Header", "values")
req.Close = true

if err := l.ModifyRequest(req); err != nil {
	fmt.Println(err)
	return
}

res := proxyutil.NewResponse(200, strings.NewReader("response content"), req)
res.ContentLength = 16
res.Header.Set("Date", "Tue, 15 Nov 1994 08:12:31 GMT")
res.Header.Set("Other-Header", "values")

if err := l.ModifyResponse(res); err != nil {
	fmt.Println(err)
	return
}
Output:

--------------------------------------------------------------------------------
Request to http://example.com/path?querystring
--------------------------------------------------------------------------------
GET /path?querystring HTTP/1.1
Host: example.com
Connection: close
Other-Header: values

request content
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
Response from http://example.com/path?querystring
--------------------------------------------------------------------------------
HTTP/1.1 200 OK
Content-Length: 16
Date: Tue, 15 Nov 1994 08:12:31 GMT
Other-Header: values

response content
--------------------------------------------------------------------------------

func NewLogger

func NewLogger() *Logger

NewLogger returns a logger that logs requests and responses, optionally logging the body. Log function defaults to martian.Infof.

func (*Logger) IncludeBody

func (l *Logger) IncludeBody(includeBody bool)

IncludeBody sets whether to include the request/response body in the log.

func (*Logger) ModifyRequest

func (l *Logger) ModifyRequest(req *http.Request) error

ModifyRequest logs the request, optionally including the body.

The format logged is: -------------------------------------------------------------------------------- Request to http://www.google.com/path?querystring -------------------------------------------------------------------------------- GET /path?querystring HTTP/1.1 Host: www.google.com Connection: close Other-Header: values

request content --------------------------------------------------------------------------------

func (*Logger) ModifyResponse

func (l *Logger) ModifyResponse(res *http.Response) error

ModifyResponse logs the response, optionally including the body.

The format logged is: -------------------------------------------------------------------------------- Response from http://www.google.com/path?querystring -------------------------------------------------------------------------------- HTTP/1.1 200 OK Date: Tue, 15 Nov 1994 08:12:31 GMT Other-Header: values

response content --------------------------------------------------------------------------------

func (*Logger) SetLogFunc

func (l *Logger) SetLogFunc(logFunc func(line string))

SetLogFunc sets the logging function for the logger.

Jump to

Keyboard shortcuts

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