mirror of
https://github.com/wneessen/go-mail.git
synced 2024-11-09 15:32: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.
91 lines
2.3 KiB
Go
91 lines
2.3 KiB
Go
// SPDX-FileCopyrightText: Copyright (c) 2023 The go-mail Authors
|
|
//
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package log
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
)
|
|
|
|
// Level is a type wrapper for an int
|
|
type Level int
|
|
|
|
// Stdlog is the default logger that satisfies the Logger interface
|
|
type Stdlog struct {
|
|
l Level
|
|
err *log.Logger
|
|
warn *log.Logger
|
|
info *log.Logger
|
|
debug *log.Logger
|
|
}
|
|
|
|
const (
|
|
// LevelError is the Level for only ERROR log messages
|
|
LevelError Level = iota
|
|
// LevelWarn is the Level for WARN and higher log messages
|
|
LevelWarn
|
|
// LevelInfo is the Level for INFO and higher log messages
|
|
LevelInfo
|
|
// LevelDebug is the Level for DEBUG and higher log messages
|
|
LevelDebug
|
|
)
|
|
|
|
// CallDepth is the call depth value for the log.Logger's Output method
|
|
// This defaults to 2 and is only here for better readablity of the code
|
|
const CallDepth = 2
|
|
|
|
// New returns a new Stdlog type that satisfies the Logger interface
|
|
func New(o io.Writer, l Level) *Stdlog {
|
|
lf := log.Lmsgprefix | log.LstdFlags
|
|
return &Stdlog{
|
|
l: l,
|
|
err: log.New(o, "ERROR: ", lf),
|
|
warn: log.New(o, " WARN: ", lf),
|
|
info: log.New(o, " INFO: ", lf),
|
|
debug: log.New(o, "DEBUG: ", lf),
|
|
}
|
|
}
|
|
|
|
// Debugf performs a Printf() on the debug logger
|
|
func (l *Stdlog) Debugf(lo Log) {
|
|
if l.l >= LevelDebug {
|
|
f := fmt.Sprintf("%s %s", lo.directionPrefix(), lo.Format)
|
|
_ = l.debug.Output(CallDepth, fmt.Sprintf(f, lo.Messages...))
|
|
}
|
|
}
|
|
|
|
// Infof performs a Printf() on the info logger
|
|
func (l *Stdlog) Infof(lo Log) {
|
|
if l.l >= LevelInfo {
|
|
f := fmt.Sprintf("%s %s", lo.directionPrefix(), lo.Format)
|
|
_ = l.info.Output(CallDepth, fmt.Sprintf(f, lo.Messages...))
|
|
}
|
|
}
|
|
|
|
// Warnf performs a Printf() on the warn logger
|
|
func (l *Stdlog) Warnf(lo Log) {
|
|
if l.l >= LevelWarn {
|
|
f := fmt.Sprintf("%s %s", lo.directionPrefix(), lo.Format)
|
|
_ = l.warn.Output(CallDepth, fmt.Sprintf(f, lo.Messages...))
|
|
}
|
|
}
|
|
|
|
// Errorf performs a Printf() on the error logger
|
|
func (l *Stdlog) Errorf(lo Log) {
|
|
if l.l >= LevelError {
|
|
f := fmt.Sprintf("%s %s", lo.directionPrefix(), lo.Format)
|
|
_ = l.err.Output(CallDepth, fmt.Sprintf(f, lo.Messages...))
|
|
}
|
|
}
|
|
|
|
// directionPrefix will return a prefix string depending on the Direction.
|
|
func (l Log) directionPrefix() string {
|
|
p := "C <-- S:"
|
|
if l.Direction == DirClientToServer {
|
|
p = "C --> S:"
|
|
}
|
|
return p
|
|
}
|