-
-
Save andrelsf/e12ca0e8fc4cc4e9ee74863091cf1848 to your computer and use it in GitHub Desktop.
Golang: Log HTTP Requests in Go
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
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) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment