1// largely adapted from https://github.com/gorilla/handlers/blob/master/handlers.go
2// to add logging of request duration as last value (and drop referrer)
15// responseLogger is wrapper of http.ResponseWriter that keeps track of its HTTP status
17type responseLogger struct {
25func (l *responseLogger) Header() http.Header {
29func (l *responseLogger) ExtractGAPMetadata() {
30 upstream := l.w.Header().Get("GAP-Upstream-Address")
33 l.w.Header().Del("GAP-Upstream-Address")
35 authInfo := l.w.Header().Get("GAP-Auth")
38 l.w.Header().Del("GAP-Auth")
42func (l *responseLogger) Write(b []byte) (int, error) {
44 // The status will be StatusOK if WriteHeader has not been called yet
45 l.status = http.StatusOK
47 l.ExtractGAPMetadata()
48 size, err := l.w.Write(b)
53func (l *responseLogger) WriteHeader(s int) {
54 l.ExtractGAPMetadata()
59func (l *responseLogger) Status() int {
63func (l *responseLogger) Size() int {
67// loggingHandler is the http.Handler implementation for LoggingHandlerTo and its friends
68type loggingHandler struct {
74func LoggingHandler(out io.Writer, h http.Handler, v bool) http.Handler {
75 return loggingHandler{out, h, v}
78func (h loggingHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
81 logger := &responseLogger{w: w}
82 h.handler.ServeHTTP(logger, req)
86 logLine := buildLogLine(logger.authInfo, logger.upstream, req, url, t, logger.Status(), logger.Size())
87 h.writer.Write(logLine)
90// Log entry for req similar to Apache Common Log Format.
91// ts is the timestamp with which the entry should be logged.
92// status, size are used to provide the response HTTP status and size.
93func buildLogLine(username, upstream string, req *http.Request, url url.URL, ts time.Time, status int, size int) []byte {
100 if url.User != nil && username == "-" {
101 if name := url.User.Username(); name != "" {
106 client := req.Header.Get("X-Real-IP")
108 client = req.RemoteAddr
111 if c, _, err := net.SplitHostPort(client); err == nil {
115 duration := float64(time.Now().Sub(ts)) / float64(time.Second)
117 logLine := fmt.Sprintf("%s - %s [%s] %s %s %s %q %s %q %d %d %0.3f\n",
120 ts.Format("02/Jan/2006:15:04:05 -0700"),
131 return []byte(logLine)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list