Skip to content

Commit

Permalink
Improve logging in handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
Icikowski committed May 30, 2022
1 parent 101bf6b commit f37daab
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 30 deletions.
32 changes: 2 additions & 30 deletions application/service/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,7 @@ import (
func getConfigHandlerFunction(log zerolog.Logger, server *http.Server) func(w http.ResponseWriter, r *http.Request) {
log.Info().Msg("preparing configuration handler")
return func(w http.ResponseWriter, r *http.Request) {
innerLog := log.With().
Dict(
"request",
zerolog.Dict().
Str("remote", r.RemoteAddr).
Str("path", r.URL.Path).
Str("method", r.Method),
).
Dict(
"endpoint",
zerolog.Dict().
Str("path", common.ConfigurationEndpoint).
Str("type", "builtin"),
).
Logger()
innerLog := getLoggerForRouteAndRequest(log, common.ConfigurationEndpoint, "builtin", r)

resolveContent := func(target any) error {
var resolverError error
Expand Down Expand Up @@ -105,21 +91,7 @@ func getConfigHandlerFunction(log zerolog.Logger, server *http.Server) func(w ht
func getDefaultHandler(log zerolog.Logger) http.Handler {
log.Info().Msg("preparing default handler")
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
innerLog := log.With().
Dict(
"request",
zerolog.Dict().
Str("remote", r.RemoteAddr).
Str("path", r.URL.Path).
Str("method", r.Method),
).
Dict(
"endpoint",
zerolog.Dict().
Str("path", "*").
Str("type", "builtin"),
).
Logger()
innerLog := getLoggerForRouteAndRequest(log, "*", "builtin", r)

headers := map[string]string{}
for key, value := range r.Header {
Expand Down
19 changes: 19 additions & 0 deletions application/service/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,29 @@ import (
"sort"
"strings"

"github.com/rs/zerolog"
"icikowski.pl/gpts/common"
"icikowski.pl/gpts/config"
)

func getLoggerForRouteAndRequest(log zerolog.Logger, routePath, routeType string, request *http.Request) zerolog.Logger {
return log.With().
Dict(
"route",
zerolog.Dict().
Str("path", routePath).
Str("type", routeType),
).
Dict(
"request",
zerolog.Dict().
Str("remote", request.RemoteAddr).
Str("method", request.Method).
Str("path", request.URL.Path),
).
Logger()
}

func getResponseForError(err error) (int, []byte) {
var details struct {
Status int `json:"status"`
Expand Down
39 changes: 39 additions & 0 deletions application/service/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,54 @@
package service

import (
"bytes"
"encoding/json"
"errors"
"net/http"
"net/url"
"testing"

"github.com/rs/zerolog"
"github.com/stretchr/testify/require"
"icikowski.pl/gpts/common"
"icikowski.pl/gpts/config"
)

func TestGetLoggerForRouteAndRequest(t *testing.T) {
routePath, routeType := "/test", "dummy"
request := &http.Request{
Method: http.MethodGet,
RemoteAddr: "127.0.0.1:8080",
URL: &url.URL{
Scheme: "https",
Host: "localhost",
Path: routePath,
},
}
buffer := bytes.NewBuffer([]byte{})
baseLogger := zerolog.New(buffer)

log := getLoggerForRouteAndRequest(baseLogger, routePath, routeType, request)
log.Info().Msg("test")

parsed := make(map[string]any, 0)
json.NewDecoder(buffer).Decode(&parsed)

for _, key := range []string{"route", "request"} {
require.Containsf(t, parsed, key, "log does not contain %s", key)
}

routeNode := parsed["route"].(map[string]any)
for _, key := range []string{"path", "type"} {
require.Containsf(t, routeNode, key, "log for route does not contain %s", key)
}

requestNode := parsed["request"].(map[string]any)
for _, key := range []string{"remote", "method", "path"} {
require.Containsf(t, requestNode, key, "log for request does not contain %s", key)
}
}

func TestGetResponseForError(t *testing.T) {
tests := map[string]struct {
err error
Expand Down

0 comments on commit f37daab

Please sign in to comment.