2023-02-03 10:19:26 +01:00
|
|
|
// SPDX-FileCopyrightText: Copyright (c) 2023 The go-mail Authors
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2023-07-24 15:27:39 +02:00
|
|
|
// 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
|
|
|
|
|
2023-02-03 10:19:26 +01:00
|
|
|
// 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
|
2023-08-02 11:43:45 +02:00
|
|
|
func (l *Stdlog) Debugf(lo Log) {
|
2023-02-03 10:19:26 +01:00
|
|
|
if l.l >= LevelDebug {
|
2023-08-02 11:43:45 +02:00
|
|
|
f := fmt.Sprintf("%s %s", lo.directionPrefix(), lo.Format)
|
|
|
|
_ = l.debug.Output(CallDepth, fmt.Sprintf(f, lo.Messages...))
|
2023-02-03 10:19:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Infof performs a Printf() on the info logger
|
2023-08-02 11:43:45 +02:00
|
|
|
func (l *Stdlog) Infof(lo Log) {
|
2023-02-03 10:19:26 +01:00
|
|
|
if l.l >= LevelInfo {
|
2023-08-02 11:43:45 +02:00
|
|
|
f := fmt.Sprintf("%s %s", lo.directionPrefix(), lo.Format)
|
|
|
|
_ = l.info.Output(CallDepth, fmt.Sprintf(f, lo.Messages...))
|
2023-02-03 10:19:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Warnf performs a Printf() on the warn logger
|
2023-08-02 11:43:45 +02:00
|
|
|
func (l *Stdlog) Warnf(lo Log) {
|
2023-02-03 10:19:26 +01:00
|
|
|
if l.l >= LevelWarn {
|
2023-08-02 11:43:45 +02:00
|
|
|
f := fmt.Sprintf("%s %s", lo.directionPrefix(), lo.Format)
|
|
|
|
_ = l.warn.Output(CallDepth, fmt.Sprintf(f, lo.Messages...))
|
2023-02-03 10:19:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Errorf performs a Printf() on the error logger
|
2023-08-02 11:43:45 +02:00
|
|
|
func (l *Stdlog) Errorf(lo Log) {
|
2023-02-03 10:19:26 +01:00
|
|
|
if l.l >= LevelError {
|
2023-08-02 11:43:45 +02:00
|
|
|
f := fmt.Sprintf("%s %s", lo.directionPrefix(), lo.Format)
|
|
|
|
_ = l.err.Output(CallDepth, fmt.Sprintf(f, lo.Messages...))
|
2023-02-03 10:19:26 +01:00
|
|
|
}
|
|
|
|
}
|