From dd0bbdc7b4ea17a70f589c917b6051956eb6d7d6 Mon Sep 17 00:00:00 2001 From: yusing Date: Mon, 20 Jan 2025 17:42:54 +0800 Subject: [PATCH] fix logs not printing correctly, removed unneccessary loggers --- cmd/main.go | 7 ++- internal/autocert/logger.go | 5 -- internal/autocert/provider.go | 17 +++--- internal/autocert/setup.go | 5 +- internal/config/config.go | 59 +++++++++++-------- internal/docker/client.go | 2 +- internal/docker/container.go | 3 +- internal/docker/idlewatcher/watcher.go | 4 +- internal/docker/logger.go | 7 --- internal/entrypoint/entrypoint.go | 9 +-- internal/entrypoint/logger.go | 7 --- internal/logging/logging.go | 1 - internal/net/http/accesslog/access_logger.go | 6 +- internal/net/http/accesslog/file_logger.go | 3 +- internal/net/http/accesslog/formatter.go | 4 +- .../net/http/loadbalancer/loadbalancer.go | 3 +- internal/net/http/loadbalancer/logger.go | 5 -- .../net/http/middleware/cloudflare_real_ip.go | 5 +- .../net/http/middleware/custom_error_page.go | 11 ++-- .../http/middleware/errorpage/error_page.go | 10 ++-- .../net/http/middleware/errorpage/logger.go | 5 -- internal/net/http/middleware/logger.go | 5 -- internal/net/http/middleware/middlewares.go | 9 +-- internal/net/http/middleware/redirect_http.go | 3 +- .../http/reverseproxy/reverse_proxy_mod.go | 10 ++-- internal/route/http.go | 5 +- internal/route/logger.go | 5 -- internal/route/provider/docker.go | 3 +- internal/route/provider/file.go | 3 +- internal/route/provider/logger.go | 5 -- internal/route/stream.go | 3 +- internal/route/udp_forwarder.go | 11 ++-- internal/task/task.go | 11 ++-- internal/task/utils.go | 6 +- internal/utils/fs.go | 6 +- internal/watcher/directory_watcher.go | 7 ++- internal/watcher/docker_watcher.go | 5 +- internal/watcher/logger.go | 5 -- 38 files changed, 130 insertions(+), 150 deletions(-) delete mode 100644 internal/autocert/logger.go delete mode 100644 internal/docker/logger.go delete mode 100644 internal/entrypoint/logger.go delete mode 100644 internal/net/http/loadbalancer/logger.go delete mode 100644 internal/net/http/middleware/errorpage/logger.go delete mode 100644 internal/net/http/middleware/logger.go delete mode 100644 internal/route/logger.go delete mode 100644 internal/route/provider/logger.go delete mode 100644 internal/watcher/logger.go diff --git a/cmd/main.go b/cmd/main.go index 2ad2247..2946e93 100755 --- a/cmd/main.go +++ b/cmd/main.go @@ -16,6 +16,7 @@ import ( "github.com/yusing/go-proxy/internal/common" "github.com/yusing/go-proxy/internal/config" E "github.com/yusing/go-proxy/internal/error" + "github.com/yusing/go-proxy/internal/homepage" "github.com/yusing/go-proxy/internal/logging" "github.com/yusing/go-proxy/internal/net/http/middleware" "github.com/yusing/go-proxy/internal/route/routes/routequery" @@ -25,13 +26,15 @@ import ( var rawLogger = log.New(os.Stdout, "", 0) -func main() { +func init() { var out io.Writer = os.Stdout if common.EnableLogStreaming { out = io.MultiWriter(out, v1.MemLogger()) } logging.InitLogger(out) +} +func main() { args := common.GetArgs() switch args.Command { @@ -94,6 +97,8 @@ func main() { } middleware.LoadComposeFiles() + internal.InitIconListCache() + homepage.InitOverridesConfig() var cfg *config.Config var err E.Error diff --git a/internal/autocert/logger.go b/internal/autocert/logger.go deleted file mode 100644 index 9339fa7..0000000 --- a/internal/autocert/logger.go +++ /dev/null @@ -1,5 +0,0 @@ -package autocert - -import "github.com/yusing/go-proxy/internal/logging" - -var logger = logging.With().Str("module", "autocert").Logger() diff --git a/internal/autocert/provider.go b/internal/autocert/provider.go index deb010e..7110e2a 100644 --- a/internal/autocert/provider.go +++ b/internal/autocert/provider.go @@ -17,6 +17,7 @@ import ( "github.com/go-acme/lego/v4/registration" "github.com/yusing/go-proxy/internal/config/types" E "github.com/yusing/go-proxy/internal/error" + "github.com/yusing/go-proxy/internal/logging" "github.com/yusing/go-proxy/internal/task" U "github.com/yusing/go-proxy/internal/utils" "github.com/yusing/go-proxy/internal/utils/strutils" @@ -89,7 +90,7 @@ func (p *Provider) ObtainCert() E.Error { }) if err != nil { p.legoCert = nil - logger.Err(err).Msg("cert renew failed, fallback to obtain") + logging.Err(err).Msg("cert renew failed, fallback to obtain") } else { p.legoCert = cert } @@ -136,7 +137,7 @@ func (p *Provider) LoadCert() E.Error { p.tlsCert = &cert p.certExpiries = expiries - logger.Info().Msgf("next renewal in %v", strutils.FormatDuration(time.Until(p.ShouldRenewOn()))) + logging.Info().Msgf("next renewal in %v", strutils.FormatDuration(time.Until(p.ShouldRenewOn()))) return p.renewIfNeeded() } @@ -172,7 +173,7 @@ func (p *Provider) ScheduleRenewal(parent task.Parent) { continue } if err := p.renewIfNeeded(); err != nil { - E.LogWarn("cert renew failed", err, &logger) + E.LogWarn("cert renew failed", err) lastErrOn = time.Now() continue } @@ -215,7 +216,7 @@ func (p *Provider) registerACME() error { } if reg, err := p.client.Registration.ResolveAccountByKey(); err == nil { p.user.Registration = reg - logger.Info().Msg("reused acme registration from private key") + logging.Info().Msg("reused acme registration from private key") return nil } @@ -224,7 +225,7 @@ func (p *Provider) registerACME() error { return err } p.user.Registration = reg - logger.Info().Interface("reg", reg).Msg("acme registered") + logging.Info().Interface("reg", reg).Msg("acme registered") return nil } @@ -270,7 +271,7 @@ func (p *Provider) certState() CertState { sort.Strings(certDomains) if !reflect.DeepEqual(certDomains, wantedDomains) { - logger.Info().Msgf("cert domains mismatch: %v != %v", certDomains, p.cfg.Domains) + logging.Info().Msgf("cert domains mismatch: %v != %v", certDomains, p.cfg.Domains) return CertStateMismatch } @@ -284,9 +285,9 @@ func (p *Provider) renewIfNeeded() E.Error { switch p.certState() { case CertStateExpired: - logger.Info().Msg("certs expired, renewing") + logging.Info().Msg("certs expired, renewing") case CertStateMismatch: - logger.Info().Msg("cert domains mismatch with config, renewing") + logging.Info().Msg("cert domains mismatch with config, renewing") default: return nil } diff --git a/internal/autocert/setup.go b/internal/autocert/setup.go index 5f172da..82e58ad 100644 --- a/internal/autocert/setup.go +++ b/internal/autocert/setup.go @@ -4,6 +4,7 @@ import ( "os" E "github.com/yusing/go-proxy/internal/error" + "github.com/yusing/go-proxy/internal/logging" "github.com/yusing/go-proxy/internal/utils/strutils" ) @@ -12,14 +13,14 @@ func (p *Provider) Setup() (err E.Error) { if !err.Is(os.ErrNotExist) { // ignore if cert doesn't exist return err } - logger.Debug().Msg("obtaining cert due to error loading cert") + logging.Debug().Msg("obtaining cert due to error loading cert") if err = p.ObtainCert(); err != nil { return err } } for _, expiry := range p.GetExpiries() { - logger.Info().Msg("certificate expire on " + strutils.FormatTime(expiry)) + logging.Info().Msg("certificate expire on " + strutils.FormatTime(expiry)) break } diff --git a/internal/config/config.go b/internal/config/config.go index 66fc0b2..94e5cc8 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -38,7 +38,6 @@ type Config struct { var ( instance *Config cfgWatcher watcher.Watcher - logger = logging.With().Str("module", "config").Logger() reloadMu sync.Mutex ) @@ -86,7 +85,7 @@ func WatchChanges() { configEventFlushInterval, OnConfigChange, func(err E.Error) { - E.LogError("config reload error", err, &logger) + E.LogError("config reload error", err) }, ) eventQueue.Start(cfgWatcher.Events(t.Context())) @@ -97,15 +96,15 @@ func OnConfigChange(ev []events.Event) { // just reload once and check the last event switch ev[len(ev)-1].Action { case events.ActionFileRenamed: - logger.Warn().Msg(cfgRenameWarn) + logging.Warn().Msg(cfgRenameWarn) return case events.ActionFileDeleted: - logger.Warn().Msg(cfgDeleteWarn) + logging.Warn().Msg(cfgDeleteWarn) return } if err := Reload(); err != nil { - logger.Warn().Msg("using last config") + logging.Warn().Msg("using last config") // recovered in event queue panic(err) } @@ -178,25 +177,37 @@ func (cfg *Config) StartProxyProviders() { }) if err := E.Join(errs...); err != nil { - E.LogError("route provider errors", err, &logger) + E.LogError("route provider errors", err) } } -func (cfg *Config) StartServers() { - server.StartServer(cfg.task, server.Options{ - Name: "proxy", - CertProvider: cfg.AutoCertProvider(), - HTTPAddr: common.ProxyHTTPAddr, - HTTPSAddr: common.ProxyHTTPSAddr, - Handler: cfg.entrypoint, - }) - server.StartServer(cfg.task, server.Options{ - Name: "api", - CertProvider: cfg.AutoCertProvider(), - HTTPAddr: common.APIHTTPAddr, - Handler: api.NewHandler(cfg), - }) - if common.PrometheusEnabled { +type StartServersOptions struct { + Proxy, API, Metrics bool +} + +func (cfg *Config) StartServers(opts ...*StartServersOptions) { + if len(opts) == 0 { + opts = append(opts, &StartServersOptions{Proxy: true, API: true, Metrics: true}) + } + opt := opts[0] + if opt.Proxy { + server.StartServer(cfg.task, server.Options{ + Name: "proxy", + CertProvider: cfg.AutoCertProvider(), + HTTPAddr: common.ProxyHTTPAddr, + HTTPSAddr: common.ProxyHTTPSAddr, + Handler: cfg.entrypoint, + }) + } + if opt.API { + server.StartServer(cfg.task, server.Options{ + Name: "api", + CertProvider: cfg.AutoCertProvider(), + HTTPAddr: common.APIHTTPAddr, + Handler: api.NewHandler(cfg), + }) + } + if opt.Metrics && common.PrometheusEnabled { server.StartServer(cfg.task, server.Options{ Name: "metrics", CertProvider: cfg.AutoCertProvider(), @@ -211,12 +222,12 @@ func (cfg *Config) load() E.Error { data, err := os.ReadFile(common.ConfigPath) if err != nil { - E.LogFatal(errMsg, err, &logger) + E.LogFatal(errMsg, err) } model := types.DefaultConfig() if err := utils.DeserializeYAML(data, model); err != nil { - E.LogFatal(errMsg, err, &logger) + E.LogFatal(errMsg, err) } // errors are non fatal below @@ -296,6 +307,6 @@ func (cfg *Config) loadRouteProviders(providers *types.Providers) E.Error { } results.Addf("%-"+strconv.Itoa(lenLongestName)+"s %d routes", p.String(), p.NumRoutes()) }) - logger.Info().Msg(results.String()) + logging.Info().Msg(results.String()) return errs.Error() } diff --git a/internal/docker/client.go b/internal/docker/client.go index a11cf64..a8ba759 100644 --- a/internal/docker/client.go +++ b/internal/docker/client.go @@ -119,7 +119,7 @@ func ConnectClient(host string) (*SharedClient, error) { Client: client, key: host, refCount: U.NewRefCounter(), - l: logger.With().Str("address", client.DaemonHost()).Logger(), + l: logging.With().Str("address", client.DaemonHost()).Logger(), } c.l.Trace().Msg("client connected") diff --git a/internal/docker/container.go b/internal/docker/container.go index 01e0518..9b985ae 100644 --- a/internal/docker/container.go +++ b/internal/docker/container.go @@ -6,6 +6,7 @@ import ( "strings" "github.com/docker/docker/api/types" + "github.com/yusing/go-proxy/internal/logging" U "github.com/yusing/go-proxy/internal/utils" "github.com/yusing/go-proxy/internal/utils/strutils" ) @@ -128,7 +129,7 @@ func (c *Container) setPublicIP() { } url, err := url.Parse(c.DockerHost) if err != nil { - logger.Err(err).Msgf("invalid docker host %q, falling back to 127.0.0.1", c.DockerHost) + logging.Err(err).Msgf("invalid docker host %q, falling back to 127.0.0.1", c.DockerHost) c.PublicIP = "127.0.0.1" return } diff --git a/internal/docker/idlewatcher/watcher.go b/internal/docker/idlewatcher/watcher.go index 878e4c9..16cdf0f 100644 --- a/internal/docker/idlewatcher/watcher.go +++ b/internal/docker/idlewatcher/watcher.go @@ -46,8 +46,6 @@ var ( watcherMapMu sync.Mutex errShouldNotReachHere = errors.New("should not reach here") - - logger = logging.With().Str("module", "idle_watcher").Logger() ) const dockerReqTimeout = 3 * time.Second @@ -78,7 +76,7 @@ func registerWatcher(watcherTask *task.Task, entry route.Entry, waker *waker) (* } w := &Watcher{ - Logger: logger.With().Str("name", cfg.ContainerName).Logger(), + Logger: logging.With().Str("name", cfg.ContainerName).Logger(), Config: cfg, waker: waker, client: client, diff --git a/internal/docker/logger.go b/internal/docker/logger.go deleted file mode 100644 index d27f2cb..0000000 --- a/internal/docker/logger.go +++ /dev/null @@ -1,7 +0,0 @@ -package docker - -import ( - "github.com/yusing/go-proxy/internal/logging" -) - -var logger = logging.With().Str("module", "docker").Logger() diff --git a/internal/entrypoint/entrypoint.go b/internal/entrypoint/entrypoint.go index faed489..3ab6dac 100644 --- a/internal/entrypoint/entrypoint.go +++ b/internal/entrypoint/entrypoint.go @@ -6,6 +6,7 @@ import ( "net/http" "strings" + "github.com/yusing/go-proxy/internal/logging" gphttp "github.com/yusing/go-proxy/internal/net/http" "github.com/yusing/go-proxy/internal/net/http/accesslog" "github.com/yusing/go-proxy/internal/net/http/middleware" @@ -50,7 +51,7 @@ func (ep *Entrypoint) SetMiddlewares(mws []map[string]any) error { } ep.middleware = mid - logger.Debug().Msg("entrypoint middleware loaded") + logging.Debug().Msg("entrypoint middleware loaded") return nil } @@ -64,7 +65,7 @@ func (ep *Entrypoint) SetAccessLogger(parent task.Parent, cfg *accesslog.Config) if err != nil { return } - logger.Debug().Msg("entrypoint access logger created") + logging.Debug().Msg("entrypoint access logger created") return } @@ -89,7 +90,7 @@ func (ep *Entrypoint) ServeHTTP(w http.ResponseWriter, r *http.Request) { // Then scraper / scanners will know the subdomain is invalid. // With StatusNotFound, they won't know whether it's the path, or the subdomain that is invalid. if served := middleware.ServeStaticErrorPageFile(w, r); !served { - logger.Err(err). + logging.Err(err). Str("method", r.Method). Str("url", r.URL.String()). Str("remote", r.RemoteAddr). @@ -99,7 +100,7 @@ func (ep *Entrypoint) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNotFound) w.Header().Set("Content-Type", "text/html; charset=utf-8") if _, err := w.Write(errorPage); err != nil { - logger.Err(err).Msg("failed to write error page") + logging.Err(err).Msg("failed to write error page") } } else { http.Error(w, err.Error(), http.StatusNotFound) diff --git a/internal/entrypoint/logger.go b/internal/entrypoint/logger.go deleted file mode 100644 index f6ae78d..0000000 --- a/internal/entrypoint/logger.go +++ /dev/null @@ -1,7 +0,0 @@ -package entrypoint - -import ( - "github.com/yusing/go-proxy/internal/logging" -) - -var logger = logging.With().Str("module", "entrypoint").Logger() diff --git a/internal/logging/logging.go b/internal/logging/logging.go index 6ee9f64..477e391 100644 --- a/internal/logging/logging.go +++ b/internal/logging/logging.go @@ -27,7 +27,6 @@ func InitLogger(out io.Writer) { default: timeFmt = "01-02 15:04" level = zerolog.InfoLevel - exclude = []string{"module"} } prefixLength := len(timeFmt) + 5 // level takes 3 + 2 spaces diff --git a/internal/net/http/accesslog/access_logger.go b/internal/net/http/accesslog/access_logger.go index 6d493f8..5116193 100644 --- a/internal/net/http/accesslog/access_logger.go +++ b/internal/net/http/accesslog/access_logger.go @@ -43,8 +43,6 @@ type ( } ) -var logger = logging.With().Str("module", "accesslog").Logger() - func NewAccessLogger(parent task.Parent, io AccessLogIO, cfg *Config) *AccessLogger { l := &AccessLogger{ task: parent.Subtask("accesslog"), @@ -133,7 +131,7 @@ func (l *AccessLogger) Flush(force bool) { } func (l *AccessLogger) handleErr(err error) { - E.LogError("failed to write access log", err, &logger) + E.LogError("failed to write access log", err) } func (l *AccessLogger) start() { @@ -170,6 +168,6 @@ func (l *AccessLogger) write(data []byte) { if err != nil { l.handleErr(err) } else { - logger.Debug().Msg("access log flushed to " + l.io.Name()) + logging.Debug().Msg("access log flushed to " + l.io.Name()) } } diff --git a/internal/net/http/accesslog/file_logger.go b/internal/net/http/accesslog/file_logger.go index e2ba4bc..1b3ace1 100644 --- a/internal/net/http/accesslog/file_logger.go +++ b/internal/net/http/accesslog/file_logger.go @@ -6,6 +6,7 @@ import ( "path" "sync" + "github.com/yusing/go-proxy/internal/logging" "github.com/yusing/go-proxy/internal/task" "github.com/yusing/go-proxy/internal/utils" ) @@ -55,7 +56,7 @@ func (f *File) Close() error { } func (f *File) closeOnZero() { - defer logger.Debug(). + defer logging.Debug(). Str("path", f.path). Msg("access log closed") diff --git a/internal/net/http/accesslog/formatter.go b/internal/net/http/accesslog/formatter.go index 30bf80d..d0fcd68 100644 --- a/internal/net/http/accesslog/formatter.go +++ b/internal/net/http/accesslog/formatter.go @@ -8,6 +8,8 @@ import ( "net/url" "strconv" "time" + + "github.com/yusing/go-proxy/internal/logging" ) type ( @@ -137,6 +139,6 @@ func (f *JSONFormatter) Format(line *bytes.Buffer, req *http.Request, res *http. marshaller := json.NewEncoder(line) err := marshaller.Encode(entry) if err != nil { - logger.Err(err).Msg("failed to marshal json log") + logging.Err(err).Msg("failed to marshal json log") } } diff --git a/internal/net/http/loadbalancer/loadbalancer.go b/internal/net/http/loadbalancer/loadbalancer.go index c4b8d71..b453648 100644 --- a/internal/net/http/loadbalancer/loadbalancer.go +++ b/internal/net/http/loadbalancer/loadbalancer.go @@ -8,6 +8,7 @@ import ( "github.com/rs/zerolog" "github.com/yusing/go-proxy/internal/common" E "github.com/yusing/go-proxy/internal/error" + "github.com/yusing/go-proxy/internal/logging" "github.com/yusing/go-proxy/internal/net/http/loadbalancer/types" "github.com/yusing/go-proxy/internal/route/routes" "github.com/yusing/go-proxy/internal/task" @@ -46,7 +47,7 @@ func New(cfg *Config) *LoadBalancer { lb := &LoadBalancer{ Config: new(Config), pool: types.NewServerPool(), - l: logger.With().Str("name", cfg.Link).Logger(), + l: logging.With().Str("name", cfg.Link).Logger(), } lb.UpdateConfigIfNeeded(cfg) return lb diff --git a/internal/net/http/loadbalancer/logger.go b/internal/net/http/loadbalancer/logger.go deleted file mode 100644 index 30fac46..0000000 --- a/internal/net/http/loadbalancer/logger.go +++ /dev/null @@ -1,5 +0,0 @@ -package loadbalancer - -import "github.com/yusing/go-proxy/internal/logging" - -var logger = logging.With().Str("module", "load_balancer").Logger() diff --git a/internal/net/http/middleware/cloudflare_real_ip.go b/internal/net/http/middleware/cloudflare_real_ip.go index 1a78580..1a66181 100644 --- a/internal/net/http/middleware/cloudflare_real_ip.go +++ b/internal/net/http/middleware/cloudflare_real_ip.go @@ -30,7 +30,6 @@ const ( var ( cfCIDRsLastUpdate time.Time cfCIDRsMu sync.Mutex - cfCIDRsLogger = logger.With().Str("name", "CloudflareRealIP").Logger() ) var CloudflareRealIP = NewMiddleware[cloudflareRealIP]() @@ -87,7 +86,7 @@ func tryFetchCFCIDR() (cfCIDRs []*types.CIDR) { ) if err != nil { cfCIDRsLastUpdate = time.Now().Add(-cfCIDRsUpdateRetryInterval - cfCIDRsUpdateInterval) - cfCIDRsLogger.Err(err).Msg("failed to update cloudflare range, retry in " + strutils.FormatDuration(cfCIDRsUpdateRetryInterval)) + logging.Err(err).Msg("failed to update cloudflare range, retry in " + strutils.FormatDuration(cfCIDRsUpdateRetryInterval)) return nil } if len(cfCIDRs) == 0 { @@ -96,7 +95,7 @@ func tryFetchCFCIDR() (cfCIDRs []*types.CIDR) { } cfCIDRsLastUpdate = time.Now() - cfCIDRsLogger.Info().Msg("cloudflare CIDR range updated") + logging.Info().Msg("cloudflare CIDR range updated") return } diff --git a/internal/net/http/middleware/custom_error_page.go b/internal/net/http/middleware/custom_error_page.go index 9748980..730422f 100644 --- a/internal/net/http/middleware/custom_error_page.go +++ b/internal/net/http/middleware/custom_error_page.go @@ -8,6 +8,7 @@ import ( "strconv" "strings" + "github.com/yusing/go-proxy/internal/logging" gphttp "github.com/yusing/go-proxy/internal/net/http" "github.com/yusing/go-proxy/internal/net/http/middleware/errorpage" ) @@ -28,7 +29,7 @@ func (customErrorPage) modifyResponse(resp *http.Response) error { if !gphttp.IsSuccess(resp.StatusCode) && (contentType.IsHTML() || contentType.IsPlainText()) { errorPage, ok := errorpage.GetErrorPageByStatus(resp.StatusCode) if ok { - logger.Debug().Msgf("error page for status %d loaded", resp.StatusCode) + logging.Debug().Msgf("error page for status %d loaded", resp.StatusCode) _, _ = io.Copy(io.Discard, resp.Body) // drain the original body resp.Body.Close() resp.Body = io.NopCloser(bytes.NewReader(errorPage)) @@ -36,7 +37,7 @@ func (customErrorPage) modifyResponse(resp *http.Response) error { resp.Header.Set(gphttp.HeaderContentLength, strconv.Itoa(len(errorPage))) resp.Header.Set(gphttp.HeaderContentType, "text/html; charset=utf-8") } else { - logger.Error().Msgf("unable to load error page for status %d", resp.StatusCode) + logging.Error().Msgf("unable to load error page for status %d", resp.StatusCode) } return nil } @@ -52,7 +53,7 @@ func ServeStaticErrorPageFile(w http.ResponseWriter, r *http.Request) (served bo filename := path[len(gphttp.StaticFilePathPrefix):] file, ok := errorpage.GetStaticFile(filename) if !ok { - logger.Error().Msg("unable to load resource " + filename) + logging.Error().Msg("unable to load resource " + filename) return false } ext := filepath.Ext(filename) @@ -64,10 +65,10 @@ func ServeStaticErrorPageFile(w http.ResponseWriter, r *http.Request) (served bo case ".css": w.Header().Set(gphttp.HeaderContentType, "text/css; charset=utf-8") default: - logger.Error().Msgf("unexpected file type %q for %s", ext, filename) + logging.Error().Msgf("unexpected file type %q for %s", ext, filename) } if _, err := w.Write(file); err != nil { - logger.Err(err).Msg("unable to write resource " + filename) + logging.Err(err).Msg("unable to write resource " + filename) http.Error(w, "Error page failure", http.StatusInternalServerError) } return true diff --git a/internal/net/http/middleware/errorpage/error_page.go b/internal/net/http/middleware/errorpage/error_page.go index e9948ba..d0afe85 100644 --- a/internal/net/http/middleware/errorpage/error_page.go +++ b/internal/net/http/middleware/errorpage/error_page.go @@ -55,7 +55,7 @@ func GetErrorPageByStatus(statusCode int) (content []byte, ok bool) { func loadContent() { files, err := U.ListFiles(errPagesBasePath, 0) if err != nil { - logger.Err(err).Msg("failed to list error page resources") + logging.Err(err).Msg("failed to list error page resources") return } for _, file := range files { @@ -64,7 +64,7 @@ func loadContent() { } content, err := os.ReadFile(file) if err != nil { - logger.Warn().Err(err).Msgf("failed to read error page resource %s", file) + logging.Warn().Err(err).Msgf("failed to read error page resource %s", file) continue } file = path.Base(file) @@ -90,14 +90,14 @@ func watchDir() { loadContent() case events.ActionFileDeleted: fileContentMap.Delete(filename) - logger.Warn().Msgf("error page resource %s deleted", filename) + logging.Warn().Msgf("error page resource %s deleted", filename) case events.ActionFileRenamed: - logger.Warn().Msgf("error page resource %s deleted", filename) + logging.Warn().Msgf("error page resource %s deleted", filename) fileContentMap.Delete(filename) loadContent() } case err := <-errCh: - E.LogError("error watching error page directory", err, &logger) + E.LogError("error watching error page directory", err) } } } diff --git a/internal/net/http/middleware/errorpage/logger.go b/internal/net/http/middleware/errorpage/logger.go deleted file mode 100644 index bc0fc30..0000000 --- a/internal/net/http/middleware/errorpage/logger.go +++ /dev/null @@ -1,5 +0,0 @@ -package errorpage - -import "github.com/yusing/go-proxy/internal/logging" - -var logger = logging.With().Str("module", "errorpage").Logger() diff --git a/internal/net/http/middleware/logger.go b/internal/net/http/middleware/logger.go deleted file mode 100644 index 643f9b6..0000000 --- a/internal/net/http/middleware/logger.go +++ /dev/null @@ -1,5 +0,0 @@ -package middleware - -import "github.com/yusing/go-proxy/internal/logging" - -var logger = logging.With().Str("module", "middleware").Logger() diff --git a/internal/net/http/middleware/middlewares.go b/internal/net/http/middleware/middlewares.go index 1785083..261954b 100644 --- a/internal/net/http/middleware/middlewares.go +++ b/internal/net/http/middleware/middlewares.go @@ -5,6 +5,7 @@ import ( "github.com/yusing/go-proxy/internal/common" E "github.com/yusing/go-proxy/internal/error" + "github.com/yusing/go-proxy/internal/logging" "github.com/yusing/go-proxy/internal/utils" "github.com/yusing/go-proxy/internal/utils/strutils" ) @@ -60,7 +61,7 @@ func LoadComposeFiles() { errs := E.NewBuilder("middleware compile errors") middlewareDefs, err := utils.ListFiles(common.MiddlewareComposeBasePath, 0) if err != nil { - logger.Err(err).Msg("failed to list middleware definitions") + logging.Err(err).Msg("failed to list middleware definitions") return } for _, defFile := range middlewareDefs { @@ -76,7 +77,7 @@ func LoadComposeFiles() { continue } allMiddlewares[name] = m - logger.Info(). + logging.Info(). Str("src", path.Base(defFile)). Str("name", name). Msg("middleware loaded") @@ -95,13 +96,13 @@ func LoadComposeFiles() { continue } allMiddlewares[name] = m - logger.Info(). + logging.Info(). Str("src", path.Base(defFile)). Str("name", name). Msg("middleware loaded") } } if errs.HasError() { - E.LogError(errs.About(), errs.Error(), &logger) + E.LogError(errs.About(), errs.Error()) } } diff --git a/internal/net/http/middleware/redirect_http.go b/internal/net/http/middleware/redirect_http.go index 1ab7197..95b7dcd 100644 --- a/internal/net/http/middleware/redirect_http.go +++ b/internal/net/http/middleware/redirect_http.go @@ -5,6 +5,7 @@ import ( "strings" "github.com/yusing/go-proxy/internal/common" + "github.com/yusing/go-proxy/internal/logging" ) type redirectHTTP struct{} @@ -22,7 +23,7 @@ func (redirectHTTP) before(w http.ResponseWriter, r *http.Request) (proceed bool host = host[:i] // strip port number if present } r.URL.Host = host + ":" + common.ProxyHTTPSPort - logger.Debug().Str("url", r.URL.String()).Msg("redirect to https") + logging.Debug().Str("url", r.URL.String()).Msg("redirect to https") http.Redirect(w, r, r.URL.String(), http.StatusTemporaryRedirect) return true } diff --git a/internal/net/http/reverseproxy/reverse_proxy_mod.go b/internal/net/http/reverseproxy/reverse_proxy_mod.go index 7d823d6..9ec6b3c 100644 --- a/internal/net/http/reverseproxy/reverse_proxy_mod.go +++ b/internal/net/http/reverseproxy/reverse_proxy_mod.go @@ -105,8 +105,6 @@ type httpMetricLogger struct { labels *metrics.HTTPRouteMetricLabels } -var logger = logging.With().Str("module", "reverse_proxy").Logger() - // WriteHeader implements http.ResponseWriter. func (l *httpMetricLogger) WriteHeader(status int) { l.ResponseWriter.WriteHeader(status) @@ -174,7 +172,7 @@ func NewReverseProxy(name string, target types.URL, transport http.RoundTripper) panic("nil transport") } rp := &ReverseProxy{ - Logger: logger.With().Str("name", name).Logger(), + Logger: logging.With().Str("name", name).Logger(), Transport: transport, TargetName: name, TargetURL: target, @@ -213,17 +211,17 @@ func (p *ReverseProxy) errorHandler(rw http.ResponseWriter, r *http.Request, err case errors.Is(err, context.Canceled), errors.Is(err, io.EOF), errors.Is(err, context.DeadlineExceeded): - logger.Debug().Err(err).Str("url", reqURL).Msg("http proxy error") + logging.Debug().Err(err).Str("url", reqURL).Msg("http proxy error") default: var recordErr tls.RecordHeaderError if errors.As(err, &recordErr) { - logger.Error(). + logging.Error(). Str("url", reqURL). Msgf(`scheme was likely misconfigured as https, try setting "proxy.%s.scheme" back to "http"`, p.TargetName) logging.Err(err).Msg("underlying error") } else { - logger.Err(err).Str("url", reqURL).Msg("http proxy error") + logging.Err(err).Str("url", reqURL).Msg("http proxy error") } } diff --git a/internal/route/http.go b/internal/route/http.go index b2b87a3..bfb6401 100755 --- a/internal/route/http.go +++ b/internal/route/http.go @@ -9,6 +9,7 @@ import ( "github.com/yusing/go-proxy/internal/docker" "github.com/yusing/go-proxy/internal/docker/idlewatcher" E "github.com/yusing/go-proxy/internal/error" + "github.com/yusing/go-proxy/internal/logging" gphttp "github.com/yusing/go-proxy/internal/net/http" "github.com/yusing/go-proxy/internal/net/http/accesslog" "github.com/yusing/go-proxy/internal/net/http/loadbalancer" @@ -63,7 +64,7 @@ func NewHTTPRoute(entry *entry.ReverseProxyEntry) (impl, E.Error) { r := &HTTPRoute{ ReverseProxyEntry: entry, rp: rp, - l: logger.With(). + l: logging.With(). Str("type", entry.URL.Scheme). Str("name", service). Logger(), @@ -123,7 +124,7 @@ func (r *HTTPRoute) Start(parent task.Parent) E.Error { case len(pathPatterns) == 1 && pathPatterns[0] == "/": r.handler = r.rp default: - logger.Warn(). + logging.Warn(). Str("route", r.TargetName()). Msg("`path_patterns` is deprecated. Use `rules` instead.") mux := gphttp.NewServeMux() diff --git a/internal/route/logger.go b/internal/route/logger.go deleted file mode 100644 index caf8f51..0000000 --- a/internal/route/logger.go +++ /dev/null @@ -1,5 +0,0 @@ -package route - -import "github.com/yusing/go-proxy/internal/logging" - -var logger = logging.With().Str("module", "route").Logger() diff --git a/internal/route/provider/docker.go b/internal/route/provider/docker.go index 3022ebd..df4466d 100755 --- a/internal/route/provider/docker.go +++ b/internal/route/provider/docker.go @@ -10,6 +10,7 @@ import ( "github.com/yusing/go-proxy/internal/common" "github.com/yusing/go-proxy/internal/docker" E "github.com/yusing/go-proxy/internal/error" + "github.com/yusing/go-proxy/internal/logging" "github.com/yusing/go-proxy/internal/route" U "github.com/yusing/go-proxy/internal/utils" "github.com/yusing/go-proxy/internal/utils/strutils" @@ -36,7 +37,7 @@ func DockerProviderImpl(name, dockerHost string) (ProviderImpl, error) { return &DockerProvider{ name, dockerHost, - logger.With().Str("type", "docker").Str("name", name).Logger(), + logging.With().Str("type", "docker").Str("name", name).Logger(), }, nil } diff --git a/internal/route/provider/file.go b/internal/route/provider/file.go index c9c51e5..a2a59cf 100644 --- a/internal/route/provider/file.go +++ b/internal/route/provider/file.go @@ -8,6 +8,7 @@ import ( "github.com/rs/zerolog" "github.com/yusing/go-proxy/internal/common" E "github.com/yusing/go-proxy/internal/error" + "github.com/yusing/go-proxy/internal/logging" "github.com/yusing/go-proxy/internal/route" "github.com/yusing/go-proxy/internal/utils" W "github.com/yusing/go-proxy/internal/watcher" @@ -23,7 +24,7 @@ func FileProviderImpl(filename string) (ProviderImpl, error) { impl := &FileProvider{ fileName: filename, path: path.Join(common.ConfigBasePath, filename), - l: logger.With().Str("type", "file").Str("name", filename).Logger(), + l: logging.With().Str("type", "file").Str("name", filename).Logger(), } _, err := os.Stat(impl.path) if err != nil { diff --git a/internal/route/provider/logger.go b/internal/route/provider/logger.go deleted file mode 100644 index d734627..0000000 --- a/internal/route/provider/logger.go +++ /dev/null @@ -1,5 +0,0 @@ -package provider - -import "github.com/yusing/go-proxy/internal/logging" - -var logger = logging.With().Str("module", "provider").Logger() diff --git a/internal/route/stream.go b/internal/route/stream.go index 1660fa6..c94d946 100755 --- a/internal/route/stream.go +++ b/internal/route/stream.go @@ -8,6 +8,7 @@ import ( "github.com/yusing/go-proxy/internal/docker" "github.com/yusing/go-proxy/internal/docker/idlewatcher" E "github.com/yusing/go-proxy/internal/error" + "github.com/yusing/go-proxy/internal/logging" net "github.com/yusing/go-proxy/internal/net/types" "github.com/yusing/go-proxy/internal/route/entry" "github.com/yusing/go-proxy/internal/route/routes" @@ -36,7 +37,7 @@ func NewStreamRoute(entry *entry.StreamEntry) (impl, E.Error) { } return &StreamRoute{ StreamEntry: entry, - l: logger.With(). + l: logging.With(). Str("type", string(entry.Scheme.ListeningScheme)). Str("name", entry.TargetName()). Logger(), diff --git a/internal/route/udp_forwarder.go b/internal/route/udp_forwarder.go index 9f4db9a..d33b91b 100644 --- a/internal/route/udp_forwarder.go +++ b/internal/route/udp_forwarder.go @@ -7,6 +7,7 @@ import ( "sync" E "github.com/yusing/go-proxy/internal/error" + "github.com/yusing/go-proxy/internal/logging" "github.com/yusing/go-proxy/internal/net/types" F "github.com/yusing/go-proxy/internal/utils/functional" ) @@ -87,7 +88,7 @@ func (w *UDPForwarder) dialDst() (dstConn net.Conn, err error) { func (w *UDPForwarder) readFromListener(buf *UDPBuf) (srcAddr *net.UDPAddr, err error) { buf.n, buf.oobn, _, srcAddr, err = w.forwarder.ReadMsgUDP(buf.data, buf.oob) if err == nil { - logger.Debug().Msgf("read from listener udp://%s success (n: %d, oobn: %d)", w.Addr().String(), buf.n, buf.oobn) + logging.Debug().Msgf("read from listener udp://%s success (n: %d, oobn: %d)", w.Addr().String(), buf.n, buf.oobn) } return } @@ -101,7 +102,7 @@ func (conn *UDPConn) read() (err error) { conn.buf.oobn = 0 } if err == nil { - logger.Debug().Msgf("read from dst %s success (n: %d, oobn: %d)", conn.DstAddrString(), conn.buf.n, conn.buf.oobn) + logging.Debug().Msgf("read from dst %s success (n: %d, oobn: %d)", conn.DstAddrString(), conn.buf.n, conn.buf.oobn) } return } @@ -109,7 +110,7 @@ func (conn *UDPConn) read() (err error) { func (w *UDPForwarder) writeToSrc(srcAddr *net.UDPAddr, buf *UDPBuf) (err error) { buf.n, buf.oobn, err = w.forwarder.WriteMsgUDP(buf.data[:buf.n], buf.oob[:buf.oobn], srcAddr) if err == nil { - logger.Debug().Msgf("write to src %s://%s success (n: %d, oobn: %d)", srcAddr.Network(), srcAddr.String(), buf.n, buf.oobn) + logging.Debug().Msgf("write to src %s://%s success (n: %d, oobn: %d)", srcAddr.Network(), srcAddr.String(), buf.n, buf.oobn) } return } @@ -119,12 +120,12 @@ func (conn *UDPConn) write() (err error) { case *net.UDPConn: conn.buf.n, conn.buf.oobn, err = dstConn.WriteMsgUDP(conn.buf.data[:conn.buf.n], conn.buf.oob[:conn.buf.oobn], nil) if err == nil { - logger.Debug().Msgf("write to dst %s success (n: %d, oobn: %d)", conn.DstAddrString(), conn.buf.n, conn.buf.oobn) + logging.Debug().Msgf("write to dst %s success (n: %d, oobn: %d)", conn.DstAddrString(), conn.buf.n, conn.buf.oobn) } default: _, err = dstConn.Write(conn.buf.data[:conn.buf.n]) if err == nil { - logger.Debug().Msgf("write to dst %s success (n: %d)", conn.DstAddrString(), conn.buf.n) + logging.Debug().Msgf("write to dst %s success (n: %d)", conn.DstAddrString(), conn.buf.n) } } diff --git a/internal/task/task.go b/internal/task/task.go index a457aed..0a0a5ed 100644 --- a/internal/task/task.go +++ b/internal/task/task.go @@ -8,6 +8,7 @@ import ( "github.com/yusing/go-proxy/internal/common" E "github.com/yusing/go-proxy/internal/error" + "github.com/yusing/go-proxy/internal/logging" "github.com/yusing/go-proxy/internal/utils/strutils" ) @@ -105,14 +106,14 @@ func (t *Task) Finish(reason any) { func (t *Task) finish(reason any) { t.cancel(fmtCause(reason)) if !waitWithTimeout(t.childrenDone) { - logger.Debug(). + logging.Debug(). Str("task", t.name). Strs("subtasks", t.listChildren()). Msg("Timeout waiting for subtasks to finish") } go t.runCallbacks() if !waitWithTimeout(t.callbacksDone) { - logger.Debug(). + logging.Debug(). Str("task", t.name). Strs("callbacks", t.listCallbacks()). Msg("Timeout waiting for callbacks to finish") @@ -123,7 +124,7 @@ func (t *Task) finish(reason any) { } t.parent.subChildCount() allTasks.Remove(t) - logger.Trace().Msg("task " + t.name + " finished") + logging.Trace().Msg("task " + t.name + " finished") } // Subtask returns a new subtask with the given name, derived from the parent's context. @@ -155,7 +156,7 @@ func (t *Task) Subtask(name string, needFinish ...bool) *Task { }() } - logger.Trace().Msg("task " + child.name + " started") + logging.Trace().Msg("task " + child.name + " started") return child } @@ -178,7 +179,7 @@ func (t *Task) MarshalText() ([]byte, error) { func (t *Task) invokeWithRecover(fn func(), caller string) { defer func() { if err := recover(); err != nil { - logger.Error(). + logging.Error(). Interface("err", err). Msg("panic in task " + t.name + "." + caller) if common.IsDebug { diff --git a/internal/task/utils.go b/internal/task/utils.go index e44f860..3f37658 100644 --- a/internal/task/utils.go +++ b/internal/task/utils.go @@ -12,8 +12,6 @@ import ( var ErrProgramExiting = errors.New("program exiting") -var logger = logging.With().Str("module", "task").Logger() - var ( root = newRoot() allTasks = F.NewSet[*Task]() @@ -67,10 +65,10 @@ func GracefulShutdown(timeout time.Duration) (err error) { case <-after: b, err := json.Marshal(DebugTaskList()) if err != nil { - logger.Warn().Err(err).Msg("failed to marshal tasks") + logging.Warn().Err(err).Msg("failed to marshal tasks") return context.DeadlineExceeded } - logger.Warn().RawJSON("tasks", b).Msgf("Timeout waiting for these %d tasks to finish", allTasks.Size()) + logging.Warn().RawJSON("tasks", b).Msgf("Timeout waiting for these %d tasks to finish", allTasks.Size()) return context.DeadlineExceeded } } diff --git a/internal/utils/fs.go b/internal/utils/fs.go index 82ee788..4102901 100644 --- a/internal/utils/fs.go +++ b/internal/utils/fs.go @@ -8,13 +8,17 @@ import ( // Recursively lists all files in a directory until `maxDepth` is reached // Returns a slice of file paths relative to `dir`. -func ListFiles(dir string, maxDepth int) ([]string, error) { +func ListFiles(dir string, maxDepth int, hideHidden ...bool) ([]string, error) { entries, err := os.ReadDir(dir) if err != nil { return nil, fmt.Errorf("error listing directory %s: %w", dir, err) } + hideHiddenFiles := len(hideHidden) > 0 && hideHidden[0] files := make([]string, 0) for _, entry := range entries { + if hideHiddenFiles && entry.Name()[0] == '.' { + continue + } if entry.IsDir() { if maxDepth <= 0 { continue diff --git a/internal/watcher/directory_watcher.go b/internal/watcher/directory_watcher.go index d20aa5c..5ce176e 100644 --- a/internal/watcher/directory_watcher.go +++ b/internal/watcher/directory_watcher.go @@ -9,6 +9,7 @@ import ( "github.com/fsnotify/fsnotify" "github.com/rs/zerolog" E "github.com/yusing/go-proxy/internal/error" + "github.com/yusing/go-proxy/internal/logging" "github.com/yusing/go-proxy/internal/task" "github.com/yusing/go-proxy/internal/watcher/events" ) @@ -40,13 +41,13 @@ func NewDirectoryWatcher(parent task.Parent, dirPath string) *DirWatcher { //! subdirectories are not watched w, err := fsnotify.NewWatcher() if err != nil { - logger.Panic().Err(err).Msg("unable to create fs watcher") + logging.Panic().Err(err).Msg("unable to create fs watcher") } if err = w.Add(dirPath); err != nil { - logger.Panic().Err(err).Msg("unable to create fs watcher") + logging.Panic().Err(err).Msg("unable to create fs watcher") } helper := &DirWatcher{ - Logger: logger.With(). + Logger: logging.With(). Str("type", "dir"). Str("path", dirPath). Logger(), diff --git a/internal/watcher/docker_watcher.go b/internal/watcher/docker_watcher.go index 9a843e5..cc81fe5 100644 --- a/internal/watcher/docker_watcher.go +++ b/internal/watcher/docker_watcher.go @@ -9,6 +9,7 @@ import ( "github.com/rs/zerolog" D "github.com/yusing/go-proxy/internal/docker" E "github.com/yusing/go-proxy/internal/error" + "github.com/yusing/go-proxy/internal/logging" "github.com/yusing/go-proxy/internal/watcher/events" ) @@ -55,7 +56,7 @@ func NewDockerWatcher(host string) DockerWatcher { return DockerWatcher{ host: host, clientOwned: true, - Logger: logger.With(). + Logger: logging.With(). Str("type", "docker"). Str("host", host). Logger(), @@ -65,7 +66,7 @@ func NewDockerWatcher(host string) DockerWatcher { func NewDockerWatcherWithClient(client *D.SharedClient) DockerWatcher { return DockerWatcher{ client: client, - Logger: logger.With(). + Logger: logging.With(). Str("type", "docker"). Str("host", client.DaemonHost()). Logger(), diff --git a/internal/watcher/logger.go b/internal/watcher/logger.go deleted file mode 100644 index ad15593..0000000 --- a/internal/watcher/logger.go +++ /dev/null @@ -1,5 +0,0 @@ -package watcher - -import "github.com/yusing/go-proxy/internal/logging" - -var logger = logging.With().Str("module", "watcher").Logger()