mail.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright (C) 2019-2023 Nicola Murino
  2. //
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU Affero General Public License as published
  5. // by the Free Software Foundation, version 3.
  6. //
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU Affero General Public License for more details.
  11. //
  12. // You should have received a copy of the GNU Affero General Public License
  13. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. package logger
  15. import (
  16. "fmt"
  17. "github.com/wneessen/go-mail/log"
  18. )
  19. const (
  20. mailLogSender = "smtpclient"
  21. )
  22. // MailAdapter is an adapter for mail.Logger
  23. type MailAdapter struct {
  24. ConnectionID string
  25. }
  26. // Errorf emits a log at Error level
  27. func (l *MailAdapter) Errorf(logMsg log.Log) {
  28. format := l.getFormatString(&logMsg)
  29. ErrorToConsole(format, logMsg.Messages...)
  30. Log(LevelError, mailLogSender, l.ConnectionID, format, logMsg.Messages...)
  31. }
  32. // Warnf emits a log at Warn level
  33. func (l *MailAdapter) Warnf(logMsg log.Log) {
  34. format := l.getFormatString(&logMsg)
  35. WarnToConsole(format, logMsg.Messages...)
  36. Log(LevelWarn, mailLogSender, l.ConnectionID, format, logMsg.Messages...)
  37. }
  38. // Infof emits a log at Info level
  39. func (l *MailAdapter) Infof(logMsg log.Log) {
  40. format := l.getFormatString(&logMsg)
  41. InfoToConsole(format, logMsg.Messages...)
  42. Log(LevelInfo, mailLogSender, l.ConnectionID, format, logMsg.Messages...)
  43. }
  44. // Debugf emits a log at Debug level
  45. func (l *MailAdapter) Debugf(logMsg log.Log) {
  46. format := l.getFormatString(&logMsg)
  47. DebugToConsole(format, logMsg.Messages...)
  48. Log(LevelDebug, mailLogSender, l.ConnectionID, format, logMsg.Messages...)
  49. }
  50. func (*MailAdapter) getFormatString(logMsg *log.Log) string {
  51. p := "C <-- S:"
  52. if logMsg.Direction == log.DirClientToServer {
  53. p = "C --> S:"
  54. }
  55. return fmt.Sprintf("%s %s", p, logMsg.Format)
  56. }