Skip to content

Commit

Permalink
implement smtp.Logger in smtpsrv to make logs go through app.cfg.Logger
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan Nimlos committed Oct 4, 2023
1 parent ffa345e commit 0dbcf2e
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions smtpsrv/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,28 @@ package smtpsrv

import (
"context"
"errors"
"fmt"
"net"
"time"

"github.com/emersion/go-smtp"
"github.com/target/goalert/util/log"
)

// SMTPLogger implements the smtp.Logger interface using the main app Logger
type SMTPLogger struct {
logger *log.Logger
}

func (l *SMTPLogger) Printf(format string, v ...interface{}) {
l.logger.Error(context.Background(), errors.New(fmt.Sprintf(format, v...)))
}

func (l *SMTPLogger) Println(v ...interface{}) {
l.logger.Error(context.Background(), errors.New(fmt.Sprint(v...)))
}

// Server implements an SMTP server that creates alerts.
type Server struct {
cfg Config
Expand All @@ -19,6 +35,7 @@ func NewServer(cfg Config) *Server {
s := &Server{cfg: cfg}

srv := smtp.NewServer(s)
srv.ErrorLog = &SMTPLogger{logger: cfg.Logger}
srv.Domain = cfg.Domain
srv.ReadTimeout = 10 * time.Second
srv.WriteTimeout = 10 * time.Second
Expand Down

0 comments on commit 0dbcf2e

Please sign in to comment.