mirror of
https://github.com/wneessen/go-mail.git
synced 2024-11-15 02:12:55 +01:00
Merge pull request #336 from sarff/log-opt
code duplication reduction for jsonlog.go and stdlog.go
This commit is contained in:
commit
9ae7681651
2 changed files with 32 additions and 24 deletions
|
@ -41,42 +41,48 @@ func NewJSON(output io.Writer, level Level) *JSONlog {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// logMessage is a helper function to handle different log levels and formats.
|
||||||
|
func logMessage(level Level, log *slog.Logger, logData Log, formatFunc func(string, ...interface{}) string) {
|
||||||
|
lGroup := log.WithGroup(DirString).With(
|
||||||
|
slog.String(DirFromString, logData.directionFrom()),
|
||||||
|
slog.String(DirToString, logData.directionTo()),
|
||||||
|
)
|
||||||
|
switch level {
|
||||||
|
case LevelDebug:
|
||||||
|
lGroup.Debug(formatFunc(logData.Format, logData.Messages...))
|
||||||
|
case LevelInfo:
|
||||||
|
lGroup.Info(formatFunc(logData.Format, logData.Messages...))
|
||||||
|
case LevelWarn:
|
||||||
|
lGroup.Warn(formatFunc(logData.Format, logData.Messages...))
|
||||||
|
case LevelError:
|
||||||
|
lGroup.Error(formatFunc(logData.Format, logData.Messages...))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Debugf logs a debug message via the structured JSON logger
|
// Debugf logs a debug message via the structured JSON logger
|
||||||
func (l *JSONlog) Debugf(log Log) {
|
func (l *JSONlog) Debugf(log Log) {
|
||||||
if l.level >= LevelDebug {
|
if l.level >= LevelDebug {
|
||||||
l.log.WithGroup(DirString).With(
|
logMessage(LevelDebug, l.log, log, fmt.Sprintf)
|
||||||
slog.String(DirFromString, log.directionFrom()),
|
|
||||||
slog.String(DirToString, log.directionTo()),
|
|
||||||
).Debug(fmt.Sprintf(log.Format, log.Messages...))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Infof logs a info message via the structured JSON logger
|
// Infof logs a info message via the structured JSON logger
|
||||||
func (l *JSONlog) Infof(log Log) {
|
func (l *JSONlog) Infof(log Log) {
|
||||||
if l.level >= LevelInfo {
|
if l.level >= LevelInfo {
|
||||||
l.log.WithGroup(DirString).With(
|
logMessage(LevelInfo, l.log, log, fmt.Sprintf)
|
||||||
slog.String(DirFromString, log.directionFrom()),
|
|
||||||
slog.String(DirToString, log.directionTo()),
|
|
||||||
).Info(fmt.Sprintf(log.Format, log.Messages...))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Warnf logs a warn message via the structured JSON logger
|
// Warnf logs a warn message via the structured JSON logger
|
||||||
func (l *JSONlog) Warnf(log Log) {
|
func (l *JSONlog) Warnf(log Log) {
|
||||||
if l.level >= LevelWarn {
|
if l.level >= LevelWarn {
|
||||||
l.log.WithGroup(DirString).With(
|
logMessage(LevelWarn, l.log, log, fmt.Sprintf)
|
||||||
slog.String(DirFromString, log.directionFrom()),
|
|
||||||
slog.String(DirToString, log.directionTo()),
|
|
||||||
).Warn(fmt.Sprintf(log.Format, log.Messages...))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Errorf logs a warn message via the structured JSON logger
|
// Errorf logs a warn message via the structured JSON logger
|
||||||
func (l *JSONlog) Errorf(log Log) {
|
func (l *JSONlog) Errorf(log Log) {
|
||||||
if l.level >= LevelError {
|
if l.level >= LevelError {
|
||||||
l.log.WithGroup(DirString).With(
|
logMessage(LevelError, l.log, log, fmt.Sprintf)
|
||||||
slog.String(DirFromString, log.directionFrom()),
|
|
||||||
slog.String(DirToString, log.directionTo()),
|
|
||||||
).Error(fmt.Sprintf(log.Format, log.Messages...))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,34 +35,36 @@ func New(output io.Writer, level Level) *Stdlog {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// logStdMessage is a helper function to handle different log levels and formats for Stdlog.
|
||||||
|
func logStdMessage(logger *log.Logger, logData Log, callDepth int) {
|
||||||
|
format := fmt.Sprintf("%s %s", logData.directionPrefix(), logData.Format)
|
||||||
|
_ = logger.Output(callDepth, fmt.Sprintf(format, logData.Messages...))
|
||||||
|
}
|
||||||
|
|
||||||
// Debugf performs a Printf() on the debug logger
|
// Debugf performs a Printf() on the debug logger
|
||||||
func (l *Stdlog) Debugf(log Log) {
|
func (l *Stdlog) Debugf(log Log) {
|
||||||
if l.level >= LevelDebug {
|
if l.level >= LevelDebug {
|
||||||
format := fmt.Sprintf("%s %s", log.directionPrefix(), log.Format)
|
logStdMessage(l.debug, log, CallDepth)
|
||||||
_ = l.debug.Output(CallDepth, fmt.Sprintf(format, log.Messages...))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Infof performs a Printf() on the info logger
|
// Infof performs a Printf() on the info logger
|
||||||
func (l *Stdlog) Infof(log Log) {
|
func (l *Stdlog) Infof(log Log) {
|
||||||
if l.level >= LevelInfo {
|
if l.level >= LevelInfo {
|
||||||
format := fmt.Sprintf("%s %s", log.directionPrefix(), log.Format)
|
logStdMessage(l.info, log, CallDepth)
|
||||||
_ = l.info.Output(CallDepth, fmt.Sprintf(format, log.Messages...))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Warnf performs a Printf() on the warn logger
|
// Warnf performs a Printf() on the warn logger
|
||||||
func (l *Stdlog) Warnf(log Log) {
|
func (l *Stdlog) Warnf(log Log) {
|
||||||
if l.level >= LevelWarn {
|
if l.level >= LevelWarn {
|
||||||
format := fmt.Sprintf("%s %s", log.directionPrefix(), log.Format)
|
logStdMessage(l.warn, log, CallDepth)
|
||||||
_ = l.warn.Output(CallDepth, fmt.Sprintf(format, log.Messages...))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Errorf performs a Printf() on the error logger
|
// Errorf performs a Printf() on the error logger
|
||||||
func (l *Stdlog) Errorf(log Log) {
|
func (l *Stdlog) Errorf(log Log) {
|
||||||
if l.level >= LevelError {
|
if l.level >= LevelError {
|
||||||
format := fmt.Sprintf("%s %s", log.directionPrefix(), log.Format)
|
logStdMessage(l.err, log, CallDepth)
|
||||||
_ = l.err.Output(CallDepth, fmt.Sprintf(format, log.Messages...))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue