httplog

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2018 License: MIT Imports: 6 Imported by: 0

README

httplog

A utility package that provides tracking of http response statistics with a callback for processing.

Golang Version

This package requires Go version 1.7 or higher because it uses the newly added context package.

Status

Build Status Coverage Status Go Report Card

Documentation and Examples

Full documentation and examples can be found at GoDoc

Documentation

Overview

Package httplog provides a Middleware function along with an http.ResponseWriter implementation that records response data for processing after a Request is complete.

Index

Constants

This section is empty.

Variables

View Source
var ErrResponseWriterIsNotAHijacker = errors.New("httplog: underlying ResponseWriter is not an http.Hijacker")

ErrResponseWriterIsNotAHijacker indicates a ResponseWriter's underlying ResponseWriter is not a Hijacker.

Functions

func Middleware

func Middleware(logger Logger, next http.Handler) http.Handler

Middleware creates a new http.Handler that calls next with a ResponseWriter. Once next.ServeHTTP() has returned, final statistics are collected, and logger.AfterServeHTTP() is called.

Normally in the Request, Context flow of data, only downstream http.Handlers are able to know about values set in the Context from prior middleware. If you desire to have this logger middleware be at the outermost level, then you will need to use WithValue() to set values within a ResponseWriter to store those values for processing after handling the Request.

func WithValue

func WithValue(r *http.Request, key, value interface{})

WithValue associates key and value in r's ResponseWriter that can later be retrieved with ResponseWriter.Value(key).

This allows downstream handlers from this logger middleware to set values on the ResponseWriter for processing after a Request has been served.

Types

type Logger

type Logger interface {
	//Called from Middleware() after next.ServeHTTP() has returned.
	AfterServeHTTP(*ResponseWriter)
}

Logger is the interface required to receive response information after an http.Handler has been served.

type ResponseWriter

type ResponseWriter struct {
	//ResponseWriter is the promoted http.ResponseWriter implementation.
	http.ResponseWriter

	//Request is the Request that the Handler is serving.
	Request *http.Request

	//RequestURI is set before next.ServeHTTP() in Middleware() so that the full
	//URL is recorded before downstream http.Handlers possibly modify the Request URL.
	RequestURI string

	//Start is set to time.Now() when Middleware() receives a Request.
	Start time.Time
	//Done is set to time.Now() when Middleware()'s next.ServeHTTP() returns.
	Done time.Time

	//Status is the recorded status code from http.ResponseWriter.WriteHeader().
	Status int
	//Size is the total number of bytes written to http.ResponseWriter.Write()
	//throughout the lifetime of all downstream http.Handlers.
	Size uint64
	// contains filtered or unexported fields
}

ResponseWriter is an http.ResponseWriter implementation that records status, size, start time, and completed time for the execution of an http.Request.

Instances of ResponseWriter are passed into next.ServeHTTP() from Middleware() in order to record response statistics that can be examined and processed after the Request has completed.

func (*ResponseWriter) Duration

func (r *ResponseWriter) Duration() time.Duration

Duration returns the length of time that all downstream http.Handlers took to execute.

func (*ResponseWriter) Hijack added in v1.2.0

func (r *ResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error)

Hijack is the http.Hijacker implementation. It returns r.ResponseWriter.Hijack() is r.ResponseWriter is a Hijacker. Otherwise ErrResponseWriterIsNotAHijacker is returned.

func (*ResponseWriter) Value

func (r *ResponseWriter) Value(key interface{}) interface{}

Value returns the value associated with key, for a given Request's ResponseWriter, that was previously set by WithValue().

func (*ResponseWriter) Write

func (r *ResponseWriter) Write(p []byte) (n int, err error)

Write is the http.ResponseWriter implementation of Write that records the number bytes written and passes p along to r.ResponseWriter.

func (*ResponseWriter) WriteHeader

func (r *ResponseWriter) WriteHeader(status int)

WriteHeader is the http.ResponseWriter implementation of WriteHeader that records status and passes status along to r.ResponseWriter.

Jump to

Keyboard shortcuts

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