martianlog

package
v2.1.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2018 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package martianlog 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.SetLogFunc(func(line string) {
	// Remove \r to make it easier to test with examples.
	fmt.Print(strings.Replace(line, "\r", "", -1))
})
l.SetDecode(true)

buf := new(bytes.Buffer)
gw := gzip.NewWriter(buf)
gw.Write([]byte("request content"))
gw.Close()

req, err := http.NewRequest("GET", "http://example.com/path?querystring", buf)
if err != nil {
	fmt.Println(err)
	return
}
req.TransferEncoding = []string{"chunked"}
req.Header.Set("Content-Encoding", "gzip")

_, remove, err := martian.TestContext(req, nil, nil)
if err != nil {
	fmt.Println(err)
	return
}
defer remove()

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 http://example.com/path?querystring HTTP/1.1
Host: example.com
Transfer-Encoding: chunked
Content-Encoding: gzip

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) 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) SetDecode

func (l *Logger) SetDecode(decode bool)

SetDecode sets whether to decode the request/response body in the log.

func (*Logger) SetHeadersOnly

func (l *Logger) SetHeadersOnly(headersOnly bool)

SetHeadersOnly sets whether to log the request/response body in the log.

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