Skip to content

Commit

Permalink
log: ingest Logger interface
Browse files Browse the repository at this point in the history
This patch moves forwarder.Logger into log.Logger.
It enables using logger in subpackages without a need to redefine it every time.
  • Loading branch information
mmatczuk committed Feb 9, 2023
1 parent b6f0a5f commit eda8b1c
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 13 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion cmd/forwarder/paceval/paceval.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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
}
Expand Down
6 changes: 4 additions & 2 deletions credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"fmt"
"net"
"net/url"

"github.com/saucelabs/forwarder/log"
)

type HostPortUser struct {
Expand All @@ -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
}
Expand Down
6 changes: 4 additions & 2 deletions dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"net"
"net/url"
"time"

"github.com/saucelabs/forwarder/log"
)

type DNSConfig struct {
Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 3 additions & 2 deletions http_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}
Expand Down
7 changes: 4 additions & 3 deletions http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion log.go → log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 6 additions & 2 deletions pac.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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) {
Expand Down

0 comments on commit eda8b1c

Please sign in to comment.