Documentation
¶
Overview ¶
Package logger is an HTTP middleware for Go that logs web requests to an io.Writer.
package main
import (
"log"
"net/http"
"github.com/unrolled/logger"
)
var myHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello world"))
})
func main() {
loggerMiddleware := logger.New(logger.Options{
Prefix: "MySampleWebApp",
RemoteAddressHeaders: []string{"X-Forwarded-Proto"},
OutputFlags: log.LstdFlags,
})
// loggerWithDefaults := logger.New()
app := loggerMiddleware.Handler(myHandler)
http.ListenAndServe("0.0.0.0:3000", app)
}
A simple GET request to "/info/" will output:
[MySampleWebApp] 2014/11/21 14:11:21 (12.34.56.78) "GET /info/ HTTP/1.1" 200 11 12.54µs
Here's a breakdown of what the values mean:
[SuppliedPrefix] Date Time (RemoteIP) "Method RequestURI Protocol" StatusCode Size Time
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Logger ¶
Logger is a HTTP middleware handler that logs a request. Outputted information includes status, method, URL, remote address, size, and the time it took to process the request.
type Options ¶
type Options struct {
// Prefix is the outputted keyword in front of the log message. Logger automatically wraps the prefix in square brackets (ie. [myApp] ) unless the `DisableAutoBrackets` is set to true. A blank value will not have brackets added. Default is blank (with no brackets).
Prefix string
// DisableAutoBrackets if set to true, will remove the prefix and square brackets. Default is false.
DisableAutoBrackets bool
// RemoteAddressHeaders is a list of header keys that Logger will look at to determine the proper remote address. Useful when using a proxy like Nginx: `[]string{"X-Forwarded-Proto"}`. Default is an empty slice, and thus will use `reqeust.RemoteAddr`.
RemoteAddressHeaders []string
// Out is the destination to which the logged data will be written too. Default is `os.Stdout`.
Out io.Writer
// OutputFlags defines the logging properties. See http://golang.org/pkg/log/#pkg-constants. To disable all flags, set this to `-1`. Defaults to log.LstdFlags (2009/01/23 01:23:23).
OutputFlags int
// IgnoredRequestURIs is a list of path values we do not want logged out. Exact match only!
IgnoredRequestURIs []string
}
Options is a struct for specifying configuration parameters for the Logger middleware.
Click to show internal directories.
Click to hide internal directories.