fix logs not printing correctly, removed unneccessary loggers

This commit is contained in:
yusing 2025-01-20 17:42:54 +08:00
parent 64e85c3076
commit dd0bbdc7b4
38 changed files with 130 additions and 150 deletions

View file

@ -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

View file

@ -1,5 +0,0 @@
package autocert
import "github.com/yusing/go-proxy/internal/logging"
var logger = logging.With().Str("module", "autocert").Logger()

View file

@ -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
}

View file

@ -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
}

View file

@ -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()
}

View file

@ -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")

View file

@ -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
}

View file

@ -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,

View file

@ -1,7 +0,0 @@
package docker
import (
"github.com/yusing/go-proxy/internal/logging"
)
var logger = logging.With().Str("module", "docker").Logger()

View file

@ -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)

View file

@ -1,7 +0,0 @@
package entrypoint
import (
"github.com/yusing/go-proxy/internal/logging"
)
var logger = logging.With().Str("module", "entrypoint").Logger()

View file

@ -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

View file

@ -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())
}
}

View file

@ -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")

View file

@ -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")
}
}

View file

@ -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

View file

@ -1,5 +0,0 @@
package loadbalancer
import "github.com/yusing/go-proxy/internal/logging"
var logger = logging.With().Str("module", "load_balancer").Logger()

View file

@ -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
}

View file

@ -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

View file

@ -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)
}
}
}

View file

@ -1,5 +0,0 @@
package errorpage
import "github.com/yusing/go-proxy/internal/logging"
var logger = logging.With().Str("module", "errorpage").Logger()

View file

@ -1,5 +0,0 @@
package middleware
import "github.com/yusing/go-proxy/internal/logging"
var logger = logging.With().Str("module", "middleware").Logger()

View file

@ -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())
}
}

View file

@ -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
}

View file

@ -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")
}
}

View file

@ -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()

View file

@ -1,5 +0,0 @@
package route
import "github.com/yusing/go-proxy/internal/logging"
var logger = logging.With().Str("module", "route").Logger()

View file

@ -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
}

View file

@ -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 {

View file

@ -1,5 +0,0 @@
package provider
import "github.com/yusing/go-proxy/internal/logging"
var logger = logging.With().Str("module", "provider").Logger()

View file

@ -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(),

View file

@ -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)
}
}

View file

@ -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 {

View file

@ -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
}
}

View file

@ -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

View file

@ -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(),

View file

@ -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(),

View file

@ -1,5 +0,0 @@
package watcher
import "github.com/yusing/go-proxy/internal/logging"
var logger = logging.With().Str("module", "watcher").Logger()