The request logger middleware logs out information about each request. The output format is customizable through its configuration, but it comes presetup with sane defaults.
Configuration
A configuration structure can be generated and modified to alter the template the logger uses to output request information
Templates
The logger uses go templates to format the output. For more information check
out https://golang.org/pkg/text/template. In short you can specify one of
several values that are provided to the template using {{ .object }}
notation.
A few common values
Value | Description |
---|---|
.Now | The current time. To format this time use |
.Context.Request.Method | The HTTP method of the request (GET, PUT, PATCH, etc) |
.Context.Request.URL.Path | The path part of the incomming request URL |
.RequestTime | The amount of time the request took to process, in a human readable string |
.StatusCode | The numerical http status code of the request. |
.StatusText | The text version of the status code “Not Found”, “Internal Server Error”, etc |
.Response.Success | If the request was successful. |
.Response.Error | The error message, if available. |
Usage
Default configuration
import (
"github.com/5Sigma/celerity"
"github.com/5Sigma/celerity/middleware"
)
celerity.HandleCLI(func() *celerity.Server{
svr := celerity.New()
svr.Use(middleware.RequestLogger())
})
Custom configuration
import (
"github.com/5Sigma/celerity"
"github.com/5Sigma/celerity/middleware"
)
config := middleware.NewLoggerConfig()
config.ConsoleTemplate = "{{.Now.Format \"1/2/2006 15:04:05\"}} {{.URL.Path}}"
logger := middleware.RequestLoggerWithConfig(config)
celerity.HandleCLI(func() *celerity.Server{
svr := celerity.New()
svr.Use(logger)
return svr
}