package log
import "github.com/google/martian/log"
Package log 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.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
// --------------------------------------------------------------------------------
}
--------------------------------------------------------------------------------
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 ¶
IncludeBody sets whether to include the request/response body in the log.
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) SetLogFunc ¶
SetLogFunc sets the logging function for the logger.
Source Files ¶
- Version
- v2.0.0-beta+incompatible
- Published
- Aug 14, 2015
- Platform
- linux/amd64
- Imports
- 8 packages
- Last checked
- 4 hours ago –
Tools for package owners.