Skip to content

Commit 02868f0

Browse files
authored
Remove dependency on zap, bump to v3 (#30)
* Remove dependency on zap in acme package Signed-off-by: Jan Tytgat <[email protected]> * Update go.mod module name Signed-off-by: Jan Tytgat <[email protected]> * Update go.mod module name Signed-off-by: Jan Tytgat <[email protected]> * Remove zap references in acme Signed-off-by: Jan Tytgat <[email protected]> * Remove zap references in acmez Signed-off-by: Jan Tytgat <[email protected]> * Cleanup examples Signed-off-by: Jan Tytgat <[email protected]> * Tidy go.mod Signed-off-by: Jan Tytgat <[email protected]> * Remove zap from comments Signed-off-by: Jan Tytgat <[email protected]> --------- Signed-off-by: Jan Tytgat <[email protected]>
1 parent 527e47c commit 02868f0

File tree

18 files changed

+122
-165
lines changed

18 files changed

+122
-165
lines changed

acme/ari.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@ import (
2020
"encoding/asn1"
2121
"encoding/base64"
2222
"fmt"
23+
"log/slog"
2324
"math/rand"
2425
"net/http"
2526
"time"
26-
27-
"go.uber.org/zap"
2827
)
2928

3029
// ErrUnsupported is used to indicate lack of support by an ACME server.
@@ -131,7 +130,8 @@ func (c *Client) GetRenewalInfo(ctx context.Context, leafCert *x509.Certificate)
131130
}
132131

133132
if c.Logger != nil {
134-
c.Logger.Debug("getting renewal info", zap.Strings("names", leafCert.DNSNames))
133+
c.Logger.LogAttrs(ctx, slog.LevelDebug, "getting renewal info",
134+
slog.Any("names", leafCert.DNSNames))
135135
}
136136

137137
certID, err := ARIUniqueIdentifier(leafCert)
@@ -154,10 +154,10 @@ func (c *Client) GetRenewalInfo(ctx context.Context, leafCert *x509.Certificate)
154154
resp, err = c.httpReq(ctx, http.MethodGet, c.ariEndpoint(certID), nil, &ari)
155155
if err != nil {
156156
if c.Logger != nil {
157-
c.Logger.Warn("error getting ARI response",
158-
zap.Error(err),
159-
zap.Int("attempt", i),
160-
zap.Strings("names", leafCert.DNSNames))
157+
c.Logger.LogAttrs(ctx, slog.LevelWarn, "error getting ARI response",
158+
slog.Any("error", err),
159+
slog.Int("attempt", i),
160+
slog.Any("names", leafCert.DNSNames))
161161
}
162162
continue
163163
}
@@ -173,10 +173,10 @@ func (c *Client) GetRenewalInfo(ctx context.Context, leafCert *x509.Certificate)
173173
ari.SuggestedWindow.Start.Equal(ari.SuggestedWindow.End) ||
174174
(ari.SuggestedWindow.End.Unix()-ari.SuggestedWindow.Start.Unix()-1 <= 0) {
175175
if c.Logger != nil {
176-
c.Logger.Debug("invalid ARI window",
177-
zap.Time("start", ari.SuggestedWindow.Start),
178-
zap.Time("end", ari.SuggestedWindow.End),
179-
zap.Strings("names", leafCert.DNSNames))
176+
c.Logger.LogAttrs(ctx, slog.LevelDebug, "invalid ARI window",
177+
slog.Time("start", ari.SuggestedWindow.Start),
178+
slog.Time("end", ari.SuggestedWindow.End),
179+
slog.Any("names", leafCert.DNSNames))
180180
}
181181
continue
182182
}
@@ -193,7 +193,8 @@ func (c *Client) GetRenewalInfo(ctx context.Context, leafCert *x509.Certificate)
193193
// interval that the ACME server recommends." draft-ietf-acme-ari-03 §4.2
194194
raTime, err := retryAfterTime(resp)
195195
if err != nil && c.Logger != nil {
196-
c.Logger.Error("invalid Retry-After value", zap.Error(err))
196+
c.Logger.LogAttrs(ctx, slog.LevelError, "invalid Retry-After value",
197+
slog.Any("error", err))
197198
}
198199
if !raTime.IsZero() {
199200
ari.RetryAfter = &raTime
@@ -212,14 +213,13 @@ func (c *Client) GetRenewalInfo(ctx context.Context, leafCert *x509.Certificate)
212213
ari.SelectedTime = time.Unix(rand.Int63n(end-start)+start, 0).UTC()
213214

214215
if c.Logger != nil {
215-
c.Logger.Info("got renewal info",
216-
zap.Strings("names", leafCert.DNSNames),
217-
zap.Time("window_start", ari.SuggestedWindow.Start),
218-
zap.Time("window_end", ari.SuggestedWindow.End),
219-
zap.Time("selected_time", ari.SelectedTime),
220-
zap.Timep("recheck_after", ari.RetryAfter),
221-
zap.String("explanation_url", ari.ExplanationURL),
222-
)
216+
c.Logger.LogAttrs(ctx, slog.LevelInfo, "got renewal info",
217+
slog.Any("names", leafCert.DNSNames),
218+
slog.Time("window_start", ari.SuggestedWindow.Start),
219+
slog.Time("window_end", ari.SuggestedWindow.End),
220+
slog.Time("selected_time", ari.SelectedTime),
221+
slog.Time("recheck_after", *ari.RetryAfter),
222+
slog.String("explanation_url", ari.ExplanationURL))
223223
}
224224

225225
return ari, nil

acme/certificate.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ import (
2222
"encoding/base64"
2323
"encoding/pem"
2424
"fmt"
25+
"log/slog"
2526
"net/http"
26-
27-
"go.uber.org/zap"
2827
)
2928

3029
// Certificate represents a certificate chain, which we usually refer
@@ -121,7 +120,8 @@ func (c *Client) GetCertificateChain(ctx context.Context, account Account, certU
121120
}
122121
ari, err := c.GetRenewalInfo(ctx, leafCert)
123122
if err != nil && c.Logger != nil {
124-
c.Logger.Error("failed getting renewal information", zap.Error(err))
123+
c.Logger.LogAttrs(ctx, slog.LevelError, "failed getting renewal information",
124+
slog.Any("error", err))
125125
}
126126
certChain.RenewalInfo = &ari
127127
}

acme/client.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,10 @@ package acme
2929
import (
3030
"context"
3131
"fmt"
32+
"log/slog"
3233
"net/http"
3334
"sync"
3435
"time"
35-
36-
"go.uber.org/zap"
3736
)
3837

3938
// Client facilitates ACME client operations as defined by the spec.
@@ -71,7 +70,7 @@ type Client struct {
7170
PollTimeout time.Duration
7271

7372
// An optional logger. Default: no logs
74-
Logger *zap.Logger
73+
Logger *slog.Logger
7574

7675
mu sync.Mutex // protects all unexported fields
7776
dir Directory

acme/http.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,14 @@ import (
2222
"errors"
2323
"fmt"
2424
"io"
25+
"log/slog"
2526
"net/http"
2627
"regexp"
2728
"runtime"
2829
"strconv"
2930
"strings"
3031
"sync"
3132
"time"
32-
33-
"go.uber.org/zap"
3433
)
3534

3635
// httpPostJWS performs robust HTTP requests by JWS-encoding the JSON of input.
@@ -96,9 +95,9 @@ func (c *Client) httpPostJWS(ctx context.Context, privateKey crypto.Signer,
9695
if errors.As(err, &problem) {
9796
if problem.Type == ProblemTypeBadNonce {
9897
if c.Logger != nil {
99-
c.Logger.Debug("server rejected our nonce; retrying",
100-
zap.String("detail", problem.Detail),
101-
zap.Error(err))
98+
c.Logger.LogAttrs(ctx, slog.LevelDebug, "server rejected our nonce; retrying",
99+
slog.String("detail", problem.Detail),
100+
slog.Any("error", err))
102101
}
103102
continue
104103
}
@@ -176,9 +175,9 @@ func (c *Client) httpReq(ctx context.Context, method, endpoint string, joseJSONP
176175
if err != nil {
177176
if retry {
178177
if c.Logger != nil {
179-
c.Logger.Warn("HTTP request failed; retrying",
180-
zap.String("url", req.URL.String()),
181-
zap.Error(err))
178+
c.Logger.LogAttrs(ctx, slog.LevelWarn, "HTTP request failed; retrying",
179+
slog.String("url", req.URL.String()),
180+
slog.Any("error", err))
182181
}
183182
continue
184183
}
@@ -273,11 +272,11 @@ func (c *Client) doHTTPRequest(req *http.Request, buf *bytes.Buffer) (resp *http
273272

274273
if c.Logger != nil {
275274
c.Logger.Debug("http request",
276-
zap.String("method", req.Method),
277-
zap.String("url", req.URL.String()),
278-
zap.Reflect("headers", req.Header),
279-
zap.Reflect("response_headers", resp.Header),
280-
zap.Int("status_code", resp.StatusCode))
275+
slog.String("method", req.Method),
276+
slog.String("url", req.URL.String()),
277+
slog.Any("headers", req.Header),
278+
slog.Any("response_headers", resp.Header),
279+
slog.Int("status_code", resp.StatusCode))
281280
}
282281

283282
// "The server MUST include a Replay-Nonce header field

acme/order.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ import (
1919
"encoding/base64"
2020
"errors"
2121
"fmt"
22+
"log/slog"
2223
"time"
23-
24-
"go.uber.org/zap"
2524
)
2625

2726
// Order is an object that "represents a client's request for a certificate
@@ -127,9 +126,9 @@ func (c *Client) NewOrder(ctx context.Context, account Account, order Order) (Or
127126
return order, err
128127
}
129128
if c.Logger != nil {
130-
c.Logger.Debug("creating order",
131-
zap.String("account", account.Location),
132-
zap.Strings("identifiers", order.identifierValues()))
129+
c.Logger.LogAttrs(ctx, slog.LevelDebug, "creating order",
130+
slog.String("account", account.Location),
131+
slog.Any("identifiers", order.identifierValues()))
133132
}
134133
resp, err := c.httpPostJWS(ctx, account.PrivateKey, account.Location, c.dir.NewOrder, order, &order)
135134
if err != nil {

acme/problem.go

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ package acme
1616

1717
import (
1818
"fmt"
19-
20-
"go.uber.org/zap/zapcore"
19+
"log/slog"
2120
)
2221

2322
// Problem carries the details of an error from HTTP APIs as
@@ -92,15 +91,14 @@ func (p Problem) Error() string {
9291
return s
9392
}
9493

95-
// MarshalLogObject satisfies the zapcore.ObjectMarshaler interface.
96-
// This allows problems to be serialized by the zap logger.
97-
func (p Problem) MarshalLogObject(enc zapcore.ObjectEncoder) error {
98-
enc.AddString("type", p.Type)
99-
enc.AddString("title", p.Title)
100-
enc.AddString("detail", p.Detail)
101-
enc.AddString("instance", p.Instance)
102-
enc.AddArray("subproblems", loggableSubproblems(p.Subproblems))
103-
return nil
94+
func (p Problem) LogValue() slog.Value {
95+
return slog.GroupValue(
96+
slog.String("type", p.Type),
97+
slog.String("title", p.Title),
98+
slog.String("detail", p.Detail),
99+
slog.String("instance", p.Instance),
100+
slog.Any("subproblems", p.Subproblems),
101+
)
104102
}
105103

106104
// Subproblem describes a more specific error in a problem according to
@@ -115,24 +113,11 @@ type Subproblem struct {
115113
Identifier Identifier `json:"identifier,omitempty"`
116114
}
117115

118-
// MarshalLogObject satisfies the zapcore.ObjectMarshaler interface.
119-
// This allows subproblems to be serialized by the zap logger.
120-
func (sp Subproblem) MarshalLogObject(enc zapcore.ObjectEncoder) error {
121-
enc.AddString("identifier_type", sp.Identifier.Type)
122-
enc.AddString("identifier", sp.Identifier.Value)
123-
enc.AddObject("subproblem", sp.Problem)
124-
return nil
125-
}
126-
127-
type loggableSubproblems []Subproblem
128-
129-
// MarshalLogArray satisfies the zapcore.ArrayMarshaler interface.
130-
// This allows a list of subproblems to be serialized by the zap logger.
131-
func (ls loggableSubproblems) MarshalLogArray(enc zapcore.ArrayEncoder) error {
132-
for _, sp := range ls {
133-
enc.AppendObject(sp)
134-
}
135-
return nil
116+
func (sp Subproblem) LogValue() slog.Value {
117+
return slog.GroupValue(
118+
slog.String("identifier_type", sp.Identifier.Type),
119+
slog.String("identifier", sp.Identifier.Value),
120+
slog.Any("subproblem", sp.Problem))
136121
}
137122

138123
// Standard token values for the "type" field of problems, as defined

0 commit comments

Comments
 (0)