Skip to content

Commit

Permalink
feat: Use rs/zerolog instead of slog
Browse files Browse the repository at this point in the history
  • Loading branch information
romshark committed Nov 17, 2024
1 parent ec24a7c commit 4f6b52a
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 14 deletions.
4 changes: 3 additions & 1 deletion cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"

"github.com/romshark/demo-islands/server"
"github.com/rs/zerolog"
)

func main() {
Expand All @@ -19,7 +20,8 @@ func main() {
)
flag.Parse()

logAccess, logError := slog.Default(), slog.Default()
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
logAccess, logError := zerolog.New(os.Stdout), zerolog.New(os.Stdout)

_, devMode := os.LookupEnv("DEV")
s := server.New(logAccess, logError, server.Config{
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/andybalholm/brotli v1.1.1
github.com/brianvoe/gofakeit/v7 v7.1.2
github.com/romshark/templier v0.8.0
github.com/rs/zerolog v1.33.0
github.com/stretchr/testify v1.9.0
)

Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UF
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cli/browser v1.3.0 h1:LejqCrpWr+1pRqmEPDGnTZOjsMe7sehifLynZJuqJpo=
github.com/cli/browser v1.3.0/go.mod h1:HH8s+fOAxjhQoBUAsKuPCbqUuxZDhQ2/aD+SzsEfBTk=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
Expand All @@ -36,6 +37,7 @@ github.com/go-playground/validator/v10 v10.22.1 h1:40JcKH+bBNGFczGuoBYgX4I6m/i27
github.com/go-playground/validator/v10 v10.22.1/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
Expand All @@ -50,10 +52,12 @@ github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjS
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/natefinch/atomic v1.0.1 h1:ZPYKxkqQOx3KZ+RsbnP/YsgvxWQPGxjC0oBt2AhwV0A=
github.com/natefinch/atomic v1.0.1/go.mod h1:N/D/ELrljoqDyT3rZrsUmtsuzvHkeB/wWjHV22AZRbM=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
Expand All @@ -62,6 +66,9 @@ github.com/romshark/templier v0.8.0 h1:KBdexESwlpQRaLkpF5k0ky4BuLa9oD5uJtl+OJUQd
github.com/romshark/templier v0.8.0/go.mod h1:12/rCedI9f4aa5Pr7z1myQ91kE09a22iI0nEmtxf6ks=
github.com/romshark/yamagiconf v1.0.2 h1:8qHzaWfQgkgOvcoGeaKsbJIuj5aGQPYZNgbnKpwXDtM=
github.com/romshark/yamagiconf v1.0.2/go.mod h1:e+3iWCTrGxFdacKtMr/76QX9nXyDYUB9zYm/UvJ2tOg=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/segmentio/asm v1.2.0 h1:9BQrFxC+YOHJlTlHGkTrFWf59nbL3XnCoFLTwDCI7ys=
github.com/segmentio/asm v1.2.0/go.mod h1:BqMnlJP91P8d+4ibuonYZw9mfnzI9HfxselHZr5aAcs=
github.com/segmentio/encoding v0.4.0 h1:MEBYvRqiUB2nfR2criEXWqwdY6HJOUrCn5hboVOVmy8=
Expand Down Expand Up @@ -112,6 +119,7 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
Expand Down
Binary file added server/mem.prof
Binary file not shown.
6 changes: 3 additions & 3 deletions server/middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ package middleware
import (
"context"
"io"
"log/slog"
"net/http"
"time"

"github.com/andybalholm/brotli"
"github.com/rs/zerolog"
)

// Compress enables brotli/gzip compression.
Expand Down Expand Up @@ -74,7 +74,7 @@ func GetCtxTheme(ctx context.Context) Theme {
// can be retrieved using GetCtxTheme.
//
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-CH-Prefers-Color-Scheme
func UserPreferences(log *slog.Logger, next http.Handler) http.Handler {
func UserPreferences(log zerolog.Logger, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Tell the browser we're interested in receiveing the theme (dark/light)
// preferences of the user to improve UX.
Expand All @@ -83,7 +83,7 @@ func UserPreferences(log *slog.Logger, next http.Handler) http.Handler {
theme := ThemeDefault // Default theme.
themeFromCookie, err := getThemeCookie(r)
if err != nil {
log.Error("reading theme cookie", slog.Any("error", err))
log.Error().Err(err).Msg("reading theme cookie")
} else if themeFromCookie == "dark" {
// Theme was explicitly set to dark via cookie.
theme = ThemeDark
Expand Down
20 changes: 10 additions & 10 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package server

import (
"embed"
"log/slog"
"net/http"
"sort"
"time"

"github.com/brianvoe/gofakeit/v7"
"github.com/rs/zerolog"

"github.com/romshark/demo-islands/domain"
"github.com/romshark/demo-islands/internal/rand"
Expand All @@ -21,8 +21,8 @@ import (
var embedDirPublic embed.FS

type Server struct {
logAccess *slog.Logger
logError *slog.Logger
logAccess zerolog.Logger
logError zerolog.Logger
m *http.ServeMux

// Pre-computed values.
Expand All @@ -40,7 +40,7 @@ type Config struct {
EnableCompression bool
}

func New(logAccess, logError *slog.Logger, conf Config) *Server {
func New(logAccess, logError zerolog.Logger, conf Config) *Server {
s := &Server{
logAccess: logAccess,
logError: logError,
Expand Down Expand Up @@ -164,11 +164,11 @@ func newShippingCompanyOptions() []template.NamedOption {
}

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.logAccess.Info("access",
slog.String("method", r.Method),
slog.String("path", r.URL.Path),
slog.String("query", r.URL.Query().Encode()),
)
s.logAccess.Info().
Str("method", r.Method).
Str("path", r.URL.Path).
Str("query", r.URL.Query().Encode()).
Msg("access")
s.m.ServeHTTP(w, r)
}

Expand Down Expand Up @@ -272,7 +272,7 @@ func (s *Server) handlePostOrders(w http.ResponseWriter, r *http.Request) {
}

func (s *Server) errInternal(w http.ResponseWriter, err error) {
s.logError.Error("internal", slog.Any("error", err))
s.logError.Error().Err(err).Msg("internal")
http.Error(w,
http.StatusText(http.StatusInternalServerError),
http.StatusInternalServerError)
Expand Down
27 changes: 27 additions & 0 deletions server/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package server_test

import (
"net/http"
"net/http/httptest"
"os"
"testing"

"github.com/romshark/demo-islands/server"
"github.com/rs/zerolog"
)

func BenchmarkServer(b *testing.B) {
logAccess := zerolog.New(os.Stdout)
logErr := zerolog.New(os.Stdout)
s := server.New(logAccess, logErr, server.Config{
ProductionMode: true,
EnableCompression: true,
})
request := httptest.NewRequest(http.MethodGet, "/", nil)
b.RunParallel(func(p *testing.PB) {
for p.Next() {
responseRecorder := httptest.NewRecorder()
s.ServeHTTP(responseRecorder, request)
}
})
}
Binary file added srv
Binary file not shown.

0 comments on commit 4f6b52a

Please sign in to comment.