Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Golang: Log HTTP Requests in Go
package main
import (
"fmt"
"log"
"net/http"
"os"
)
func main() {
logPath := "development.log"
httpPort := 4000
openLogFile(logPath)
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
http.HandleFunc("/", rootHandler)
fmt.Printf("listening on %v\n", httpPort)
fmt.Printf("Logging to %v\n", logPath)
err := http.ListenAndServe(fmt.Sprintf(":%d", httpPort), logRequest(http.DefaultServeMux))
if err != nil {
log.Fatal(err)
}
}
func rootHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "<h1>Hello World</h1><div>Welcome to whereever you are</div>")
}
func logRequest(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("%s %s %s\n", r.RemoteAddr, r.Method, r.URL)
handler.ServeHTTP(w, r)
})
}
func openLogFile(logfile string) {
if logfile != "" {
lf, err := os.OpenFile(logfile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0640)
if err != nil {
log.Fatal("OpenLogfile: os.OpenFile:", err)
}
log.SetOutput(lf)
}
}
@DomHudson

This comment has been minimized.

Copy link

DomHudson commented Aug 2, 2018

Thanks!

@LeonhardPrintz

This comment has been minimized.

Copy link

LeonhardPrintz commented Oct 11, 2018

This is still insufficient, I'd also want to know when the response status and the time of the method request..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.