Skip to content

Commit

Permalink
Use slog.Logger with webserver-go (#200)
Browse files Browse the repository at this point in the history
The package has been updated to use slog.Logger instead of zap, so we
can provide the logger again, with added Unwrap method so that we can
access the underlying logger rather than our version.
  • Loading branch information
VoyTechnology authored Sep 19, 2023
1 parent e4c0a40 commit 58c13ae
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ jobs:
go-version-file: 'go.mod'

- name: Build
run: go build ./cmd/saferplace -o saferplace
run: go build -o saferplace ./cmd/saferplace
# Autobuild attempts to build any compiled languages (C/C++, C#, Go, Java, or Swift).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
Expand Down
2 changes: 1 addition & 1 deletion cmd/saferplace/saferplace.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func main() {
}

func run() error {
configFile := flag.String("config", "", "Config file")
configFile := flag.String("config", "/etc/saferplace/config.yaml", "Config file")
flag.Parse()

components := saferplace.AllComponents()
Expand Down
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/kelseyhightower/envconfig v1.4.0
github.com/mattn/go-sqlite3 v1.14.17
github.com/minio/minio-go/v7 v7.0.63
github.com/saferplace/webserver-go v0.0.5
github.com/saferplace/webserver-go v0.0.6
go.opentelemetry.io/otel v1.17.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.17.0
go.opentelemetry.io/otel/sdk v1.17.0
Expand Down Expand Up @@ -51,8 +51,6 @@ require (
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.17.0
go.opentelemetry.io/otel/metric v1.17.0 // indirect
go.opentelemetry.io/proto/otlp v1.0.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.25.0 // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/sys v0.12.0 // indirect
Expand Down
10 changes: 2 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ connectrpc.com/connect v1.11.1 h1:dqRwblixqkVh+OFBOOL1yIf1jS/yP0MSJLijRj29bFg=
connectrpc.com/connect v1.11.1/go.mod h1:3AGaO6RRGMx5IKFfqbe3hvK1NqLosFNP2BxDYTPmNPo=
connectrpc.com/otelconnect v0.5.0 h1:K7xQKFbgeaHx563B+IIbd1EJe856AanueIYtGEtdnH8=
connectrpc.com/otelconnect v0.5.0/go.mod h1:cjBMmtJmTokg4/k/3iDjLOjfNVM4qSVfIWz/qWQ8FNw=
github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A=
github.com/benbjohnson/clock v1.3.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bwmarrin/discordgo v0.27.1 h1:ib9AIc/dom1E/fSIulrBwnez0CToJE113ZGt4HoliGY=
Expand Down Expand Up @@ -86,8 +84,8 @@ github.com/rs/xid v1.5.0 h1:mKX4bl4iPYJtEIxp6CYiUuLQ/8DYMoz0PUdtGgMFRVc=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/saferplace/api v0.0.18 h1:bFtvpObqSe3tywPEAlJ9egThPrZQY/3ErZJqS3Zq+is=
github.com/saferplace/api v0.0.18/go.mod h1:lRxRMAomMgkH/vAE0BDiZnotYa0DeMTQybA4sdprWNw=
github.com/saferplace/webserver-go v0.0.5 h1:f3YEOXUdNkVv334mvDBkvHN0tOuchKBRibALOp6DduI=
github.com/saferplace/webserver-go v0.0.5/go.mod h1:ybzRFTKzWiSS9BW+TgbeZr1f5/0/MRgtJQLZqvufhPE=
github.com/saferplace/webserver-go v0.0.6 h1:uhXWjF0pbWr8lT+AE3cnYYozJUuLWwEZLWWJ2GZn174=
github.com/saferplace/webserver-go v0.0.6/go.mod h1:OJkIjWNGIKvtqB1Mgd7ZGWa3gkEVEP+fbUen9KStiSs=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand All @@ -113,10 +111,6 @@ go.opentelemetry.io/proto/otlp v1.0.0 h1:T0TX0tmXU8a3CbNXzEKGeU5mIVOdf0oykP+u2lI
go.opentelemetry.io/proto/otlp v1.0.0/go.mod h1:Sy6pihPLfYHkr3NkUbEhGHFhINUSI/v80hjKIs5JXpM=
go.uber.org/goleak v1.2.1 h1:NBol2c7O1ZokfZ0LEU9K6Whx/KnwvepVetCUhtKja4A=
go.uber.org/goleak v1.2.1/go.mod h1:qlT2yGI9QafXHhZZLxlSuNsMw3FFLxBr+tBRlmO1xH4=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.25.0 h1:4Hvk6GtkucQ790dqmj7l1eEnRdKm3k3ZUrUMS2d5+5c=
go.uber.org/zap v1.25.0/go.mod h1:JIAUzQIH94IC4fOJQm7gMmBJP5k7wQfdcnYdPoEXJYk=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk=
golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw=
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/saferplace/saferplace.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package saferplace
import (
"context"
"fmt"
"log/slog"
"net/http"
_ "net/http/pprof"

Expand Down Expand Up @@ -83,8 +84,7 @@ func Run(ctx context.Context, components []Component, cfg *config.Config) (err e
}

srv, err := webserver.New(
// TODO: Enable slog in webserver
// webserver.Logger(deps.logger.With(slog.String("component", "server"))),
webserver.Logger(deps.logger.Unwrap().With(slog.String("component", "server"))),
webserver.Services(services...),
webserver.TLSConfig(tlsConfig),
webserver.Middlewares(middlewares...),
Expand Down
6 changes: 6 additions & 0 deletions internal/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Logger interface {
Error(context.Context, string, ...slog.Attr)

With(...slog.Attr) Logger
Unwrap() *slog.Logger
}

var _ Logger = (*logger)(nil)
Expand Down Expand Up @@ -60,6 +61,11 @@ func (l *logger) With(args ...slog.Attr) Logger {
return New(l.l.Handler().WithAttrs(args))
}

// Unwrap returns the underlying [slog.Logger]
func (l *logger) Unwrap() *slog.Logger {
return l.l
}

// Error is the standard Entry for when we want to log an error
func Error(err error) slog.Attr {
return slog.String("error", err.Error())
Expand Down

0 comments on commit 58c13ae

Please sign in to comment.