Skip to content

Commit

Permalink
feat: drop Trx.Log and WithSyslog from mailfilter
Browse files Browse the repository at this point in the history
Trx.Log would just add the QueueId to your log string, that's not that much of a convenience to warrant the tight coupling with the log standard library. The implementation cannot be used with structured logging libraries so wen users of this library use a more sophisticated logging they cannot use this little helper anyway.

BREAKING CHANGE: you should bring your own logging
  • Loading branch information
d--j committed Apr 5, 2023
1 parent 6eb16cd commit 46df6fc
Show file tree
Hide file tree
Showing 7 changed files with 0 additions and 64 deletions.
15 changes: 0 additions & 15 deletions mailfilter/mailfilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ package mailfilter

import (
"context"
"fmt"
"log"
"log/syslog"
"net"
"sync"

Expand Down Expand Up @@ -45,18 +42,6 @@ func New(network, address string, decision DecisionModificationFunc, opts ...Opt
o(&resolvedOptions)
}

if resolvedOptions.syslogPrefix != "" {
sysLogger, err := syslog.NewLogger(syslog.LOG_MAIL, 0)
if err != nil {
return nil, err
}
sysLogger.SetPrefix(resolvedOptions.syslogPrefix)
milter.LogWarning = func(format string, v ...interface{}) {
log.Printf(fmt.Sprintf("milter: warning: %s", format), v...)
sysLogger.Printf(format, v...)
}
}

actions := milter.AllClientSupportedActionMasks
protocols := milter.OptHeaderLeadingSpace | milter.OptNoUnknown

Expand Down
10 changes: 0 additions & 10 deletions mailfilter/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ type options struct {
decisionAt DecisionAt
errorHandling ErrorHandling
skipBody bool
syslogPrefix string
}

type Option func(opt *options)
Expand All @@ -68,12 +67,3 @@ func WithoutBody() Option {
opt.skipBody = true
}
}

// WithSyslog enables logging to syslog with a prefix of prefix.
// This is a global option.
// All calls to [github.com/d--j/go-milter.LogWarning] will be also send to the syslog.
func WithSyslog(prefix string) Option {
return func(opt *options) {
opt.syslogPrefix = prefix
}
}
3 changes: 0 additions & 3 deletions mailfilter/testtrx/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

func Filter(_ context.Context, trx mailfilter.Trx) (mailfilter.Decision, error) {
trx.ChangeMailFrom("", "A=B")
trx.Log("test")
return mailfilter.Accept, nil
}

Expand Down Expand Up @@ -48,8 +47,6 @@ func ExampleTrx() {
for _, m := range trx.Modifications() {
fmt.Println(m)
}
fmt.Println(trx.Logs())

// Output: {0 A=B 0 []}
// [test]
}
10 changes: 0 additions & 10 deletions mailfilter/testtrx/trx.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package testtrx

import (
"bytes"
"fmt"
"io"

"github.com/d--j/go-milter/internal/header"
Expand Down Expand Up @@ -51,7 +50,6 @@ type Trx struct {
rcptTos []*addr.RcptTo
origRcptTos []*addr.RcptTo
queueId string
logs []string
header *header.Header
origHeader *header.Header
body io.ReadSeeker
Expand Down Expand Up @@ -187,14 +185,6 @@ func (t *Trx) SetQueueId(value string) *Trx {
return t
}

func (t *Trx) Log(format string, v ...any) {
t.logs = append(t.logs, fmt.Sprintf(format, v...))
}

func (t *Trx) Logs() []string {
return t.logs
}

func (t *Trx) Modifications() []Modification {
var mods []Modification
if t.origMailFrom.Addr != t.mailFrom.Addr || t.origMailFrom.Args != t.mailFrom.Args {
Expand Down
6 changes: 0 additions & 6 deletions mailfilter/testtrx/trx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,4 @@ func TestTestTrx(t *testing.T) {
if !reflect.DeepEqual(m, expected) {
t.Fatalf("trx.Modifications() = %+v, want %+v", m, expected)
}

trx.Log("test")
expectedLogs := []string{"test"}
if got := trx.Logs(); !reflect.DeepEqual(got, expectedLogs) {
t.Fatalf("trx.Logs() = %+v, want %+v", got, expectedLogs)
}
}
11 changes: 0 additions & 11 deletions mailfilter/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package mailfilter
import (
"bytes"
"context"
"fmt"
"io"
"os"
"regexp"
Expand Down Expand Up @@ -83,16 +82,6 @@ func (t *transaction) QueueId() string {
return t.queueId
}

func (t *transaction) Log(format string, v ...any) {
if LogFunc != nil {
queueId := t.queueId
if queueId == "" {
queueId = "?"
}
LogFunc(fmt.Sprintf("[%s] %s", queueId, format), v...)
}
}

func (t *transaction) cleanup() {
t.headers = nil
t.origHeaders = nil
Expand Down
9 changes: 0 additions & 9 deletions mailfilter/trx.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package mailfilter

import (
"io"
"log"

"github.com/d--j/go-milter/mailfilter/addr"
"github.com/d--j/go-milter/mailfilter/header"
Expand Down Expand Up @@ -66,12 +65,4 @@ type Trx interface {
//
// Only populated if [WithDecisionAt] is bigger than [DecisionAtMailFrom].
QueueId() string

// Log uses [LogFunc] to log the data. The [Trx.QueueId] will be prepended.
Log(format string, v ...any)
}

// LogFunc is the fmt.Printf-like function that [Trx.Log] uses.
// You can overwrite this variable to use something different than [log.Printf].
// If you assign nil to it, all [Trx.Log] calls are no-ops.
var LogFunc = log.Printf

0 comments on commit 46df6fc

Please sign in to comment.