通过middleware机制写日志

5年以前  |  阅读数:502 次  |  编程语言:Golang 
package main

import "net/http"
import "time"
import "fmt"

func runLogging(logs chan string) {
    for log := range logs {
        fmt.Println(log)
    }
}

func wrapLogging(f http.HandlerFunc) http.HandlerFunc {
    logs := make(chan string, 10000)
    go runLogging(logs)
    return func(w http.ResponseWriter, r *http.Request) {
        start := time.Now()
        f(w, r)
        method := r.Method
        path := r.URL.Path
        elapsed := float64(time.Since(start)) / 1000000.0
        logs <- fmt.Sprintf(
            "method=%s path=%s elapsed=%f",
            method, path, elapsed)
    }
}

func hello(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "text/plain")
    time.Sleep(time.Millisecond * 50)
    fmt.Fprintln(w, "Hello logged world")
}

func main() {
    handler := wrapLogging(hello)
    http.HandleFunc("/", handler)
    http.ListenAndServe(":5000", nil)
}

Copyright© 2013-2020

All Rights Reserved 京ICP备2023019179号-8