(next http.Handler)
| 107 | } |
| 108 | |
| 109 | func LoggerMiddleware(next http.Handler) http.Handler { |
| 110 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 111 | metrics := httpsnoop.CaptureMetrics( |
| 112 | next, |
| 113 | w, |
| 114 | r, |
| 115 | ) |
| 116 | |
| 117 | remoteHost := ExtractIP(r) |
| 118 | |
| 119 | uri := r.RequestURI |
| 120 | |
| 121 | if r.ProtoMajor == 2 && r.Method == "CONNECT" { |
| 122 | uri = r.Host |
| 123 | } |
| 124 | if uri == "" { |
| 125 | uri = r.URL.RequestURI() |
| 126 | } |
| 127 | |
| 128 | host, port, err := net.SplitHostPort(r.Host) |
| 129 | if err != nil { |
| 130 | host = r.Host |
| 131 | if r.TLS != nil { |
| 132 | port = "443" |
| 133 | } else { |
| 134 | port = "80" |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | entry := &loggingEntry{ |
| 139 | Proto: r.Proto, |
| 140 | Method: r.Method, |
| 141 | URL: uri, |
| 142 | StatusCode: metrics.Code, |
| 143 | Size: metrics.Written, |
| 144 | Duration: metrics.Duration, |
| 145 | Referer: r.Referer(), |
| 146 | RemoteIP: remoteHost, |
| 147 | HostName: host, |
| 148 | Port: port, |
| 149 | } |
| 150 | entry.Log() |
| 151 | }) |
| 152 | } |
no test coverage detected