goji/logger provides request logging and ID generation middleware for Goji, a HTTP multiplexer written in Go.
The middleware functions include:
- Logger - a request logger that logs the method, remote IP and execution time of the request to stdout. When outputting to a terminal, the output is colorized for readability.
- RequestID generates a random base62 identifier for the request. When used
before the
Logger
middleware, the RequestID is included in the log output.
Here's what it looks like:
$ go get github.com/goji/logger
Here's an example of the logger.RequestID
and logger.Logger
middleware in
action:
package main
import (
"fmt"
"log"
"net/http"
"golang.org/x/net/context"
"github.com/goji/logger"
"goji.io"
"goji.io/pat"
)
func main() {
mux := goji.NewMux()
// Include logger.RequestID before logger.Logger, and the request ID will
// automatically be added to the log output.
mux.UseC(logger.RequestID)
mux.UseC(logger.Logger)
mux.HandleFuncC(pat.Get("/"), SomeHandler)
log.Fatal(http.ListenAndServe("localhost:8000", mux))
}
func SomeHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Goji!\n")
}
Note: Provided you implement the goji.Handler
interface in your own
application, this middleware can be used beyond just Goji.
MIT licensed. See the LICENSE file for details.