From 1bfef3ea40dee90df4f61ef272f02020870d04d4 Mon Sep 17 00:00:00 2001 From: Leo Date: Sun, 11 Feb 2024 12:45:35 +0200 Subject: [PATCH] Refactor logging in smtpd-proxy.go and server.go --- app/cmd/smtpd-proxy.go | 19 ++++++++++--------- app/server/server.go | 3 +-- app/upstream/forwarder/log-forwarder.go | 7 ++++--- app/upstream/forwarder/ses.go | 6 ++++-- app/upstream/forwarder/smtp.go | 6 ++++-- app/upstream/upstream.go | 12 +++++++----- 6 files changed, 30 insertions(+), 23 deletions(-) diff --git a/app/cmd/smtpd-proxy.go b/app/cmd/smtpd-proxy.go index 2bfa69b..2113b04 100644 --- a/app/cmd/smtpd-proxy.go +++ b/app/cmd/smtpd-proxy.go @@ -53,15 +53,15 @@ func Main(ctx context.Context, args ...string) error { fmt.Printf("smtpd-proxy revision %s-%s\n", BRANCH, COMMIT) opts.ConfigYamlFile = filepath.Clean(opts.ConfigYamlFile) - slog.InfoContext(ctx, "parsing yaml", "path", opts.ConfigYamlFile) + logger.InfoContext(ctx, "parsing yaml", "path", opts.ConfigYamlFile) cfg, err := config.ParseFile(opts.ConfigYamlFile) if err != nil { - slog.ErrorContext(ctx, "failed to parse configuration", "path", opts.ConfigYamlFile, "err", err) + logger.ErrorContext(ctx, "failed to parse configuration", "path", opts.ConfigYamlFile, "err", err) return err } cfg, err = cfg.LoadDefaults() if err != nil { - slog.ErrorContext(ctx, "failed to load configuration", "path", opts.ConfigYamlFile, "err", err) + logger.ErrorContext(ctx, "failed to load configuration", "path", opts.ConfigYamlFile, "err", err) return err } @@ -77,7 +77,7 @@ func ListenProxyAndServe(ctx context.Context, c *config.Config) error { return err } - upstreamServers, err := createUpstreamServers(ctx, srvConfig.UpstreamServers) + upstreamServers, err := createUpstreamServers(ctx, logger, srvConfig.UpstreamServers) if err != nil { return err } @@ -88,6 +88,7 @@ func ListenProxyAndServe(ctx context.Context, c *config.Config) error { srv := server.NewServer( ctx, + logger, srvConfig.Listen, srvConfig.Ehlo, ).WithOptions( @@ -133,21 +134,21 @@ func loadTLSConfig(serverCertificatePath, serverKeyPath string) (*tls.Config, er return &tls.Config{Certificates: []tls.Certificate{cer}, MinVersion: tls.VersionTLS12}, nil } -func createUpstreamServers(ctx context.Context, upstreamServersConfig []config.UpstreamServer) (reg upstream.Registry, err error) { - reg = upstream.NewEmptyRegistry() +func createUpstreamServers(ctx context.Context, logger *slog.Logger, upstreamServersConfig []config.UpstreamServer) (reg upstream.Registry, err error) { + reg = upstream.NewEmptyRegistry(logger) for _, serverConfig := range upstreamServersConfig { var handler upstream.Forwarder var _err error switch serverConfig.Type { case "smtp": - srv := forwarder.NewSMTPServer() + srv := forwarder.NewSMTPServer(logger) handler, _err = srv.Configure(ctx, serverConfig.Settings) case "ses": - srv := forwarder.NewSESServer() + srv := forwarder.NewSESServer(logger) handler, _err = srv.Configure(ctx, serverConfig.Settings) case "log": - srv := forwarder.NewLogServer() + srv := forwarder.NewLogServer(logger) handler, err = srv.Configure(ctx, serverConfig.Settings) default: _err = fmt.Errorf("unrecognized server type: %s. allowed values: smtp, ses, log", serverConfig.Type) diff --git a/app/server/server.go b/app/server/server.go index 861a573..74d2833 100644 --- a/app/server/server.go +++ b/app/server/server.go @@ -51,8 +51,7 @@ func (srv *SrvBackend) ListenAndServe() error { } // NewServer prepares SMTP server -func NewServer(ctx context.Context, addr, domain string) *SrvBackend { - logger := slog.Default().With("server", addr, "ehlo", domain) +func NewServer(ctx context.Context, logger *slog.Logger, addr, domain string) *SrvBackend { bkd := newBackend(ctx, logger, NoOpAuthFunc()) s := smtp.NewServer(bkd) s.Addr = addr diff --git a/app/upstream/forwarder/log-forwarder.go b/app/upstream/forwarder/log-forwarder.go index 84c86db..a5af1a3 100644 --- a/app/upstream/forwarder/log-forwarder.go +++ b/app/upstream/forwarder/log-forwarder.go @@ -13,14 +13,15 @@ type logUpstreamSettings struct { type logServer struct { settings logUpstreamSettings + logger *slog.Logger } var _ upstream.Server = (*logServer)(nil) var _ upstream.Forwarder = (*logServer)(nil) // NewLogServer new ses upstream -func NewLogServer() upstream.Server { - return new(logServer) +func NewLogServer(logger *slog.Logger) upstream.Server { + return &logServer{logger: logger} } func (u *logServer) Configure(ctx context.Context, settings map[string]any) (upstream.Forwarder, error) { @@ -44,7 +45,7 @@ func (u *logServer) Forward(ctx context.Context, mail *upstream.Email) error { } text := string(([]rune(string(mail.Text)))[:20]) + "..." - slog.InfoContext(ctx, "log-forwarder", + u.logger.InfoContext(ctx, "log-forwarder", "uid", uid, "from", mail.From, "to", mail.To, diff --git a/app/upstream/forwarder/ses.go b/app/upstream/forwarder/ses.go index 4db0f71..6e8a78f 100644 --- a/app/upstream/forwarder/ses.go +++ b/app/upstream/forwarder/ses.go @@ -3,6 +3,7 @@ package forwarder import ( "context" "encoding/json" + "log/slog" gohttp "net/http" "github.com/aws/aws-sdk-go-v2/aws" @@ -28,14 +29,15 @@ type sesUpstreamSettings struct { type sesUpstream struct { settings sesUpstreamSettings ses *ses.Client + logger *slog.Logger } var _ upstream.Server = (*sesUpstream)(nil) var _ upstream.Forwarder = (*sesUpstream)(nil) // NewSESServer new ses upstream -func NewSESServer() upstream.Server { - return new(sesUpstream) +func NewSESServer(logger *slog.Logger) upstream.Server { + return &sesUpstream{logger: logger} } func (u *sesUpstream) Configure(ctx context.Context, settings map[string]any) (upstream.Forwarder, error) { diff --git a/app/upstream/forwarder/smtp.go b/app/upstream/forwarder/smtp.go index 6c0880a..0baad64 100644 --- a/app/upstream/forwarder/smtp.go +++ b/app/upstream/forwarder/smtp.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "log/slog" "net" "net/smtp" @@ -22,14 +23,15 @@ type smtpUpstreamSettings struct { type smptUpstream struct { settings smtpUpstreamSettings auth smtp.Auth + logger *slog.Logger } var _ upstream.Server = (*smptUpstream)(nil) var _ upstream.Forwarder = (*smptUpstream)(nil) // NewSMTPServer new smtp upstream -func NewSMTPServer() upstream.Server { - return new(smptUpstream) +func NewSMTPServer(logger *slog.Logger) upstream.Server { + return &smptUpstream{logger: logger} } func (u *smptUpstream) Configure(ctx context.Context, settings map[string]any) (upstream.Forwarder, error) { diff --git a/app/upstream/upstream.go b/app/upstream/upstream.go index aa4c346..2cf5a53 100644 --- a/app/upstream/upstream.go +++ b/app/upstream/upstream.go @@ -72,6 +72,7 @@ type RegistryMap struct { entriesSorted []registryEntry totalWeight int rnd randInt + logger *slog.Logger } // randInt interface for random values. @@ -109,10 +110,11 @@ func (s *randIntStruc) Int() int { } // NewEmptyRegistry creates empty new registry -func NewEmptyRegistry() *RegistryMap { - r := new(RegistryMap) - r.rnd = newRandIntStruc() - return r +func NewEmptyRegistry(logger *slog.Logger) *RegistryMap { + return &RegistryMap{ + rnd: newRandIntStruc(), + logger: logger, + } } func (r *RegistryMap) AddForwarder(forwarder Forwarder, weight int) { @@ -159,7 +161,7 @@ func (r *RegistryMap) Forward(ctx context.Context, mail *Email) error { uid := entry.meta.UID err = sender.Forward(context.WithValue(ctx, entryContextKey, &entry.meta), mail) if err != nil { - slog.WarnContext(ctx, "forward error", "uid", uid, "err", err) + r.logger.WarnContext(ctx, "forward error", "uid", uid, "err", err) } return err }