some request logging
This commit is contained in:
parent
749f6c3c8a
commit
8cb4f1080a
5
README
5
README
@ -40,9 +40,10 @@ gobyexample.com signups
|
|||||||
|
|
||||||
= topics
|
= topics
|
||||||
* web app
|
* web app
|
||||||
* serve static files
|
* middleware in general
|
||||||
|
* return status code
|
||||||
* log requests
|
* log requests
|
||||||
* basic auth
|
* serve static files
|
||||||
* force https
|
* force https
|
||||||
* force canonical host
|
* force canonical host
|
||||||
* not found page
|
* not found page
|
||||||
|
@ -5,6 +5,5 @@ import ("time"; "fmt")
|
|||||||
func main() {
|
func main() {
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
time.Sleep(3 * time.Second)
|
time.Sleep(3 * time.Second)
|
||||||
finish := time.Now()
|
fmt.Println(time.Since(start))
|
||||||
fmt.Println(finish.Sub(start))
|
|
||||||
}
|
}
|
||||||
|
35
src/xx-http-server-log.go
Normal file
35
src/xx-http-server-log.go
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import ("net/http"; "fmt"; "time")
|
||||||
|
|
||||||
|
func hello(res http.ResponseWriter, req *http.Request) {
|
||||||
|
res.Header().Set("Content-Type", "text/plain")
|
||||||
|
time.Sleep(time.Millisecond * 50)
|
||||||
|
fmt.Fprintln(res, "Hello logged world")
|
||||||
|
}
|
||||||
|
|
||||||
|
func wrapLogging(f http.HandlerFunc, logs chan string) http.HandlerFunc {
|
||||||
|
return func(res http.ResponseWriter, req *http.Request) {
|
||||||
|
start := time.Now()
|
||||||
|
f(res, req)
|
||||||
|
method := req.Method
|
||||||
|
path := req.URL.Path
|
||||||
|
elapsed := float64(time.Since(start)) / 1000000.0
|
||||||
|
logs <- fmt.Sprintf("method=%s path=%s elapsed=%f", method, path, elapsed)
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func runLogging(logs chan string) {
|
||||||
|
for log := range logs {
|
||||||
|
fmt.Println(log)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
logs := make(chan string, 10000)
|
||||||
|
go runLogging(logs)
|
||||||
|
handler := wrapLogging(hello, logs)
|
||||||
|
http.HandleFunc("/", handler)
|
||||||
|
http.ListenAndServe(":5000", nil)
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user