wneessen / go-mail

📧 Easy to use, yet comprehensive library for sending mails with Go
https://go-mail.dev
MIT License
571 stars 44 forks source link

Customize smtp traffic log format #136

Closed Dennis-Zhang-SH closed 1 year ago

Dennis-Zhang-SH commented 1 year ago

Is your feature request related to a problem? Please describe.

No response

Describe the solution you'd like

pass a format string and logging with the format.

Describe alternatives you've considered

fork and chang the log message in forked repo

Additional context

No response

wneessen commented 1 year ago

Hi @Dennis-Zhang-SH, thanks for opening this issue. Can you elaborate a bit more what you are actually requesting? The standard logger we ship with go-mail does support formatted logging already - in fact it's even required by the interface that Debugf, Infof, Warnf and Errorf are satisfied.

Dennis-Zhang-SH commented 1 year ago

Hi @Dennis-Zhang-SH, thanks for opening this issue. Can you elaborate a bit more what you are actually requesting? The standard logger we ship with go-mail does support formatted logging already - in fact it's even required by the interface that Debugf, Infof, Warnf and Errorf are satisfied.

Hi I mean the hard coded "C <-- S:" and "C --> S:", it would be nice to have self defined format, say "Sent: msg" and "Reply (code): msg"

wneessen commented 1 year ago

Thanks for clarifying. I'll see what would be the best approach for this

wneessen commented 1 year ago

So I see two possible options here:

What are your thoughts?

Dennis-Zhang-SH commented 1 year ago

So I see two possible options here:

  • We can add a new option to the Client for you to provide the In- and Out logging strings. But that would require adding new fields to the Client and smtp.Client structs, which might be not favourable.
  • Since this is very much an edge case, we can check the ENV variables for the in- and out strings directly in the method in the smtp client and if set, we could override the defaults. This would be the most favourable option but might not work in every edge case, neither.

What are your thoughts?

I think maybe providing the raw logs would be better? people can manipulate the logs by themselves.

wneessen commented 1 year ago

That's an option, but the communication is two-way. How would we differentiate between message sent by the client or by the server without making the logging interface much more complex?

Dennis-Zhang-SH commented 1 year ago

That's an option, but the communication is two-way. How would we differentiate between message sent by the client or by the server without making the logging interface much more complex?

something like this?

type Logs struct {
    Recv []RecvLog
    Sent []string
}

type RecvLog struct {
    Code int
    Msg  string
}

func (l *Logs) Debug() {
    for _, m := l.Recv {
        logger.Debug("s ---> c", m)
    }
}

then you can setup a new Debug func to overwrite the default debug func.

wneessen commented 1 year ago

I've proposed a change to the log.Logger interface as well as to the Stdlog logger. It breaks the old interface but allows the logging interface to be much more flexible and should allow you to use your own formatting. Feedback is welcome.

If we accept this change, we need to mark it as breaking, even though I don't think that many users are actually using their own logging interface in their implementations.

Dennis-Zhang-SH commented 1 year ago

the default log format should be "C ---> S:" and it should be able to overwrite, but now it's like "C ---> S: myformat", if you don't mind, I can work on this later and try to find a better way to solve this, and probably won't be a breanking change,

wneessen commented 1 year ago

The default format for the StdLoggerwas not changed. It was C --> S: <format> <message> before this change. It was only moved from the smtp package to the log package. For you to not use the C --> S syntax, all you need to do is implement your own Debugf() function that satisfies the log.Logger interface and then you can completely skip the C --> S part or format it in the way you want.

Dennis-Zhang-SH commented 1 year ago

The default format for the StdLoggerwas not changed. It was C --> S: <format> <message> before this change. It was only moved from the smtp package to the log package. For you to not use the C --> S syntax, all you need to do is implement your own Debugf() function that satisfies the log.Logger interface and then you can completely skip the C --> S part or format it in the way you want.

Oh okay, that should fix it, thanks for your work and appologize for my late reply.

wneessen commented 1 year ago

Are you ok with the proposal? In that case i would merge it to main then.

Dennis-Zhang-SH commented 1 year ago

Are you ok with the proposal? In that case i would merge it to main then.

Yes I am fine with it, you can close this issue after merged.