Skip to content

Commit 6292abe

Browse files
Replace logrus with slog and upgrade to pgx/v5
1 parent c13400c commit 6292abe

File tree

6 files changed

+63
-41
lines changed

6 files changed

+63
-41
lines changed

go.mod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ require (
1010
github.com/golang-migrate/migrate/v4 v4.17.1
1111
github.com/google/go-cmp v0.6.0
1212
github.com/jackc/pgtype v1.14.3
13-
github.com/jackc/pgx/v4 v4.18.3
13+
github.com/jackc/pgx/v5 v5.5.4
14+
github.com/mcosta74/pgx-slog v0.3.1
1415
github.com/ory/dockertest/v3 v3.6.0
1516
github.com/sirupsen/logrus v1.9.3
1617
github.com/soheilhy/cmux v0.1.5
@@ -35,12 +36,11 @@ require (
3536
github.com/golang/protobuf v1.5.4 // indirect
3637
github.com/hashicorp/errwrap v1.1.0 // indirect
3738
github.com/hashicorp/go-multierror v1.1.1 // indirect
38-
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
39-
github.com/jackc/pgconn v1.14.3 // indirect
4039
github.com/jackc/pgio v1.0.0 // indirect
4140
github.com/jackc/pgpassfile v1.0.0 // indirect
42-
github.com/jackc/pgproto3/v2 v2.3.3 // indirect
4341
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
42+
github.com/jackc/pgx v3.6.2+incompatible // indirect
43+
github.com/jackc/puddle/v2 v2.2.1 // indirect
4444
github.com/jhump/protoreflect v1.16.0 // indirect
4545
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 // indirect
4646
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect

go.sum

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78/go.mod h1:LmzpDX
44
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 h1:L/gRVlceqvL25UVaW/CKtUDjefjrs0SPonmDGUVOYP0=
55
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
66
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
7-
github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030IGemrRc=
87
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
98
github.com/Masterminds/squirrel v1.5.4 h1:uUcX/aBc8O7Fg9kaISIUsHXdKuqehiXAMQTYX8afzqM=
109
github.com/Masterminds/squirrel v1.5.4/go.mod h1:NNaOrjSoIDfDA40n7sr2tPNZRfjzjA400rg+riTZj10=
@@ -86,8 +85,9 @@ github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/me
8685
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
8786
github.com/gofrs/flock v0.8.1 h1:+gYjHKf32LDeiEEFhQaotPbLuUXjY5ZqxKgXy7n59aw=
8887
github.com/gofrs/flock v0.8.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU=
89-
github.com/gofrs/uuid v4.0.0+incompatible h1:1SD/1F5pU8p29ybwgQSwpQk+mwdRrXCYuPhW6m+TnJw=
9088
github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
89+
github.com/gofrs/uuid v4.4.0+incompatible h1:3qXRTX8/NbyulANqlc0lchS1gqAVxRgsuW1YrTJupqA=
90+
github.com/gofrs/uuid v4.4.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
9191
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
9292
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
9393
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
@@ -130,10 +130,13 @@ github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+l
130130
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
131131
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
132132
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
133+
github.com/jackc/chunkreader v1.0.0 h1:4s39bBR8ByfqH+DKm8rQA3E1LHZWB9XWcrz8fqaZbe0=
133134
github.com/jackc/chunkreader v1.0.0/go.mod h1:RT6O25fNZIuasFJRyZ4R/Y2BbhasbmZXF9QQ7T3kePo=
134135
github.com/jackc/chunkreader/v2 v2.0.0/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk=
135136
github.com/jackc/chunkreader/v2 v2.0.1 h1:i+RDz65UE+mmpjTfyz0MoVTnzeYxroil2G82ki7MGG8=
136137
github.com/jackc/chunkreader/v2 v2.0.1/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk=
138+
github.com/jackc/fake v0.0.0-20150926172116-812a484cc733 h1:vr3AYkKovP8uR8AvSGGUK1IDqRa5lAAvEkZG1LKaCRc=
139+
github.com/jackc/fake v0.0.0-20150926172116-812a484cc733/go.mod h1:WrMFNQdiFJ80sQsxDoMokWK1W5TQtxBFNpzWTD84ibQ=
137140
github.com/jackc/pgconn v0.0.0-20190420214824-7e0022ef6ba3/go.mod h1:jkELnwuX+w9qN5YIfX0fl88Ehu4XC3keFuOJJk9pcnA=
138141
github.com/jackc/pgconn v0.0.0-20190824142844-760dd75542eb/go.mod h1:lLjNuW/+OfW9/pnVKPazfWOgNfH2aPem8YQ7ilXGvJE=
139142
github.com/jackc/pgconn v0.0.0-20190831204454-2fabfa3c18b7/go.mod h1:ZJKsE/KZfsUgOEh9hBm+xYTstcNHg7UPMVJqRfQxq4s=
@@ -146,10 +149,10 @@ github.com/jackc/pgio v1.0.0 h1:g12B9UwVnzGhueNavwioyEEpAmqMe1E/BN9ES+8ovkE=
146149
github.com/jackc/pgio v1.0.0/go.mod h1:oP+2QK2wFfUWgr+gxjoBH9KGBb31Eio69xUb0w5bYf8=
147150
github.com/jackc/pgmock v0.0.0-20190831213851-13a1b77aafa2/go.mod h1:fGZlG77KXmcq05nJLRkk0+p82V8B8Dw8KN2/V9c/OAE=
148151
github.com/jackc/pgmock v0.0.0-20201204152224-4fe30f7445fd/go.mod h1:hrBW0Enj2AZTNpt/7Y5rr2xe/9Mn757Wtb2xeBzPv2c=
149-
github.com/jackc/pgmock v0.0.0-20210724152146-4ad1a8207f65 h1:DadwsjnMwFjfWc9y5Wi/+Zz7xoE5ALHsRQlOctkOiHc=
150152
github.com/jackc/pgmock v0.0.0-20210724152146-4ad1a8207f65/go.mod h1:5R2h2EEX+qri8jOWMbJCtaPWkrrNc7OHwsp2TCqp7ak=
151153
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
152154
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
155+
github.com/jackc/pgproto3 v1.1.0 h1:FYYE4yRw+AgI8wXIinMlNjBbp/UitDJwfj5LqqewP1A=
153156
github.com/jackc/pgproto3 v1.1.0/go.mod h1:eR5FA3leWg7p9aeAqi37XOTgTIbkABlvcPB3E5rlc78=
154157
github.com/jackc/pgproto3/v2 v2.0.0-alpha1.0.20190420180111-c116219b62db/go.mod h1:bhq50y+xrl9n5mRYyCBFKkpRVTLYJVWeCc+mEAI3yXA=
155158
github.com/jackc/pgproto3/v2 v2.0.0-alpha1.0.20190609003834-432c2951c711/go.mod h1:uH0AWtUmuShn0bcesswc4aBTWGvw0cAxIJp+6OB//Wg=
@@ -170,17 +173,23 @@ github.com/jackc/pgtype v1.8.1-0.20210724151600-32e20a603178/go.mod h1:C516IlIV9
170173
github.com/jackc/pgtype v1.14.0/go.mod h1:LUMuVrfsFfdKGLw+AFFVv6KtHOFMwRgDDzBt76IqCA4=
171174
github.com/jackc/pgtype v1.14.3 h1:h6W9cPuHsRWQFTWUZMAKMgG5jSwQI0Zurzdvlx3Plus=
172175
github.com/jackc/pgtype v1.14.3/go.mod h1:aKeozOde08iifGosdJpz9MBZonJOUJxqNpPBcMJTlVA=
176+
github.com/jackc/pgx v3.6.2+incompatible h1:2zP5OD7kiyR3xzRYMhOcXVvkDZsImVXfj+yIyTQf3/o=
177+
github.com/jackc/pgx v3.6.2+incompatible/go.mod h1:0ZGrqGqkRlliWnWB4zKnWtjbSWbGkVEFm4TeybAXq+I=
173178
github.com/jackc/pgx/v4 v4.0.0-20190420224344-cc3461e65d96/go.mod h1:mdxmSJJuR08CZQyj1PVQBHy9XOp5p8/SHH6a0psbY9Y=
174179
github.com/jackc/pgx/v4 v4.0.0-20190421002000-1b8f0016e912/go.mod h1:no/Y67Jkk/9WuGR0JG/JseM9irFbnEPbuWV2EELPNuM=
175180
github.com/jackc/pgx/v4 v4.0.0-pre1.0.20190824185557-6972a5742186/go.mod h1:X+GQnOEnf1dqHGpw7JmHqHc1NxDoalibchSk9/RWuDc=
176181
github.com/jackc/pgx/v4 v4.12.1-0.20210724153913-640aa07df17c/go.mod h1:1QD0+tgSXP7iUjYm9C1NxKhny7lq6ee99u/z+IHFcgs=
177182
github.com/jackc/pgx/v4 v4.18.2/go.mod h1:Ey4Oru5tH5sB6tV7hDmfWFahwF15Eb7DNXlRKx2CkVw=
178183
github.com/jackc/pgx/v4 v4.18.3 h1:dE2/TrEsGX3RBprb3qryqSV9Y60iZN1C6i8IrmW9/BA=
179184
github.com/jackc/pgx/v4 v4.18.3/go.mod h1:Ey4Oru5tH5sB6tV7hDmfWFahwF15Eb7DNXlRKx2CkVw=
185+
github.com/jackc/pgx/v5 v5.5.4 h1:Xp2aQS8uXButQdnCMWNmvx6UysWQQC+u1EoizjguY+8=
186+
github.com/jackc/pgx/v5 v5.5.4/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A=
180187
github.com/jackc/puddle v0.0.0-20190413234325-e4ced69a3a2b/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
181188
github.com/jackc/puddle v0.0.0-20190608224051-11cab39313c9/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
182189
github.com/jackc/puddle v1.1.3/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
183190
github.com/jackc/puddle v1.3.0/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
191+
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
192+
github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
184193
github.com/jhump/protoreflect v1.16.0 h1:54fZg+49widqXYQ0b+usAFHbMkBGR4PpXrsHc8+TBDg=
185194
github.com/jhump/protoreflect v1.16.0/go.mod h1:oYPd7nPvcBw/5wlDfm/AVmU9zH9BgqGCI469pGxfj/8=
186195
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
@@ -213,6 +222,8 @@ github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hd
213222
github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
214223
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
215224
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
225+
github.com/mcosta74/pgx-slog v0.3.1 h1:zsyxnYEjJ2hVQYC/SxcHkzcb5K+CSgmLVcBsK6460M8=
226+
github.com/mcosta74/pgx-slog v0.3.1/go.mod h1:73/rhilX7+ybQ9RH/BZBtOkTDiGAH1yBrcatN6jQW5E=
216227
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
217228
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
218229
github.com/moby/sys/mountinfo v0.4.1/go.mod h1:rEr8tzG/lsIZHBtN/JjGG+LMYx9eXgW2JI+6q0qou+A=
@@ -261,8 +272,9 @@ github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQD
261272
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
262273
github.com/seccomp/libseccomp-golang v0.9.1/go.mod h1:GbW5+tmTXfcxTToHLXlScSlAvWlF4P2Ca7zGrPiEpWo=
263274
github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9NzErvs504Cn4c5DxATwIqPbtswREoFCre64PpcG4=
264-
github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=
265275
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
276+
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
277+
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
266278
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
267279
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
268280
github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=

main.go

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ package main
33
import (
44
"context"
55
"fmt"
6+
"log/slog"
67
"net"
78
"net/http"
89
"net/url"
910
"os"
1011
"time"
1112

1213
"github.com/fullstorydev/grpcui/standalone"
13-
"github.com/sirupsen/logrus"
1414
"github.com/soheilhy/cmux"
1515
"google.golang.org/grpc"
1616
"google.golang.org/grpc/credentials/insecure"
@@ -23,16 +23,17 @@ import (
2323
const defaultPort = "8080"
2424

2525
func main() {
26-
log := logrus.New()
27-
log.Formatter = &logrus.JSONFormatter{}
26+
log := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}))
2827

2928
pgURL := os.Getenv("POSTGRES_URL")
3029
if pgURL == "" {
31-
log.Fatal("POSTGRES_URL must be set")
30+
log.Error("POSTGRES_URL must be set")
31+
return
3232
}
3333
parsedURL, err := url.Parse(pgURL)
3434
if err != nil {
35-
log.WithError(err).Fatal("Failed to parse POSTGRES_URL")
35+
log.Error("Failed to parse POSTGRES_URL", "error", err)
36+
return
3637
}
3738

3839
port := defaultPort
@@ -41,7 +42,8 @@ func main() {
4142
}
4243
lis, err := net.Listen("tcp", ":"+port)
4344
if err != nil {
44-
log.WithError(err).Fatal("Failed to create listener")
45+
log.Error("Failed to create listener", "error", err)
46+
return
4547
}
4648

4749
mux := cmux.New(lis)
@@ -51,7 +53,8 @@ func main() {
5153
go func() {
5254
sErr := mux.Serve()
5355
if sErr != nil {
54-
log.WithError(err).Fatal("Failed to serve cmux")
56+
log.Error("Failed to serve cmux", "error", sErr)
57+
return
5558
}
5659
}()
5760

@@ -61,16 +64,18 @@ func main() {
6164
var dir userspb.UserServiceServer
6265
dir, err = users.NewDirectory(log, parsedURL)
6366
if err != nil {
64-
log.WithError(err).Fatal("Failed to create user directory")
67+
log.Error("Failed to create user directory", "error", err)
68+
return
6569
}
6670
userspb.RegisterUserServiceServer(s, dir)
6771

6872
// Serve gRPC Server
6973
go func() {
70-
log.Info("Serving gRPC on ", grpcL.Addr().String())
74+
log.Info("Serving gRPC on " + grpcL.Addr().String())
7175
sErr := s.Serve(grpcL)
7276
if sErr != nil {
73-
log.WithError(err).Fatal("Failed to serve gRPC")
77+
log.Error("Failed to serve gRPC", "error", sErr)
78+
return
7479
}
7580
}()
7681

@@ -80,13 +85,15 @@ func main() {
8085
sAddr := fmt.Sprintf("dns:///0.0.0.0:%s", port)
8186
cc, err := grpc.NewClient(sAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
8287
if err != nil {
83-
log.WithError(err).Fatal("Failed to dial local server")
88+
log.Error("Failed to dial local server", "error", err)
89+
return
8490
}
8591
defer cc.Close()
8692

8793
handler, err := standalone.HandlerViaReflection(ctx, cc, sAddr)
8894
if err != nil {
89-
log.WithError(err).Fatal("Failed to create grpc UI handler")
95+
log.Error("Failed to create grpc UI handler", "error", err)
96+
return
9097
}
9198

9299
httpS := &http.Server{
@@ -97,6 +104,7 @@ func main() {
97104
log.Info("Serving Web UI on http://0.0.0.0:", port)
98105
err = httpS.Serve(httpL)
99106
if err != http.ErrServerClosed {
100-
log.WithError(err).Fatal("Failed to serve Web UI")
107+
log.Error("failed to serve Web UI", "error", err)
108+
return
101109
}
102110
}

users/helpers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"github.com/golang-migrate/migrate/v4/database/cockroachdb"
1212
"github.com/golang-migrate/migrate/v4/database/postgres"
1313
"github.com/golang-migrate/migrate/v4/source/iofs"
14-
"github.com/jackc/pgx/v4"
14+
"github.com/jackc/pgx/v5"
1515
"google.golang.org/grpc/codes"
1616
"google.golang.org/grpc/status"
1717
"google.golang.org/protobuf/types/known/timestamppb"

users/users.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ import (
44
"context"
55
"database/sql"
66
"fmt"
7+
"log/slog"
78
"net/url"
89

910
"github.com/Masterminds/squirrel"
1011
"github.com/jackc/pgtype"
11-
"github.com/jackc/pgx/v4"
12-
"github.com/jackc/pgx/v4/log/logrusadapter"
13-
"github.com/jackc/pgx/v4/stdlib"
14-
"github.com/sirupsen/logrus"
12+
"github.com/jackc/pgx/v5"
13+
"github.com/jackc/pgx/v5/stdlib"
14+
"github.com/jackc/pgx/v5/tracelog"
15+
slogadapter "github.com/mcosta74/pgx-slog"
1516
"google.golang.org/grpc/codes"
1617
"google.golang.org/grpc/status"
1718
"google.golang.org/protobuf/types/known/emptypb"
@@ -21,15 +22,15 @@ import (
2122

2223
// Directory stores a directory of users.
2324
type Directory struct {
24-
logger *logrus.Logger
25+
logger *slog.Logger
2526
db *sql.DB
2627
sb squirrel.StatementBuilderType
2728
querier Querier
2829
}
2930

3031
// NewDirectory creates a new Directory, connecting it to the postgres server on
3132
// the URL provided.
32-
func NewDirectory(logger *logrus.Logger, pgURL *url.URL) (*Directory, error) {
33+
func NewDirectory(logger *slog.Logger, pgURL *url.URL) (*Directory, error) {
3334
connURL := *pgURL
3435
if connURL.Scheme == "cockroachdb" {
3536
// Overwrite the scheme before parsing with pgx, since
@@ -41,7 +42,10 @@ func NewDirectory(logger *logrus.Logger, pgURL *url.URL) (*Directory, error) {
4142
return nil, fmt.Errorf("parsing postgres URI: %w", err)
4243
}
4344

44-
c.Logger = logrusadapter.NewLogger(logger)
45+
c.Tracer = &tracelog.TraceLog{
46+
Logger: slogadapter.NewLogger(logger),
47+
LogLevel: tracelog.LogLevelTrace,
48+
}
4549
db := stdlib.OpenDB(*c)
4650

4751
err = validateSchema(db, pgURL.Scheme)

users/users_test.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ import (
55
"database/sql"
66
"fmt"
77
"io"
8-
"io/ioutil"
8+
"log/slog"
99
"net"
1010
"net/url"
11+
"os"
1112
"runtime"
1213
"testing"
1314
"time"
1415

1516
"github.com/google/go-cmp/cmp"
1617
"github.com/ory/dockertest/v3"
1718
"github.com/ory/dockertest/v3/docker"
18-
"github.com/sirupsen/logrus"
1919
"google.golang.org/grpc"
2020
"google.golang.org/grpc/codes"
2121
"google.golang.org/grpc/status"
@@ -27,7 +27,7 @@ import (
2727
"github.com/johanbrandhorst/grpc-postgres/users"
2828
)
2929

30-
func startDatabase(tb testing.TB, log *logrus.Logger) *url.URL {
30+
func startDatabase(tb testing.TB, log *slog.Logger) *url.URL {
3131
tb.Helper()
3232

3333
pgURL := &url.URL{
@@ -71,8 +71,8 @@ func startDatabase(tb testing.TB, log *logrus.Logger) *url.URL {
7171

7272
logWaiter, err := pool.Client.AttachToContainerNonBlocking(docker.AttachToContainerOptions{
7373
Container: resource.Container.ID,
74-
OutputStream: log.Writer(),
75-
ErrorStream: log.Writer(),
74+
OutputStream: os.Stdout,
75+
ErrorStream: os.Stdout,
7676
Stderr: true,
7777
Stdout: true,
7878
Stream: true,
@@ -117,7 +117,7 @@ func startDatabase(tb testing.TB, log *logrus.Logger) *url.URL {
117117
func TestAddDeleteUser(t *testing.T) {
118118
t.Parallel()
119119

120-
log := logrus.New()
120+
log := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}))
121121
directory, err := users.NewDirectory(log, startDatabase(t, log))
122122
if err != nil {
123123
t.Fatalf("Failed to create a new directory: %s", err)
@@ -187,7 +187,7 @@ func TestAddDeleteUser(t *testing.T) {
187187
func TestListUsers(t *testing.T) {
188188
t.Parallel()
189189

190-
log := logrus.New()
190+
log := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}))
191191
directory, err := users.NewDirectory(log, startDatabase(t, log))
192192
if err != nil {
193193
t.Fatalf("Failed to create a new directory: %s", err)
@@ -338,7 +338,7 @@ func TestListUsers(t *testing.T) {
338338
func TestAddUsers(t *testing.T) {
339339
t.Parallel()
340340

341-
log := logrus.New()
341+
log := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}))
342342
directory, err := users.NewDirectory(log, startDatabase(t, log))
343343
if err != nil {
344344
t.Fatalf("Failed to create a new directory: %s", err)
@@ -387,8 +387,7 @@ func TestAddUsers(t *testing.T) {
387387

388388
func BenchmarkAddUsers(b *testing.B) {
389389
b.Skip("Benchmarks take a while to run")
390-
log := logrus.New()
391-
log.Out = ioutil.Discard
390+
log := slog.New(slog.NewTextHandler(io.Discard, nil))
392391
directory, err := users.NewDirectory(log, startDatabase(b, log))
393392
if err != nil {
394393
b.Fatalf("Failed to create a new directory: %s", err)
@@ -426,8 +425,7 @@ var benchmarkUser *userspb.User
426425

427426
func BenchmarkAddUser(b *testing.B) {
428427
b.Skip("Benchmarks take a while to run")
429-
log := logrus.New()
430-
log.Out = ioutil.Discard
428+
log := slog.New(slog.NewTextHandler(io.Discard, nil))
431429
directory, err := users.NewDirectory(log, startDatabase(b, log))
432430
if err != nil {
433431
b.Fatalf("Failed to create a new directory: %s", err)

0 commit comments

Comments
 (0)