Created
November 30, 2015 15:48
-
-
Save jotto/ec63613c334c924842d0 to your computer and use it in GitHub Desktop.
include POST params and/or JSON params in Golang HTTP log output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// just an extension of the default Negroni logger | |
// https://github.com/codegangsta/negroni/blob/c7477ad8e330bef55bf1ebe300cf8aa67c492d1b/logger.go | |
// USAGE | |
// import ( | |
// "github.com/gorilla/mux" | |
// "github.com/codegangsta/negroni" | |
// "github.com/kr/pretty" | |
// ) | |
// router := mux.NewRouter() | |
// negroniRouter := negroni.New(NewLogger()) | |
// negroniRouter.UseHandler(router) | |
// negroniRouter.Run(fmt.Sprintf(":8080")) | |
// # OUTPUT FOR application/json | |
// [VerboseHttpLogger] Started POST, /blogs, application/json | |
// [VerboseHttpLogger] JSON: map[string]interface {}{ | |
// "myJsonKey": "myJsonValue", | |
// } | |
// [VerboseHttpLogger] Completed 201 Accepted in 4.568037ms | |
// | |
// # ---------------------------------- | |
// | |
// # OUTPUT FOR application/x-www-form-urlencoded | |
// [VerboseHttpLogger] Started POST, /blogs, application/x-www-form-urlencoded | |
// [VerboseHttpLogger] urlencoded-Form: url.Values{ | |
// "myFieldName": {"someValue"}, | |
// } | |
// [VerboseHttpLogger] Completed 201 Accepted in 4.568037ms | |
type VerboseHttpLogger struct { | |
*log.Logger | |
} | |
func NewLogger() *VerboseHttpLogger { | |
return &VerboseHttpLogger{log.New(os.Stdout, "[VerboseHttpLogger] ", 0)} | |
} | |
func (l *VerboseHttpLogger) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) { | |
start := time.Now() | |
contentType := r.Header.Get("Content-Type") | |
l.Printf("Started %s, %s, %s", r.Method, r.URL.Path, contentType) | |
r.ParseForm() | |
if r.Body != nil { | |
bodyBytes, _ := ioutil.ReadAll(r.Body) | |
r.Body.Close() | |
r.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes)) | |
if contentType == "application/x-www-form-urlencoded" { | |
l.Printf("urlencoded-Form: %# v\n", pretty.Formatter(r.Form)) | |
} else if contentType == "application/json" { | |
var data map[string]interface{} | |
json.Unmarshal(bodyBytes, &data) | |
l.Printf("JSON: %# v\n", pretty.Formatter(data)) | |
} | |
} | |
next(rw, r) | |
res := rw.(negroni.ResponseWriter) | |
l.Printf("Completed %v %s in %v", res.Status(), http.StatusText(res.Status()), time.Since(start)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment