@marcel91200 / caddy Mirror · read-only / commits / e43b6d81
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 13, 11:45 UTC

core: Variadic Context.Logger(); soft deprecation

Ideally I'd just remove the parameter to caddy.Context.Logger(), but this would break most Caddy plugins. Instead, I'm making it variadic and marking it as partially deprecated. In the future, I might completely remove the parameter once most plugins have updated.

Matthew Holt committed Sep 16, 2022 at 16:55 UTC e43b6d81782ef79f22058179d8793f40cea89556
27 files changed +71 -67
caddyconfig/httploader.go
+2 -2
@@ -113,7 +113,7 @@ func (hl HTTPLoader) LoadConfig(ctx caddy.Context) ([]byte, error) {
113 return nil, err
114 }
115 for _, warn := range warnings {
116 - ctx.Logger(hl).Warn(warn.String())
116 + ctx.Logger().Warn(warn.String())
117 }
118
119 return result, nil
@@ -129,7 +129,7 @@ func (hl HTTPLoader) makeClient(ctx caddy.Context) (*http.Client, error) {
129
130 // client authentication
131 if hl.TLS.UseServerIdentity {
132 - certs, err := ctx.IdentityCredentials(ctx.Logger(hl))
132 + certs, err := ctx.IdentityCredentials(ctx.Logger())
133 if err != nil {
134 return nil, fmt.Errorf("getting server identity credentials: %v", err)
135 }
context.go
+25 -18
@@ -442,10 +442,27 @@ func (ctx Context) Storage() certmagic.Storage {
442 return ctx.cfg.storage
443 }
444
445 -// TODO: aw man, can I please change this?
446 -// Logger returns a logger that can be used by mod.
447 -func (ctx Context) Logger(mod Module) *zap.Logger {
448 - // TODO: if mod is nil, use ctx.Module() instead...
445 +// Logger returns a logger that is intended for use by the most
446 +// recent module associated with the context. Callers should not
447 +// pass in any arguments unless they want to associate with a
448 +// different module; it panics if more than 1 value is passed in.
449 +//
450 +// Originally, this method's signature was `Logger(mod Module)`,
451 +// requiring that an instance of a Caddy module be passsed in.
452 +// However, that is no longer necessary, as the closest module
453 +// most recently associated with the context will be automatically
454 +// assumed. To prevent a sudden breaking change, this method's
455 +// signature has been changed to be variadic, but we may remove
456 +// the parameter altogether in the future. Callers should not
457 +// pass in any argument. If there is valid need to specify a
458 +// different module, please open an issue to discuss.
459 +//
460 +// PARTIALLY DEPRECATED: The Logger(module) form is deprecated and
461 +// may be removed in the future. Do not pass in any arguments.
462 +func (ctx Context) Logger(module ...Module) *zap.Logger {
463 + if len(module) > 1 {
464 + panic("more than 1 module passed in")
465 + }
466 if ctx.cfg == nil {
467 // often the case in tests; just use a dev logger
468 l, err := zap.NewDevelopment()
@@ -454,23 +471,13 @@ func (ctx Context) Logger(mod Module) *zap.Logger {
471 }
472 return l
473 }
474 + mod := ctx.Module()
475 + if len(module) > 0 {
476 + mod = module[0]
477 + }
478 return ctx.cfg.Logging.Logger(mod)
479 }
480
460 -// TODO: use this
461 -// // Logger returns a logger that can be used by the current module.
462 -// func (ctx Context) Log() *zap.Logger {
463 -// if ctx.cfg == nil {
464 -// // often the case in tests; just use a dev logger
465 -// l, err := zap.NewDevelopment()
466 -// if err != nil {
467 -// panic("config missing, unable to create dev logger: " + err.Error())
468 -// }
469 -// return l
470 -// }
471 -// return ctx.cfg.Logging.Logger(ctx.Module())
472 -// }
473 -
481 // Modules returns the lineage of modules that this context provisioned,
482 // with the most recent/current module being last in the list.
483 func (ctx Context) Modules() []Module {
modules/caddyevents/app.go
+1 -1
@@ -119,7 +119,7 @@ func (App) CaddyModule() caddy.ModuleInfo {
119
120 // Provision sets up the app.
121 func (app *App) Provision(ctx caddy.Context) error {
122 - app.logger = ctx.Logger(app)
122 + app.logger = ctx.Logger()
123 app.subscriptions = make(map[string]map[caddy.ModuleID][]Handler)
124
125 for _, sub := range app.Subscriptions {
modules/caddyhttp/app.go
+1 -1
@@ -160,7 +160,7 @@ func (app *App) Provision(ctx caddy.Context) error {
160 }
161 app.tlsApp = tlsAppIface.(*caddytls.TLS)
162 app.ctx = ctx
163 - app.logger = ctx.Logger(app)
163 + app.logger = ctx.Logger()
164
165 eventsAppIface, err := ctx.App("events")
166 if err != nil {
modules/caddyhttp/caddyauth/caddyauth.go
+1 -1
@@ -56,7 +56,7 @@ func (Authentication) CaddyModule() caddy.ModuleInfo {
56
57 // Provision sets up a.
58 func (a *Authentication) Provision(ctx caddy.Context) error {
59 - a.logger = ctx.Logger(a)
59 + a.logger = ctx.Logger()
60 a.Providers = make(map[string]Authenticator)
61 mods, err := ctx.LoadModule(a, "ProvidersRaw")
62 if err != nil {
modules/caddyhttp/caddyauth/hashes.go
+1 -1
@@ -92,7 +92,7 @@ func (ScryptHash) CaddyModule() caddy.ModuleInfo {
92 // Provision sets up s.
93 func (s *ScryptHash) Provision(ctx caddy.Context) error {
94 s.SetDefaults()
95 - ctx.Logger(s).Warn("use of 'scrypt' is deprecated, please use 'bcrypt' instead")
95 + ctx.Logger().Warn("use of 'scrypt' is deprecated, please use 'bcrypt' instead")
96 return nil
97 }
98
modules/caddyhttp/celmatcher.go
+8 -8
@@ -90,7 +90,7 @@ func (m *MatchExpression) UnmarshalJSON(data []byte) error {
90
91 // Provision sets ups m.
92 func (m *MatchExpression) Provision(ctx caddy.Context) error {
93 - m.log = ctx.Logger(m)
93 + m.log = ctx.Logger()
94
95 // replace placeholders with a function call - this is just some
96 // light (and possibly naïve) syntactic sugar
@@ -294,13 +294,13 @@ type CELLibraryProducer interface {
294 // CELMatcherImpl creates a new cel.Library based on the following pieces of
295 // data:
296 //
297 -// - macroName: the function name to be used within CEL. This will be a macro
298 -// and not a function proper.
299 -// - funcName: the function overload name generated by the CEL macro used to
300 -// represent the matcher.
301 -// - matcherDataTypes: the argument types to the macro.
302 -// - fac: a matcherFactory implementation which converts from CEL constant
303 -// values to a Matcher instance.
297 +// - macroName: the function name to be used within CEL. This will be a macro
298 +// and not a function proper.
299 +// - funcName: the function overload name generated by the CEL macro used to
300 +// represent the matcher.
301 +// - matcherDataTypes: the argument types to the macro.
302 +// - fac: a matcherFactory implementation which converts from CEL constant
303 +// values to a Matcher instance.
304 //
305 // Note, macro names and function names must not collide with other macros or
306 // functions exposed within CEL expressions, or an error will be produced
modules/caddyhttp/fileserver/matcher.go
+1 -1
@@ -256,7 +256,7 @@ func celFileMatcherMacroExpander() parser.MacroExpander {
256
257 // Provision sets up m's defaults.
258 func (m *MatchFile) Provision(ctx caddy.Context) error {
259 - m.logger = ctx.Logger(m)
259 + m.logger = ctx.Logger()
260
261 // establish the file system to use
262 if len(m.FileSystemRaw) > 0 {
modules/caddyhttp/fileserver/staticfiles.go
+1 -1
@@ -167,7 +167,7 @@ func (FileServer) CaddyModule() caddy.ModuleInfo {
167
168 // Provision sets up the static files responder.
169 func (fsrv *FileServer) Provision(ctx caddy.Context) error {
170 - fsrv.logger = ctx.Logger(fsrv)
170 + fsrv.logger = ctx.Logger()
171
172 // establish which file system (possibly a virtual one) we'll be using
173 if len(fsrv.FileSystemRaw) > 0 {
modules/caddyhttp/matchers.go
+1 -1
@@ -1326,7 +1326,7 @@ func (MatchRemoteIP) CELLibrary(ctx caddy.Context) (cel.Library, error) {
1326
1327 // Provision parses m's IP ranges, either from IP or CIDR expressions.
1328 func (m *MatchRemoteIP) Provision(ctx caddy.Context) error {
1329 - m.logger = ctx.Logger(m)
1329 + m.logger = ctx.Logger()
1330 for _, str := range m.Ranges {
1331 // Exclude the zone_id from the IP
1332 if strings.Contains(str, "%") {
modules/caddyhttp/push/handler.go
+1 -1
@@ -61,7 +61,7 @@ func (Handler) CaddyModule() caddy.ModuleInfo {
61
62 // Provision sets up h.
63 func (h *Handler) Provision(ctx caddy.Context) error {
64 - h.logger = ctx.Logger(h)
64 + h.logger = ctx.Logger()
65 if h.Headers != nil {
66 err := h.Headers.Provision(ctx)
67 if err != nil {
modules/caddyhttp/reverseproxy/fastcgi/fastcgi.go
+1 -1
@@ -94,7 +94,7 @@ func (Transport) CaddyModule() caddy.ModuleInfo {
94
95 // Provision sets up t.
96 func (t *Transport) Provision(ctx caddy.Context) error {
97 - t.logger = ctx.Logger(t)
97 + t.logger = ctx.Logger()
98
99 if t.Root == "" {
100 t.Root = "{http.vars.root}"
modules/caddyhttp/reverseproxy/httptransport.go
+1 -1
@@ -195,7 +195,7 @@ func (h *HTTPTransport) NewTransport(caddyCtx caddy.Context) (*http.Transport, e
195 TCPConn: tcpConn,
196 readTimeout: time.Duration(h.ReadTimeout),
197 writeTimeout: time.Duration(h.WriteTimeout),
198 - logger: caddyCtx.Logger(h),
198 + logger: caddyCtx.Logger(),
199 }
200 }
201
modules/caddyhttp/reverseproxy/reverseproxy.go
+1 -1
@@ -217,7 +217,7 @@ func (h *Handler) Provision(ctx caddy.Context) error {
217 }
218 h.events = eventAppIface.(*caddyevents.App)
219 h.ctx = ctx
220 - h.logger = ctx.Logger(h)
220 + h.logger = ctx.Logger()
221 h.connections = make(map[io.ReadWriteCloser]openConnection)
222 h.connectionsMu = new(sync.Mutex)
223
modules/caddyhttp/reverseproxy/upstreams.go
+2 -2
@@ -76,7 +76,7 @@ func (SRVUpstreams) CaddyModule() caddy.ModuleInfo {
76 }
77
78 func (su *SRVUpstreams) Provision(ctx caddy.Context) error {
79 - su.logger = ctx.Logger(su)
79 + su.logger = ctx.Logger()
80 if su.Refresh == 0 {
81 su.Refresh = caddy.Duration(time.Minute)
82 }
@@ -383,7 +383,7 @@ func (MultiUpstreams) CaddyModule() caddy.ModuleInfo {
383 }
384
385 func (mu *MultiUpstreams) Provision(ctx caddy.Context) error {
386 - mu.logger = ctx.Logger(mu)
386 + mu.logger = ctx.Logger()
387
388 if mu.SourcesRaw != nil {
389 mod, err := ctx.LoadModule(mu, "SourcesRaw")
modules/caddyhttp/rewrite/rewrite.go
+1 -1
@@ -101,7 +101,7 @@ func (Rewrite) CaddyModule() caddy.ModuleInfo {
101
102 // Provision sets up rewr.
103 func (rewr *Rewrite) Provision(ctx caddy.Context) error {
104 - rewr.logger = ctx.Logger(rewr)
104 + rewr.logger = ctx.Logger()
105
106 for i, rep := range rewr.PathRegexp {
107 if rep.Find == "" {
modules/caddyhttp/tracing/module.go
+4 -5
@@ -42,7 +42,7 @@ func (Tracing) CaddyModule() caddy.ModuleInfo {
42
43 // Provision implements caddy.Provisioner.
44 func (ot *Tracing) Provision(ctx caddy.Context) error {
45 - ot.logger = ctx.Logger(ot)
45 + ot.logger = ctx.Logger()
46
47 var err error
48 ot.otel, err = newOpenTelemetryWrapper(ctx, ot.SpanName)
@@ -66,10 +66,9 @@ func (ot *Tracing) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyh
66
67 // UnmarshalCaddyfile sets up the module from Caddyfile tokens. Syntax:
68 //
69 -// tracing {
70 -// [span <span_name>]
71 -// }
72 -//
69 +// tracing {
70 +// [span <span_name>]
71 +// }
72 func (ot *Tracing) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
73 setParameter := func(d *caddyfile.Dispenser, val *string) error {
74 if d.NextArg() {
modules/caddypki/acmeserver/acmeserver.go
+1 -1
@@ -87,7 +87,7 @@ func (Handler) CaddyModule() caddy.ModuleInfo {
87
88 // Provision sets up the ACME server handler.
89 func (ash *Handler) Provision(ctx caddy.Context) error {
90 - ash.logger = ctx.Logger(ash)
90 + ash.logger = ctx.Logger()
91 // set some defaults
92 if ash.CA == "" {
93 ash.CA = caddypki.DefaultCAID
modules/caddypki/adminapi.go
+1 -1
@@ -47,7 +47,7 @@ func (adminAPI) CaddyModule() caddy.ModuleInfo {
47 // Provision sets up the adminAPI module.
48 func (a *adminAPI) Provision(ctx caddy.Context) error {
49 a.ctx = ctx
50 - a.log = ctx.Logger(a)
50 + a.log = ctx.Logger(a) // TODO: passing in 'a' is a hack until the admin API is officially extensible (see #5032)
51
52 // First check if the PKI app was configured, because
53 // a.ctx.App() has the side effect of instantiating
modules/caddypki/pki.go
+1 -1
@@ -54,7 +54,7 @@ func (PKI) CaddyModule() caddy.ModuleInfo {
54 // Provision sets up the configuration for the PKI app.
55 func (p *PKI) Provision(ctx caddy.Context) error {
56 p.ctx = ctx
57 - p.log = ctx.Logger(p)
57 + p.log = ctx.Logger()
58
59 for caID, ca := range p.CAs {
60 err := ca.Provision(ctx, caID, p.log)
modules/caddytls/acmeissuer.go
+1 -1
@@ -103,7 +103,7 @@ func (ACMEIssuer) CaddyModule() caddy.ModuleInfo {
103
104 // Provision sets up iss.
105 func (iss *ACMEIssuer) Provision(ctx caddy.Context) error {
106 - iss.logger = ctx.Logger(iss)
106 + iss.logger = ctx.Logger()
107
108 repl := caddy.NewReplacer()
109
modules/caddytls/certmanagers.go
+1 -1
@@ -43,7 +43,7 @@ func (Tailscale) CaddyModule() caddy.ModuleInfo {
43 }
44
45 func (ts *Tailscale) Provision(ctx caddy.Context) error {
46 - ts.logger = ctx.Logger(ts)
46 + ts.logger = ctx.Logger()
47 return nil
48 }
49
modules/caddytls/internalissuer.go
+6 -7
@@ -65,7 +65,7 @@ func (InternalIssuer) CaddyModule() caddy.ModuleInfo {
65
66 // Provision sets up the issuer.
67 func (iss *InternalIssuer) Provision(ctx caddy.Context) error {
68 - iss.logger = ctx.Logger(iss)
68 + iss.logger = ctx.Logger()
69
70 // set some defaults
71 if iss.CA == "" {
@@ -148,12 +148,11 @@ func (iss InternalIssuer) Issue(ctx context.Context, csr *x509.CertificateReques
148
149 // UnmarshalCaddyfile deserializes Caddyfile tokens into iss.
150 //
151 -// ... internal {
152 -// ca <name>
153 -// lifetime <duration>
154 -// sign_with_root
155 -// }
156 -//
151 +// ... internal {
152 +// ca <name>
153 +// lifetime <duration>
154 +// sign_with_root
155 +// }
156 func (iss *InternalIssuer) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
157 for d.Next() {
158 for d.NextBlock(0) {
modules/caddytls/matchers.go
+1 -1
@@ -81,7 +81,7 @@ func (MatchRemoteIP) CaddyModule() caddy.ModuleInfo {
81
82 // Provision parses m's IP ranges, either from IP or CIDR expressions.
83 func (m *MatchRemoteIP) Provision(ctx caddy.Context) error {
84 - m.logger = ctx.Logger(m)
84 + m.logger = ctx.Logger()
85 for _, str := range m.Ranges {
86 cidrs, err := m.parseIPRange(str)
87 if err != nil {
modules/caddytls/tls.go
+1 -1
@@ -94,7 +94,7 @@ func (t *TLS) Provision(ctx caddy.Context) error {
94 }
95 t.events = eventsAppIface.(*caddyevents.App)
96 t.ctx = ctx
97 - t.logger = ctx.Logger(t)
97 + t.logger = ctx.Logger()
98 repl := caddy.NewReplacer()
99
100 // set up a new certificate cache; this (re)loads all certificates
modules/caddytls/zerosslissuer.go
+1 -1
@@ -66,7 +66,7 @@ func (*ZeroSSLIssuer) CaddyModule() caddy.ModuleInfo {
66
67 // Provision sets up iss.
68 func (iss *ZeroSSLIssuer) Provision(ctx caddy.Context) error {
69 - iss.logger = ctx.Logger(iss)
69 + iss.logger = ctx.Logger()
70 if iss.ACMEIssuer == nil {
71 iss.ACMEIssuer = new(ACMEIssuer)
72 }
modules/metrics/metrics.go
+4 -5
@@ -62,7 +62,7 @@ func (l *zapLogger) Println(v ...any) {
62
63 // Provision sets up m.
64 func (m *Metrics) Provision(ctx caddy.Context) error {
65 - log := ctx.Logger(m)
65 + log := ctx.Logger()
66 m.metricsHandler = createMetricsHandler(&zapLogger{log}, !m.DisableOpenMetrics)
67 return nil
68 }
@@ -75,10 +75,9 @@ func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error)
75
76 // UnmarshalCaddyfile sets up the handler from Caddyfile tokens. Syntax:
77 //
78 -// metrics [<matcher>] {
79 -// disable_openmetrics
80 -// }
81 -//
78 +// metrics [<matcher>] {
79 +// disable_openmetrics
80 +// }
81 func (m *Metrics) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
82 for d.Next() {
83 args := d.RemainingArgs()