Documentation ¶
Overview ¶
Package handlers provides a collection of logging http.Handlers for use by HTTP services that take in configuration from environment variables
Combining Handlers ¶
We can combine the following handlers automatically or manually ¶
Usage:
r := mux.NewRouter() r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("This is a catch-all route")) }) loggedRouter := handlers.AllHandlers(r) http.ListenAndServe(":1123", loggedRouter)
They can also be manually chained together
loggedRouter := handlers.StatsdHandler(handlers.HealthdHandler(r))
Logging Context ¶
This creates a logging context to be passed into the handling function with information about the request ¶
Usage:
r := mux.NewRouter() r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { log.Ctx(r.Context()).Info("log a message with the context") w.Write([]byte("This is a catch-all route")) }) loggedRouter := handlers.LogContextHandler(r) http.ListenAndServe(":1123", loggedRouter)
Healthd ¶
This provides healthd logging (http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/health-enhanced-serverlogs.html) for health monitoring when using Elastic Beanstalk.
Usage:
r := mux.NewRouter() r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("This is a catch-all route")) }) loggedRouter := handlers.HealthdHandler(r) http.ListenAndServe(":1123", loggedRouter)
Statsd ¶
Log request duration to a statsd host ¶
Environment Variables:
STATSD_HOST: The host of the statsd server STATSD_PORT: The port of the statsd server STATSD_NAMESPACE: The namespace to prefix to every metric name STATSD_TAGS: A comma separared list of tags to apply to every metric reported
Example:
STATSD_HOST: localhost STATSD_PORT: 8125 STATSD_NAMESPACE: app.live. STATSD_TAGS: env:live,version:1.0.2
Usage:
r := mux.NewRouter() r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("This is a catch-all route")) }) loggedRouter := handlers.StatsdHandler(r) http.ListenAndServe(":1123", loggedRouter)
Structured ¶
Log requests using a structured format for handling with json/logfmt
Usage:
r := mux.NewRouter() r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("This is a catch-all route")) }) loggedRouter := handlers.StructuredLogHandler( log.With(log.KV{"module":"request.handler"}), r) http.ListenAndServe(":1123", loggedRouter)
Default Output:
time="2016-10-28T10:51:32Z" level=info msg="GET / HTTP/1.1" dur=0.003200881 http.bytes=80 http.host="localhost:1123" http.method=GET http.path="/" http.protocol="HTTP/1.1" http.ref= http.status=200 http.uri="/" http.user= module=request.handler tag="request_handled" ts="2016-10-28T10:51:31.542424381Z"
Index ¶
- func AllHandlers(h http.Handler) http.Handler
- func HealthdHandler(h http.Handler) http.Handler
- func HealthdIoHandler(out io.Writer, h http.Handler) http.Handler
- func LogContextHandler(h http.Handler) http.Handler
- func LogServeHTTP(w http.ResponseWriter, req *http.Request, handler http.Handler, ...)
- func LoggingContextHandler(logger log.FieldLogger, h http.Handler) http.Handler
- func StatsdHandler(h http.Handler) http.Handler
- func StatsdIoHandler(out *statsd.Client, h http.Handler) http.Handler
- func StructuredHandler(h http.Handler) http.Handler
- func StructuredLogHandler(logger log.FieldLogger, h http.Handler) http.Handler
- type LoggingResponseWriter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AllHandlers ¶
AllHandlers returns log context, structured, statsd and healthd chained handlers for use with AWS and Graze services
func HealthdHandler ¶
HealthdHandler return a http.Handler that wraps h and logs request to out in nginx Healthd format to /var/log/nginx/healthd/application.log.<year>-<month>-<day>-<hour>
see http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/health-enhanced-serverlogs.html
Example:
r := mux.NewRouter() r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("This is a catch-all route")) }) loggedRouter := handlers.HealthdHandler(r) http.ListenAndServe(":1123", loggedRouter)
func HealthdIoHandler ¶ added in v0.2.0
HealthdIoHandler return a http.Handler that wraps h and logs request to out in nginx Healthd format
see http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/health-enhanced-serverlogs.html
Example:
r := mux.NewRouter() r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("This is a catch-all route")) }) loggedRouter := handlers.HealthdIoHandler(os.Stdout, r) http.ListenAndServe(":1123", loggedRouter)
func LogContextHandler ¶ added in v0.2.0
LogContextHandler returns a handler that adds `http` and `transaction` items into a common logging context
It adds the following fields to the `LoggingResponseWriter` log context:
http.method - GET/POST/... http.protocol - HTTP/1.1 http.uri - /path?with=query http.path - /path http.host - localhost:80 http.user - 192.168.0.1 - ip address of the user http.ref - http://google.com - referrer http.user-agent - The user agent of the user transaction - unique uuid4 for this request
func LogServeHTTP ¶ added in v0.2.0
func LogServeHTTP(w http.ResponseWriter, req *http.Request, handler http.Handler, caller func(w LoggingResponseWriter, req *http.Request, url url.URL, ts time.Time, dur time.Duration, status, size int))
LogServeHTTP creates a LoggingResponseWriter from `w` if applicable and calls `caller` with the request status, size, time and duration
func LoggingContextHandler ¶ added in v0.2.0
LoggingContextHandler returns a handler that adds `http` and `transaction` items into the provided logging context
func StatsdHandler ¶
StatsdHandler returns a handlers.StatsdHandler to write request and response informtion to statsd
Usage:
r := mux.NewRouter() r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("This is a catch-all route")) }) loggedRouter := handlers.StatsdHandler(r) http.ListenAndServe(":1123", loggedRouter)
func StatsdIoHandler ¶ added in v0.2.0
StatsdIoHandler returns a http.Handler that wraps h and logs request to statsd
Example:
r := mux.NewRouter() r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("This is a catch-all route")) }) c, err := statsd.New("127.0.0.1:8125") loggedRouter := handlers.StatsdHandler(c, r) http.ListenAndServe(":1123", loggedRouter)
func StructuredHandler ¶ added in v0.2.0
StructuredHandler returns an opinionated structuredHandler using the standard logger and setting a context with the fields:
component = request.handler
Usage:
r := mux.NewRouter() r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("This is a catch-all route")) }) loggedRouter := handlers.StructuredHandler(r) http.ListenAndServe(":1123", loggedRouter)
func StructuredLogHandler ¶ added in v0.2.0
StructuredLogHandler returns a http.Handler that wraps h and logs request to out in a structured format that can be outputted in json or logfmt
Example:
r := mux.NewRouter() r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("This is a catch-all route")) }) logger := log.New() logger.SetFormatter(&logrus.JSONFormatter{}) loggedRouter := handlers.StructuredLogHandler( logger.With(log.KV{"module":"request.handler"}) , r) http.ListenAndServe(":1123", loggedRouter)
Types ¶
type LoggingResponseWriter ¶ added in v0.2.0
LoggingResponseWriter wraps a `http.ResponseWriter` `http.Flusher` and stores a logging context and status/size info
func MakeLogger ¶ added in v0.2.0
func MakeLogger(w http.ResponseWriter) LoggingResponseWriter
MakeLogger creates a LoggingResponseWriter from a http.ResponseWriter
The loggingResponsWriter adds status field and the size of the response to the LoggingResponseWriter