From eda8b1cf478509dd655c93ae35ab78ecc9b15b74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Matczuk?= Date: Thu, 9 Feb 2023 15:47:16 +0100 Subject: [PATCH] log: ingest Logger interface This patch moves forwarder.Logger into log.Logger. It enables using logger in subpackages without a need to redefine it every time. --- .golangci.yml | 1 + cmd/forwarder/paceval/paceval.go | 3 ++- credentials.go | 6 ++++-- dns.go | 6 ++++-- http_proxy.go | 5 +++-- http_server.go | 7 ++++--- log.go => log/log.go | 2 +- pac.go | 8 ++++++-- 8 files changed, 25 insertions(+), 13 deletions(-) rename log.go => log/log.go (97%) diff --git a/.golangci.yml b/.golangci.yml index b3a5a6c7..e6f06d89 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -68,6 +68,7 @@ issues: - "can be `fmt.Stringer`" - "returns interface \\(github.com/dop251/goja\\.Value\\)" - "returns interface \\(github.com/google/martian/v3\\.(Request|Response)+Modifier\\)" + - "shadow of imported from 'github.com/saucelabs/forwarder/log' package 'log'" - "string `https?` has \\d+ occurrences" exclude-rules: diff --git a/cmd/forwarder/paceval/paceval.go b/cmd/forwarder/paceval/paceval.go index dd428dfb..bef8cd47 100644 --- a/cmd/forwarder/paceval/paceval.go +++ b/cmd/forwarder/paceval/paceval.go @@ -12,6 +12,7 @@ import ( "github.com/saucelabs/forwarder" "github.com/saucelabs/forwarder/bind" + "github.com/saucelabs/forwarder/log" "github.com/saucelabs/forwarder/pac" "github.com/spf13/cobra" ) @@ -25,7 +26,7 @@ type command struct { func (c *command) RunE(cmd *cobra.Command, args []string) error { var resolver *net.Resolver if len(c.dnsConfig.Servers) > 0 { - r, err := forwarder.NewResolver(c.dnsConfig, forwarder.NopLogger) + r, err := forwarder.NewResolver(c.dnsConfig, log.NopLogger) if err != nil { return err } diff --git a/credentials.go b/credentials.go index b0495a33..9b56e804 100644 --- a/credentials.go +++ b/credentials.go @@ -8,6 +8,8 @@ import ( "fmt" "net" "net/url" + + "github.com/saucelabs/forwarder/log" ) type HostPortUser struct { @@ -34,10 +36,10 @@ type CredentialsMatcher struct { host map[string]*url.Userinfo port map[string]*url.Userinfo global *url.Userinfo - log Logger + log log.Logger } -func NewCredentialsMatcher(credentials []*HostPortUser, log Logger) (*CredentialsMatcher, error) { +func NewCredentialsMatcher(credentials []*HostPortUser, log log.Logger) (*CredentialsMatcher, error) { if len(credentials) == 0 { return nil, nil //nolint:nilnil // nil is a valid value } diff --git a/dns.go b/dns.go index c0dbc6ed..4c589401 100644 --- a/dns.go +++ b/dns.go @@ -10,6 +10,8 @@ import ( "net" "net/url" "time" + + "github.com/saucelabs/forwarder/log" ) type DNSConfig struct { @@ -45,10 +47,10 @@ type resolver struct { resolver net.Resolver dialer net.Dialer servers []*url.URL - log Logger + log log.Logger } -func NewResolver(cfg *DNSConfig, log Logger) (*net.Resolver, error) { +func NewResolver(cfg *DNSConfig, log log.Logger) (*net.Resolver, error) { if err := cfg.Validate(); err != nil { return nil, err } diff --git a/http_proxy.go b/http_proxy.go index 2ff47eb8..71d43e38 100644 --- a/http_proxy.go +++ b/http_proxy.go @@ -21,6 +21,7 @@ import ( "github.com/google/martian/v3/fifo" "github.com/google/martian/v3/httpspec" "github.com/saucelabs/forwarder/httplog" + "github.com/saucelabs/forwarder/log" "github.com/saucelabs/forwarder/middleware" "github.com/saucelabs/forwarder/pac" ) @@ -103,14 +104,14 @@ type HTTPProxy struct { pac PACResolver creds *CredentialsMatcher transport http.RoundTripper - log Logger + log log.Logger proxy *martian.Proxy addr atomic.Pointer[string] TLSConfig *tls.Config } -func NewHTTPProxy(cfg *HTTPProxyConfig, pr PACResolver, cm *CredentialsMatcher, rt http.RoundTripper, log Logger) (*HTTPProxy, error) { +func NewHTTPProxy(cfg *HTTPProxyConfig, pr PACResolver, cm *CredentialsMatcher, rt http.RoundTripper, log log.Logger) (*HTTPProxy, error) { if err := cfg.Validate(); err != nil { return nil, err } diff --git a/http_server.go b/http_server.go index 42f3f2cf..1368de7a 100644 --- a/http_server.go +++ b/http_server.go @@ -18,6 +18,7 @@ import ( "github.com/prometheus/client_golang/prometheus" "github.com/saucelabs/forwarder/httplog" + "github.com/saucelabs/forwarder/log" "github.com/saucelabs/forwarder/middleware" ) @@ -124,12 +125,12 @@ func (c *HTTPServerConfig) loadCertificate(tlsCfg *tls.Config) error { type HTTPServer struct { config HTTPServerConfig - log Logger + log log.Logger srv *http.Server addr atomic.Pointer[string] } -func NewHTTPServer(cfg *HTTPServerConfig, h http.Handler, log Logger) (*HTTPServer, error) { +func NewHTTPServer(cfg *HTTPServerConfig, h http.Handler, log log.Logger) (*HTTPServer, error) { if err := cfg.Validate(); err != nil { return nil, err } @@ -162,7 +163,7 @@ func NewHTTPServer(cfg *HTTPServerConfig, h http.Handler, log Logger) (*HTTPServ return hs, nil } -func withMiddleware(cfg *HTTPServerConfig, log Logger, h http.Handler) http.Handler { +func withMiddleware(cfg *HTTPServerConfig, log log.Logger, h http.Handler) http.Handler { // Note that the order of execution is reversed. if cfg.BasicAuth != nil { p, _ := cfg.BasicAuth.Password() diff --git a/log.go b/log/log.go similarity index 97% rename from log.go rename to log/log.go index eaa20082..51ae8a7e 100644 --- a/log.go +++ b/log/log.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a MPL // license that can be found in the LICENSE file. -package forwarder +package log // Logger is the logger used by the forwarder package. type Logger interface { diff --git a/pac.go b/pac.go index 50c0f051..90baf5d1 100644 --- a/pac.go +++ b/pac.go @@ -4,7 +4,11 @@ package forwarder -import "net/url" +import ( + "net/url" + + "github.com/saucelabs/forwarder/log" +) type PACResolver interface { // FindProxyForURL calls FindProxyForURL or FindProxyForURLEx function in the PAC script. @@ -14,7 +18,7 @@ type PACResolver interface { type LoggingPACResolver struct { Resolver PACResolver - Logger Logger + Logger log.Logger } func (r *LoggingPACResolver) FindProxyForURL(u *url.URL, hostname string) (string, error) {