Documentation ¶
Overview ¶
Package httplog provides logging for http requests.
Apart from a ready to use logger that can be used freely, the package also provides a logging middleware (or wrapper) over http Handlers.
The logger outputs a small set of default parameters and provides an extensible way to log extra parameters if needed. The log format is a nice balance between human and machine readability.
The log output is one line per request. The parameters are separated with a blank space while the parameter key and its value are separated by the "=" character. Here's an example log output for a single request.
level=I time=2017-07-08T17:08:12UTC ip=193.92.20.19 method=GET path=/logs ua=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36 status=200 params={}
Standalone usage ¶
The logger needs a stream that implements the io.Writer interface. This is where all logging output will go.
type stream struct {} func (s *stream) Write(p []byte) (n int, err error) { // write somewhere } l := httplog.New(&stream{}) l.Log()
Middleware usage ¶
You just need to provide your handler and a logger as arguments to the httplog.WithLogging function.
type customHandler {} func (h *customHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.WriteHeader(200) // ...your handler logic goes here... } func main() { // Configure a logger l := httplog.New(os.Stdout) // And use the middleware http.Handle("/logs", httplog.WithLogging(&customHandler{}, l)) http.ListenAndServe(":8080", nil) }
Adding extra log parameters
type User struct { ID int } user := &User{ID: 1234} l := httplog.New(os.Stdout) l.Add("uid", user.ID) l.Add("meta", "new-request") l.Log() // => level=I [...] uid=1234 meta=new-request
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WithLogging ¶
WithLogging provides HTTP logging capabilities to an http.Handler. It is used as http.Handler middleware. Requires the http.Handler and a pre-configured Logger. Returns a new handler that wraps the supplied handler and provides logging functionality. Example:
type customHandler {} func (h *customHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.WriteHeader(200) // ...your handler logic goes here... } func main() { // Configure a logger l := httplog.New(os.Stdout) // And use the middleware http.Handle("/logs", httplog.WithLogging(&customHandler{}, l)) http.ListenAndServe(":8080", nil) }
Types ¶
type Logger ¶
type Logger interface { Log() SetStatus(int) SetRequestInfo(*http.Request) Add(string, interface{}) }
Logger is the interface that wraps the basic Log method. Log builds a log entry with all the parameters the Logger has been configured with and writes the log entry to the underlying io.Writer's stream.
The Logger can be configured with information from the request, using the SetRequestInfo method. After the request is made, the SetStatus method will set the response status.
Logger also provides an Add method, to add extra parameters for logging.