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)
17 defaultRequestLoggingFormat = "{{.Client}} - {{.Username}} [{{.Timestamp}}] {{.Host}} {{.RequestMethod}} {{.Upstream}} {{.RequestURI}} {{.Protocol}} {{.UserAgent}} {{.StatusCode}} {{.ResponseSize}} {{.RequestDuration}}"
20// responseLogger is wrapper of http.ResponseWriter that keeps track of its HTTP status
22type responseLogger struct {
30// Header returns the ResponseWriter's Header
31func (l *responseLogger) Header() http.Header {
35// ExtractGAPMetadata extracts and removes GAP headers from the ResponseWriter's
37func (l *responseLogger) ExtractGAPMetadata() {
38 upstream := l.w.Header().Get("GAP-Upstream-Address")
41 l.w.Header().Del("GAP-Upstream-Address")
43 authInfo := l.w.Header().Get("GAP-Auth")
46 l.w.Header().Del("GAP-Auth")
50// Write writes the response using the ResponseWriter
51func (l *responseLogger) Write(b []byte) (int, error) {
53 // The status will be StatusOK if WriteHeader has not been called yet
54 l.status = http.StatusOK
56 l.ExtractGAPMetadata()
57 size, err := l.w.Write(b)
62// WriteHeader writes the status code for the Response
63func (l *responseLogger) WriteHeader(s int) {
64 l.ExtractGAPMetadata()
69// Status returns the response status code
70func (l *responseLogger) Status() int {
74// Size returns teh response size
75func (l *responseLogger) Size() int {
79// logMessageData is the container for all values that are available as variables in the request logging format.
80// All values are pre-formatted strings so it is easy to use them in the format string.
81type logMessageData struct {
96// loggingHandler is the http.Handler implementation for LoggingHandlerTo and its friends
97type loggingHandler struct {
101 logTemplate *template.Template
104// LoggingHandler provides an http.Handler which logs requests to the HTTP server
105func LoggingHandler(out io.Writer, h http.Handler, v bool, requestLoggingTpl string) http.Handler {
106 return loggingHandler{
110 logTemplate: template.Must(template.New("request-log").Parse(requestLoggingTpl)),
114func (h loggingHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
117 logger := &responseLogger{w: w}
118 h.handler.ServeHTTP(logger, req)
122 h.writeLogLine(logger.authInfo, logger.upstream, req, url, t, logger.Status(), logger.Size())
125// Log entry for req similar to Apache Common Log Format.
126// ts is the timestamp with which the entry should be logged.
127// status, size are used to provide the response HTTP status and size.
128func (h loggingHandler) writeLogLine(username, upstream string, req *http.Request, url url.URL, ts time.Time, status int, size int) {
135 if url.User != nil && username == "-" {
136 if name := url.User.Username(); name != "" {
141 client := req.Header.Get("X-Real-IP")
143 client = req.RemoteAddr
146 if c, _, err := net.SplitHostPort(client); err == nil {
150 duration := float64(time.Now().Sub(ts)) / float64(time.Second)
152 h.logTemplate.Execute(h.writer, logMessageData{
156 RequestDuration: fmt.Sprintf("%0.3f", duration),
157 RequestMethod: req.Method,
158 RequestURI: fmt.Sprintf("%q", url.RequestURI()),
159 ResponseSize: fmt.Sprintf("%d", size),
160 StatusCode: fmt.Sprintf("%d", status),
161 Timestamp: ts.Format("02/Jan/2006:15:04:05 -0700"),
163 UserAgent: fmt.Sprintf("%q", req.UserAgent()),
167 h.writer.Write([]byte("\n"))
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