Skip to content

Commit

Permalink
feat: use zap for http access logging
Browse files Browse the repository at this point in the history
Closes #54
  • Loading branch information
stigok committed Dec 18, 2023
1 parent dca2011 commit c2f42a9
Showing 1 changed file with 55 additions and 1 deletion.
56 changes: 55 additions & 1 deletion pkg/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http"
"strings"
"sync"
"time"

"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
Expand Down Expand Up @@ -83,7 +84,7 @@ func (reg *Registry) SetAuthTokens(authTokens map[string]string) {
// setupRoutes initialises and configures the HTTP router. Must be called before starting the server (`ServeHTTP`).
func (reg *Registry) setupRoutes() {
reg.router = chi.NewRouter()
reg.router.Use(middleware.Logger)
reg.router.Use(reg.RequestLogger())
reg.router.NotFound(reg.NotFound())
reg.router.MethodNotAllowed(reg.MethodNotAllowed())
reg.router.Get("/", reg.Index())
Expand All @@ -98,6 +99,59 @@ func (reg *Registry) setupRoutes() {
})
}

// Request logger for Chi using Zap as the logger.
func (reg *Registry) RequestLogger() func(next http.Handler) http.Handler {
// This method is _adapted_ from https://github.com/moul/chizap/blob/0ebf11a6a5535e3c6bb26f1236b2833ae7825675/chizap.go
// written by Manfred Touron, licensed under the MIT license.
//
//
// Copyright (c) 2021 Manfred Touron <[email protected]> (manfred.life)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
wr := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
t1 := time.Now()
defer func() {
ua := wr.Header().Get("User-Agent")
if ua == "" {
ua = r.Header.Get("User-Agent")
}

reqLogger := reg.logger.With(
zap.String("proto", r.Proto),
zap.String("method", r.Method),
zap.String("path", r.URL.Path),
zap.Int("size", wr.BytesWritten()),
zap.Int("status", wr.Status()),
zap.String("reqId", middleware.GetReqID(r.Context())),
zap.Duration("responseTime", time.Since(t1)),
zap.String("userAgent", ua),
)

reqLogger.Info("HTTP request")
}()
next.ServeHTTP(wr, r)
})
}
}

func (reg *Registry) ServeHTTP(w http.ResponseWriter, r *http.Request) {
reg.router.ServeHTTP(w, r)
}
Expand Down

0 comments on commit c2f42a9

Please sign in to comment.