package martianlog
import "github.com/google/martian/v3/martianlog"
Package martianlog provides a Martian modifier that logs the request and response.
Index ¶
Examples ¶
Types ¶
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger is a modifier that logs requests and responses.
Code:
Output: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
// --------------------------------------------------------------------------------
}
--------------------------------------------------------------------------------
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 ¶
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 ¶
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 ¶
SetDecode sets whether to decode the request/response body in the log.
func (*Logger) SetHeadersOnly ¶
SetHeadersOnly sets whether to log the request/response body in the log.
func (*Logger) SetLogFunc ¶
SetLogFunc sets the logging function for the logger.
Source Files ¶
- Version
- v3.1.0
- Published
- Feb 24, 2020
- Platform
- windows/amd64
- Imports
- 10 packages
- Last checked
- 4 hours ago –
Tools for package owners.