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)
19 defaultRequestLoggingFormat = "{{.Client}} - {{.Username}} [{{.Timestamp}}] {{.Host}} {{.RequestMethod}} {{.Upstream}} {{.RequestURI}} {{.Protocol}} {{.UserAgent}} {{.StatusCode}} {{.ResponseSize}} {{.RequestDuration}}"
22// responseLogger is wrapper of http.ResponseWriter that keeps track of its HTTP status
24type responseLogger struct {
32// Header returns the ResponseWriter's Header
33func (l *responseLogger) Header() http.Header {
38func (l *responseLogger) Hijack() (rwc net.Conn, buf *bufio.ReadWriter, err error) {
39 if hj, ok := l.w.(http.Hijacker); ok {
42 return nil, nil, errors.New("http.Hijacker is not available on writer")
45// ExtractGAPMetadata extracts and removes GAP headers from the ResponseWriter's
47func (l *responseLogger) ExtractGAPMetadata() {
48 upstream := l.w.Header().Get("GAP-Upstream-Address")
51 l.w.Header().Del("GAP-Upstream-Address")
53 authInfo := l.w.Header().Get("GAP-Auth")
56 l.w.Header().Del("GAP-Auth")
60// Write writes the response using the ResponseWriter
61func (l *responseLogger) Write(b []byte) (int, error) {
63 // The status will be StatusOK if WriteHeader has not been called yet
64 l.status = http.StatusOK
66 l.ExtractGAPMetadata()
67 size, err := l.w.Write(b)
72// WriteHeader writes the status code for the Response
73func (l *responseLogger) WriteHeader(s int) {
74 l.ExtractGAPMetadata()
79// Status returns the response status code
80func (l *responseLogger) Status() int {
84// Size returns teh response size
85func (l *responseLogger) Size() int {
89func (l *responseLogger) Flush() {
90 if flusher, ok := l.w.(http.Flusher); ok {
95// logMessageData is the container for all values that are available as variables in the request logging format.
96// All values are pre-formatted strings so it is easy to use them in the format string.
97type logMessageData struct {
112// loggingHandler is the http.Handler implementation for LoggingHandlerTo and its friends
113type loggingHandler struct {
117 logTemplate *template.Template
120// LoggingHandler provides an http.Handler which logs requests to the HTTP server
121func LoggingHandler(out io.Writer, h http.Handler, v bool, requestLoggingTpl string) http.Handler {
122 return loggingHandler{
126 logTemplate: template.Must(template.New("request-log").Parse(requestLoggingTpl)),
130func (h loggingHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
133 logger := &responseLogger{w: w}
134 h.handler.ServeHTTP(logger, req)
138 h.writeLogLine(logger.authInfo, logger.upstream, req, url, t, logger.Status(), logger.Size())
141// Log entry for req similar to Apache Common Log Format.
142// ts is the timestamp with which the entry should be logged.
143// status, size are used to provide the response HTTP status and size.
144func (h loggingHandler) writeLogLine(username, upstream string, req *http.Request, url url.URL, ts time.Time, status int, size int) {
151 if url.User != nil && username == "-" {
152 if name := url.User.Username(); name != "" {
157 client := req.Header.Get("X-Real-IP")
159 client = req.RemoteAddr
162 if c, _, err := net.SplitHostPort(client); err == nil {
166 duration := float64(time.Now().Sub(ts)) / float64(time.Second)
168 h.logTemplate.Execute(h.writer, logMessageData{
172 RequestDuration: fmt.Sprintf("%0.3f", duration),
173 RequestMethod: req.Method,
174 RequestURI: fmt.Sprintf("%q", url.RequestURI()),
175 ResponseSize: fmt.Sprintf("%d", size),
176 StatusCode: fmt.Sprintf("%d", status),
177 Timestamp: ts.Format("02/Jan/2006:15:04:05 -0700"),
179 UserAgent: fmt.Sprintf("%q", req.UserAgent()),
183 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