func WithHide(ctx context.Context) context.Context
WithHide can be used to protect a request from being exposed.
BodyFilter allows you to skip printing a HTTP body based on its associated Header.
It can be used for omitting HTTP Request and Response bodies. You can filter by checking properties such as Content-Type or Content-Length.
On a HTTP server, this function is called even when no body is present due to http.Request always carrying a non-nil value.
type BodyFilter func(h http.Header) (skip bool, err error)
Filter allows you to skip requests.
If an error happens and you want to log it, you can pass a not-null error value.
type Filter func(req *http.Request) (skip bool, err error)
Flusher defines how logger prints requests.
type Flusher int
Logger can print without flushing, when they are available, or when the request is done.
const ( // NoBuffer strategy prints anything immediately, without buffering. // It has the issue of mingling concurrent requests in unpredictable ways. NoBuffer Flusher = iota // OnReady buffers and prints each step of the request or response (header, body) whenever they are ready. // It reduces mingling caused by mingling but does not give any ordering guarantee, so responses can still be out of order. OnReady // OnEnd buffers the whole request and flushes it once, in the end. OnEnd )
Formatter can be used to format body.
If the Format function returns an error, the content is printed in verbatim after a warning. Match receives a media type from the Content-Type field. The body is formatted if it returns true.
type Formatter interface { Match(mediatype string) bool Format(w io.Writer, src []byte) error }
JSONFormatter helps you read unreadable JSON documents.
github.com/tidwall/pretty could be used to add colors to it. However, it would add an external dependency. If you want, you can define your own formatter using it or anything else. See Formatter.
type JSONFormatter struct{}
func (j *JSONFormatter) Format(w io.Writer, src []byte) error
Format JSON content.
func (j *JSONFormatter) Match(mediatype string) bool
Match JSON media type.
Logger provides a way for you to print client and server-side information about your HTTP traffic.
type Logger struct { // SkipRequestInfo avoids printing a line showing the request URI on all requests plus a line // containing the remote address on server-side requests. SkipRequestInfo bool // Time the request began and its duration. Time bool // TLS information, such as certificates and ciphers. // BUG(henvic): Currently, the TLS information prints after the response header, although it // should be printed before the request header. TLS bool // RequestHeader set by the client or received from the server. RequestHeader bool // RequestBody sent by the client or received by the server. RequestBody bool // ResponseHeader received by the client or set by the HTTP handlers. ResponseHeader bool // ResponseBody received by the client or set by the server. ResponseBody bool // SkipSanitize bypasses sanitizing headers containing credentials (such as Authorization). SkipSanitize bool // Colors set ANSI escape codes that terminals use to print text in different colors. Colors bool // Formatters for the request and response bodies. // No standard formatters are used. You need to add what you want to use explicitly. // We provide a JSONFormatter for convenience (add it manually). Formatters []Formatter // MaxRequestBody the logger can print. // If value is not set and Content-Length is not sent, 4096 bytes is considered. MaxRequestBody int64 // MaxResponseBody the logger can print. // If value is not set and Content-Length is not sent, 4096 bytes is considered. MaxResponseBody int64 // contains filtered or unexported fields }
func (l *Logger) Middleware(next http.Handler) http.Handler
Middleware for logging incoming requests to a HTTP server.
func (l *Logger) PrintRequest(req *http.Request)
PrintRequest prints a request, even when WithHide is used to hide it.
It doesn't log TLS connection details or request duration.
func (l *Logger) PrintResponse(resp *http.Response)
PrintResponse prints a response.
func (l *Logger) RoundTripper(rt http.RoundTripper) http.RoundTripper
RoundTripper returns a RoundTripper that uses the logger.
func (l *Logger) SetBodyFilter(f BodyFilter)
SetBodyFilter allows you to set a function to skip printing a body. Pass nil to remove the body filter. This method is concurrency safe.
func (l *Logger) SetFilter(f Filter)
SetFilter allows you to set a function to skip requests. Pass nil to remove the filter. This method is concurrency safe.
func (l *Logger) SetFlusher(f Flusher)
SetFlusher sets the flush strategy for the logger.
func (l *Logger) SetOutput(w io.Writer)
SetOutput sets the output destination for the logger.
func (l *Logger) SkipHeader(headers []string)
SkipHeader allows you to skip printing specific headers. This method is concurrency safe.