mirror of
https://github.com/wneessen/go-mail.git
synced 2024-11-14 09:52:54 +01:00
Winni Neessen
1c5c1e318c
The logging system in the smtp.go and log package has been refactored. A new custom log type `Log` was introduced, that includes the message direction, format and arguments. The `Logger` interface and the `Stdlog` implementation were modified to accept this new type. This change provides a clearer understanding of message direction inside logs, allowing for easier debugging and reduced complexity. This change does not affect features or disrupt user functionality. Additionally, it allows for custom implementations of the log.Logger interface to without being forced to use the C --> S/C <-- S direction logging.
114 lines
3.4 KiB
Go
114 lines
3.4 KiB
Go
// SPDX-FileCopyrightText: Copyright (c) 2023 The go-mail Authors
|
|
//
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
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(Log{Direction: DirServerToClient, Format: "test %s", Messages: []interface{}{"foo"}})
|
|
expected := "DEBUG: C <-- S: test foo\n"
|
|
if !strings.HasSuffix(b.String(), expected) {
|
|
t.Errorf("Expected %q, got %q", expected, b.String())
|
|
}
|
|
l.Debugf(Log{Direction: DirClientToServer, Format: "test %s", Messages: []interface{}{"foo"}})
|
|
expected = "DEBUG: C --> S: 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(Log{Direction: DirServerToClient, Format: "test %s", Messages: []interface{}{"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(Log{Direction: DirServerToClient, Format: "test %s", Messages: []interface{}{"foo"}})
|
|
expected := " INFO: C <-- S: test foo\n"
|
|
if !strings.HasSuffix(b.String(), expected) {
|
|
t.Errorf("Expected %q, got %q", expected, b.String())
|
|
}
|
|
l.Infof(Log{Direction: DirClientToServer, Format: "test %s", Messages: []interface{}{"foo"}})
|
|
expected = " INFO: C --> S: 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(Log{Direction: DirServerToClient, Format: "test %s", Messages: []interface{}{"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(Log{Direction: DirServerToClient, Format: "test %s", Messages: []interface{}{"foo"}})
|
|
expected := " WARN: C <-- S: test foo\n"
|
|
if !strings.HasSuffix(b.String(), expected) {
|
|
t.Errorf("Expected %q, got %q", expected, b.String())
|
|
}
|
|
l.Warnf(Log{Direction: DirClientToServer, Format: "test %s", Messages: []interface{}{"foo"}})
|
|
expected = " WARN: C --> S: 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(Log{Direction: DirServerToClient, Format: "test %s", Messages: []interface{}{"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(Log{Direction: DirServerToClient, Format: "test %s", Messages: []interface{}{"foo"}})
|
|
expected := "ERROR: C <-- S: test foo\n"
|
|
if !strings.HasSuffix(b.String(), expected) {
|
|
t.Errorf("Expected %q, got %q", expected, b.String())
|
|
}
|
|
l.Errorf(Log{Direction: DirClientToServer, Format: "test %s", Messages: []interface{}{"foo"}})
|
|
expected = "ERROR: C --> S: 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.Errorf(Log{Direction: DirServerToClient, Format: "test %s", Messages: []interface{}{"foo"}})
|
|
if b.String() != "" {
|
|
t.Error("Error message was not expected to be logged")
|
|
}
|
|
}
|