mirror of
https://github.com/wneessen/go-mail.git
synced 2024-11-12 17:02:55 +01:00
Winni Neessen
6633591b51
As stated in https://github.com/wneessen/go-mail/pull/102#issuecomment-1411956040 it would be beneficial if, instead of forcing the Go stdlib logger on the user to provide a simple interface and use that for logging purposes. This PR implements this simple log.Logger interface as well as a standard logger that satisfies this interface. If no custom logger is provided, the Stdlog will be used (which makes use of the Go stdlib again). Accordingly, a `Client.WithLogger` and `Client.SetLogger` have been implemented. Same applies for the smtp counterparts.
89 lines
1.8 KiB
Go
89 lines
1.8 KiB
Go
package log
|
|
|
|
import (
|
|
"bytes"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestNew(t *testing.T) {
|
|
var b bytes.Buffer
|
|
l := New(&b, LevelDebug)
|
|
if l.l != LevelDebug {
|
|
t.Error("Expected level to be LevelDebug, got ", l.l)
|
|
}
|
|
if l.err == nil || l.warn == nil || l.info == nil || l.debug == nil {
|
|
t.Error("Loggers not initialized")
|
|
}
|
|
}
|
|
|
|
func TestDebugf(t *testing.T) {
|
|
var b bytes.Buffer
|
|
l := New(&b, LevelDebug)
|
|
|
|
l.Debugf("test %s", "foo")
|
|
expected := "DEBUG: test foo\n"
|
|
if !strings.HasSuffix(b.String(), expected) {
|
|
t.Errorf("Expected %q, got %q", expected, b.String())
|
|
}
|
|
|
|
b.Reset()
|
|
l.l = LevelInfo
|
|
l.Debugf("test %s", "foo")
|
|
if b.String() != "" {
|
|
t.Error("Debug message was not expected to be logged")
|
|
}
|
|
}
|
|
|
|
func TestInfof(t *testing.T) {
|
|
var b bytes.Buffer
|
|
l := New(&b, LevelInfo)
|
|
|
|
l.Infof("test %s", "foo")
|
|
expected := " INFO: test foo\n"
|
|
if !strings.HasSuffix(b.String(), expected) {
|
|
t.Errorf("Expected %q, got %q", expected, b.String())
|
|
}
|
|
|
|
b.Reset()
|
|
l.l = LevelWarn
|
|
l.Infof("test %s", "foo")
|
|
if b.String() != "" {
|
|
t.Error("Info message was not expected to be logged")
|
|
}
|
|
}
|
|
|
|
func TestWarnf(t *testing.T) {
|
|
var b bytes.Buffer
|
|
l := New(&b, LevelWarn)
|
|
|
|
l.Warnf("test %s", "foo")
|
|
expected := " WARN: test foo\n"
|
|
if !strings.HasSuffix(b.String(), expected) {
|
|
t.Errorf("Expected %q, got %q", expected, b.String())
|
|
}
|
|
|
|
b.Reset()
|
|
l.l = LevelError
|
|
l.Warnf("test %s", "foo")
|
|
if b.String() != "" {
|
|
t.Error("Warn message was not expected to be logged")
|
|
}
|
|
}
|
|
|
|
func TestErrorf(t *testing.T) {
|
|
var b bytes.Buffer
|
|
l := New(&b, LevelError)
|
|
|
|
l.Errorf("test %s", "foo")
|
|
expected := "ERROR: test foo\n"
|
|
if !strings.HasSuffix(b.String(), expected) {
|
|
t.Errorf("Expected %q, got %q", expected, b.String())
|
|
}
|
|
b.Reset()
|
|
l.l = LevelError - 1
|
|
l.Warnf("test %s", "foo")
|
|
if b.String() != "" {
|
|
t.Error("Error message was not expected to be logged")
|
|
}
|
|
}
|