Skip to content

Commit 52c37cb

Browse files
committed
chore: migrate from zap to slog
Replace go.uber.org/zap with stdlib log/slog throughout the plugin.
1 parent fe5e3f8 commit 52c37cb

6 files changed

Lines changed: 39 additions & 42 deletions

File tree

handler_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"errors"
7+
"log/slog"
78
"net/http"
89
"net/http/httptest"
910
"net/url"
@@ -14,7 +15,6 @@ import (
1415
apiStatus "github.com/roadrunner-server/api-plugins/v6/status"
1516
"github.com/stretchr/testify/assert"
1617
"github.com/stretchr/testify/require"
17-
"go.uber.org/zap"
1818
)
1919

2020
// --- Mock implementations ---
@@ -72,7 +72,7 @@ func parseJobsReports(t *testing.T, body []byte) []*JobsReport {
7272
// --- Health Handler Tests ---
7373

7474
func TestHealthHandler(t *testing.T) {
75-
log := zap.NewNop()
75+
log := slog.New(slog.DiscardHandler)
7676

7777
// ---- All-plugins path (no query params) ----
7878

@@ -313,7 +313,7 @@ func TestHealthHandler(t *testing.T) {
313313
// --- Ready Handler Tests ---
314314

315315
func TestReadyHandler(t *testing.T) {
316-
log := zap.NewNop()
316+
log := slog.New(slog.DiscardHandler)
317317

318318
// ---- All-plugins path (no query params) ----
319319

@@ -555,7 +555,7 @@ func TestReadyHandler(t *testing.T) {
555555
// --- Jobs Handler Tests ---
556556

557557
func TestJobsHandler(t *testing.T) {
558-
log := zap.NewNop()
558+
log := slog.New(slog.DiscardHandler)
559559

560560
t.Run("Shutdown", func(t *testing.T) {
561561
h := NewJobsHandler(nil, newShutdownPtr(true), log, http.StatusServiceUnavailable)
@@ -641,7 +641,7 @@ func FuzzHealthPluginQuery(f *testing.F) {
641641
registry := map[string]Checker{
642642
"http": &mockChecker{name: "http", st: &apiStatus.Status{Code: 200}},
643643
}
644-
log := zap.NewNop()
644+
log := slog.New(slog.DiscardHandler)
645645
h := NewHealthHandler(registry, newShutdownPtr(false), log, http.StatusServiceUnavailable)
646646

647647
f.Fuzz(func(t *testing.T, query string) {
@@ -665,7 +665,7 @@ func FuzzReadyPluginQuery(f *testing.F) {
665665
registry := map[string]Readiness{
666666
"http": &mockReadiness{name: "http", st: &apiStatus.Status{Code: 200}},
667667
}
668-
log := zap.NewNop()
668+
log := slog.New(slog.DiscardHandler)
669669
h := NewReadyHandler(registry, newShutdownPtr(false), log, http.StatusServiceUnavailable)
670670

671671
f.Fuzz(func(t *testing.T, query string) {

health.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,19 @@ package status
22

33
import (
44
"encoding/json"
5+
"log/slog"
56
"net/http"
67
"sync/atomic"
7-
8-
"go.uber.org/zap"
98
)
109

1110
type Health struct {
12-
log *zap.Logger
11+
log *slog.Logger
1312
unavailableStatusCode int
1413
statusRegistry map[string]Checker
1514
shutdownInitiated *atomic.Pointer[bool]
1615
}
1716

18-
func NewHealthHandler(sr map[string]Checker, shutdownInitiated *atomic.Pointer[bool], log *zap.Logger, usc int) *Health {
17+
func NewHealthHandler(sr map[string]Checker, shutdownInitiated *atomic.Pointer[bool], log *slog.Logger, usc int) *Health {
1918
return &Health{
2019
statusRegistry: sr,
2120
unavailableStatusCode: usc,
@@ -46,7 +45,7 @@ func (rd *Health) ServeHTTP(w http.ResponseWriter, r *http.Request) {
4645
StatusCode: http.StatusNotFound,
4746
})
4847

49-
rd.log.Info("plugin is nil or not initialized", zap.String("plugin", k))
48+
rd.log.Info("plugin is nil or not initialized", "plugin", k)
5049
continue
5150
}
5251

@@ -96,13 +95,13 @@ func (rd *Health) ServeHTTP(w http.ResponseWriter, r *http.Request) {
9695
data, err := json.Marshal(report)
9796
if err != nil {
9897
// TODO do we need to write this error to the ResponseWriter?
99-
rd.log.Error("failed to marshal response", zap.Error(err))
98+
rd.log.Error("failed to marshal response", "error", err)
10099
return
101100
}
102101
// write the response
103102
_, err = w.Write(data)
104103
if err != nil {
105-
rd.log.Error("failed to write response", zap.Error(err))
104+
rd.log.Error("failed to write response", "error", err)
106105
}
107106

108107
return
@@ -158,18 +157,18 @@ func (rd *Health) ServeHTTP(w http.ResponseWriter, r *http.Request) {
158157
})
159158
}
160159
} else {
161-
rd.log.Info("plugin does not support health checks", zap.String("plugin", plg[i]))
160+
rd.log.Info("plugin does not support health checks", "plugin", plg[i])
162161
}
163162
}
164163

165164
data, err := json.Marshal(report)
166165
if err != nil {
167-
rd.log.Error("failed to marshal response", zap.Error(err))
166+
rd.log.Error("failed to marshal response", "error", err)
168167
}
169168

170169
// write the response
171170
_, err = w.Write(data)
172171
if err != nil {
173-
rd.log.Error("failed to write response", zap.Error(err))
172+
rd.log.Error("failed to write response", "error", err)
174173
}
175174
}

jobs.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,19 @@ package status
33
import (
44
"context"
55
"encoding/json"
6+
"log/slog"
67
"net/http"
78
"sync/atomic"
8-
9-
"go.uber.org/zap"
109
)
1110

1211
type Jobs struct {
1312
statusJobsRegistry JobsChecker
1413
unavailableStatusCode int
15-
log *zap.Logger
14+
log *slog.Logger
1615
shutdownInitiated *atomic.Pointer[bool]
1716
}
1817

19-
func NewJobsHandler(jc JobsChecker, shutdownInitiated *atomic.Pointer[bool], log *zap.Logger, usc int) *Jobs {
18+
func NewJobsHandler(jc JobsChecker, shutdownInitiated *atomic.Pointer[bool], log *slog.Logger, usc int) *Jobs {
2019
return &Jobs{
2120
statusJobsRegistry: jc,
2221
unavailableStatusCode: usc,
@@ -38,7 +37,7 @@ func (jb *Jobs) ServeHTTP(w http.ResponseWriter, _ *http.Request) {
3837

3938
jobStates, err := jb.statusJobsRegistry.JobsState(context.Background())
4039
if err != nil {
41-
jb.log.Error("jobs state", zap.Error(err))
40+
jb.log.Error("jobs state", "error", err)
4241
http.Error(w, "jobs plugin not found", jb.unavailableStatusCode)
4342
return
4443
}
@@ -62,12 +61,12 @@ func (jb *Jobs) ServeHTTP(w http.ResponseWriter, _ *http.Request) {
6261

6362
data, err := json.Marshal(report)
6463
if err != nil {
65-
jb.log.Error("failed to marshal jobs state report", zap.Error(err))
64+
jb.log.Error("failed to marshal jobs state report", "error", err)
6665
return
6766
}
6867

6968
_, err = w.Write(data)
7069
if err != nil {
71-
jb.log.Error("failed to write jobs state report", zap.Error(err))
70+
jb.log.Error("failed to write jobs state report", "error", err)
7271
}
7372
}

plugin.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package status
33
import (
44
"context"
55
stderr "errors"
6+
"log/slog"
67
"net/http"
78
"sync"
89
"sync/atomic"
@@ -12,7 +13,6 @@ import (
1213
"github.com/roadrunner-server/api-plugins/v6/status"
1314
"github.com/roadrunner-server/endure/v2/dep"
1415
"github.com/roadrunner-server/errors"
15-
"go.uber.org/zap"
1616
)
1717

1818
const (
@@ -29,7 +29,7 @@ type Configurer interface {
2929
}
3030

3131
type Logger interface {
32-
NamedLogger(name string) *zap.Logger
32+
NamedLogger(name string) *slog.Logger
3333
}
3434

3535
// Checker interface used to get the latest status from the plugin
@@ -62,7 +62,7 @@ type Plugin struct {
6262
// shared pointer
6363
shutdownInitiated atomic.Pointer[bool]
6464
server *http.Server
65-
log *zap.Logger
65+
log *slog.Logger
6666
cfg *Config
6767
}
6868

ready.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,22 @@ package status
22

33
import (
44
"encoding/json"
5+
"log/slog"
56
"net/http"
67
"sync/atomic"
7-
8-
"go.uber.org/zap"
98
)
109

1110
// readiness Handler return 200OK if all Plugins are ready to serve
1211
// if one of the Plugins returns status from the 5xx range, the status for all queries will be 503
1312

1413
type Ready struct {
15-
log *zap.Logger
14+
log *slog.Logger
1615
unavailableStatusCode int
1716
statusRegistry map[string]Readiness
1817
shutdownInitiated *atomic.Pointer[bool]
1918
}
2019

21-
func NewReadyHandler(sr map[string]Readiness, shutdownInitiated *atomic.Pointer[bool], log *zap.Logger, usc int) *Ready {
20+
func NewReadyHandler(sr map[string]Readiness, shutdownInitiated *atomic.Pointer[bool], log *slog.Logger, usc int) *Ready {
2221
return &Ready{
2322
log: log,
2423
statusRegistry: sr,
@@ -47,7 +46,7 @@ func (rd *Ready) ServeHTTP(w http.ResponseWriter, r *http.Request) {
4746
StatusCode: http.StatusNotFound,
4847
})
4948

50-
rd.log.Info("plugin is nil or not initialized", zap.String("plugin", k))
49+
rd.log.Info("plugin is nil or not initialized", "plugin", k)
5150
continue
5251
}
5352

@@ -98,14 +97,14 @@ func (rd *Ready) ServeHTTP(w http.ResponseWriter, r *http.Request) {
9897
data, err := json.Marshal(report)
9998
if err != nil {
10099
// TODO do we need to write this error to the ResponseWriter?
101-
rd.log.Error("failed to marshal response", zap.Error(err))
100+
rd.log.Error("failed to marshal response", "error", err)
102101
return
103102
}
104103

105104
// write the response
106105
_, err = w.Write(data)
107106
if err != nil {
108-
rd.log.Error("failed to write response", zap.Error(err))
107+
rd.log.Error("failed to write response", "error", err)
109108
}
110109

111110
return
@@ -160,18 +159,18 @@ func (rd *Ready) ServeHTTP(w http.ResponseWriter, r *http.Request) {
160159
})
161160
}
162161
} else {
163-
rd.log.Info("plugin does not support readiness checks", zap.String("plugin", plg[i]))
162+
rd.log.Info("plugin does not support readiness checks", "plugin", plg[i])
164163
}
165164
}
166165

167166
data, err := json.Marshal(report)
168167
if err != nil {
169-
rd.log.Error("failed to marshal response", zap.Error(err))
168+
rd.log.Error("failed to marshal response", "error", err)
170169
}
171170

172171
// write the response
173172
_, err = w.Write(data)
174173
if err != nil {
175-
rd.log.Error("failed to write response", zap.Error(err))
174+
rd.log.Error("failed to write response", "error", err)
176175
}
177176
}

rpc.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ package status
33
import (
44
statusV2 "github.com/roadrunner-server/api-go/v6/status/v2"
55
"github.com/roadrunner-server/errors"
6-
"go.uber.org/zap"
6+
"log/slog"
77
)
88

99
type rpc struct {
1010
srv *Plugin
11-
log *zap.Logger
11+
log *slog.Logger
1212
}
1313

1414
// Status returns the current status of the provided plugin
1515
func (rpc *rpc) Status(req *statusV2.StatusRequest, resp *statusV2.StatusResponse) error {
1616
const op = errors.Op("checker_rpc_status")
17-
rpc.log.Debug("Status method was invoked", zap.String("plugin", req.GetPlugin()))
17+
rpc.log.Debug("Status method was invoked", "plugin", req.GetPlugin())
1818
st, err := rpc.srv.status(req.GetPlugin())
1919
if err != nil {
2020
resp.Message = err.Error()
@@ -23,7 +23,7 @@ func (rpc *rpc) Status(req *statusV2.StatusRequest, resp *statusV2.StatusRespons
2323

2424
if st != nil {
2525
resp.Code = int64(st.Code)
26-
rpc.log.Debug("status code", zap.Int("code", st.Code))
26+
rpc.log.Debug("status code", "code", st.Code)
2727
}
2828

2929
rpc.log.Debug("successfully finished the Status method")
@@ -33,7 +33,7 @@ func (rpc *rpc) Status(req *statusV2.StatusRequest, resp *statusV2.StatusRespons
3333
// Ready to return the readiness check of the provided plugin
3434
func (rpc *rpc) Ready(req *statusV2.StatusRequest, resp *statusV2.StatusResponse) error {
3535
const op = errors.Op("checker_rpc_ready")
36-
rpc.log.Debug("Ready method was invoked", zap.String("plugin", req.GetPlugin()))
36+
rpc.log.Debug("Ready method was invoked", "plugin", req.GetPlugin())
3737
st, err := rpc.srv.ready(req.GetPlugin())
3838
if err != nil {
3939
resp.Message = err.Error()
@@ -42,7 +42,7 @@ func (rpc *rpc) Ready(req *statusV2.StatusRequest, resp *statusV2.StatusResponse
4242

4343
if st != nil {
4444
resp.Code = int64(st.Code)
45-
rpc.log.Debug("status code", zap.Int("code", st.Code))
45+
rpc.log.Debug("status code", "code", st.Code)
4646
}
4747

4848
rpc.log.Debug("successfully finished the Ready method")

0 commit comments

Comments
 (0)