Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use zap for http access logging #148

Merged
merged 2 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
License information should be included in most files in the repository.
See /LICENSES folder for complete license text.
9 changes: 9 additions & 0 deletions LICENSES/MIT.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
MIT License

Copyright (c) <year> <copyright holders>

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.
41 changes: 40 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,44 @@ func (reg *Registry) setupRoutes() {
})
}

// SPDX-SnippetBegin
// SPDX-License-Identifier: MIT
// SPDX-SnippetCopyrightText: Copyright (c) 2021 Manfred Touron <[email protected]> (manfred.life)
// SDPX—SnippetName: Function to configure Zap logger with Chi HTTP router
// SPDX-SnippetComment: Original work at https://github.com/moul/chizap/blob/0ebf11a6a5535e3c6bb26f1236b2833ae7825675/chizap.go. All further changes are licensed under this file's main license.

// Request logger for Chi using Zap as the logger.
func (reg *Registry) RequestLogger() func(next http.Handler) http.Handler {
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("responseTimeNSec", time.Since(t1)),
zap.String("userAgent", ua),
)

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

// SPDX-SnippetEnd

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