vendored
This commit is contained in:
1 parent
ffa5cd9cd7
commit
f380dfe58d
238 files changed
+151817
-41
No files matched your search
+85
@@ -0,0 +1,85 @@
|
||||
// SPDX-FileCopyrightText: Copyright (c) The go-mail Authors
|
||||
//
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package log
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
)
|
||||
|
||||
// JSONlog is the default structured JSON logger that satisfies the Logger interface
|
||||
type JSONlog struct {
|
||||
level Level
|
||||
log *slog.Logger
|
||||
}
|
||||
|
||||
// NewJSON returns a new JSONlog type that satisfies the Logger interface
|
||||
func NewJSON(output io.Writer, level Level) *JSONlog {
|
||||
logOpts := slog.HandlerOptions{}
|
||||
switch level {
|
||||
case LevelDebug:
|
||||
logOpts.Level = slog.LevelDebug
|
||||
case LevelInfo:
|
||||
logOpts.Level = slog.LevelInfo
|
||||
case LevelWarn:
|
||||
logOpts.Level = slog.LevelWarn
|
||||
case LevelError:
|
||||
logOpts.Level = slog.LevelError
|
||||
default:
|
||||
logOpts.Level = slog.LevelDebug
|
||||
}
|
||||
logHandler := slog.NewJSONHandler(output, &logOpts)
|
||||
return &JSONlog{
|
||||
level: level,
|
||||
log: slog.New(logHandler),
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
func (l *JSONlog) Debugf(log Log) {
|
||||
if l.level >= LevelDebug {
|
||||
logMessage(LevelDebug, l.log, log, fmt.Sprintf)
|
||||
}
|
||||
}
|
||||
|
||||
// Infof logs a info message via the structured JSON logger
|
||||
func (l *JSONlog) Infof(log Log) {
|
||||
if l.level >= LevelInfo {
|
||||
logMessage(LevelInfo, l.log, log, fmt.Sprintf)
|
||||
}
|
||||
}
|
||||
|
||||
// Warnf logs a warn message via the structured JSON logger
|
||||
func (l *JSONlog) Warnf(log Log) {
|
||||
if l.level >= LevelWarn {
|
||||
logMessage(LevelWarn, l.log, log, fmt.Sprintf)
|
||||
}
|
||||
}
|
||||
|
||||
// Errorf logs a warn message via the structured JSON logger
|
||||
func (l *JSONlog) Errorf(log Log) {
|
||||
if l.level >= LevelError {
|
||||
logMessage(LevelError, l.log, log, fmt.Sprintf)
|
||||
}
|
||||
}
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
// SPDX-FileCopyrightText: Copyright (c) The go-mail Authors
|
||||
//
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
// Package log implements a logger interface that can be used within the go-mail package
|
||||
package log
|
||||
|
||||
const (
|
||||
DirServerToClient Direction = iota // Server to Client communication
|
||||
DirClientToServer // Client to Server communication
|
||||
)
|
||||
|
||||
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
|
||||
)
|
||||
|
||||
const (
|
||||
// DirString is a constant used for the structured logger
|
||||
DirString = "direction"
|
||||
// DirFromString is a constant used for the structured logger
|
||||
DirFromString = "from"
|
||||
// DirToString is a constant used for the structured logger
|
||||
DirToString = "to"
|
||||
)
|
||||
|
||||
// Direction is a type wrapper for the direction a debug log message goes
|
||||
type Direction int
|
||||
|
||||
// Level is a type wrapper for an int
|
||||
type Level int
|
||||
|
||||
// Log represents a log message type that holds a log Direction, a Format string
|
||||
// and a slice of Messages
|
||||
type Log struct {
|
||||
Direction Direction
|
||||
Format string
|
||||
Messages []interface{}
|
||||
}
|
||||
|
||||
// Logger is the log interface for go-mail
|
||||
type Logger interface {
|
||||
Debugf(Log)
|
||||
Infof(Log)
|
||||
Warnf(Log)
|
||||
Errorf(Log)
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// directionFrom will return a from direction string depending on the Direction.
|
||||
func (l Log) directionFrom() string {
|
||||
p := "server"
|
||||
if l.Direction == DirClientToServer {
|
||||
p = "client"
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
// directionTo will return a to direction string depending on the Direction.
|
||||
func (l Log) directionTo() string {
|
||||
p := "client"
|
||||
if l.Direction == DirClientToServer {
|
||||
p = "server"
|
||||
}
|
||||
return p
|
||||
}
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
// SPDX-FileCopyrightText: Copyright (c) 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 {
|
||||
level Level
|
||||
err *log.Logger
|
||||
warn *log.Logger
|
||||
info *log.Logger
|
||||
debug *log.Logger
|
||||
}
|
||||
|
||||
// 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(output io.Writer, level Level) *Stdlog {
|
||||
lf := log.Lmsgprefix | log.LstdFlags
|
||||
return &Stdlog{
|
||||
level: level,
|
||||
err: log.New(output, "ERROR: ", lf),
|
||||
warn: log.New(output, " WARN: ", lf),
|
||||
info: log.New(output, " INFO: ", lf),
|
||||
debug: log.New(output, "DEBUG: ", lf),
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
func (l *Stdlog) Debugf(log Log) {
|
||||
if l.level >= LevelDebug {
|
||||
logStdMessage(l.debug, log, CallDepth)
|
||||
}
|
||||
}
|
||||
|
||||
// Infof performs a Printf() on the info logger
|
||||
func (l *Stdlog) Infof(log Log) {
|
||||
if l.level >= LevelInfo {
|
||||
logStdMessage(l.info, log, CallDepth)
|
||||
}
|
||||
}
|
||||
|
||||
// Warnf performs a Printf() on the warn logger
|
||||
func (l *Stdlog) Warnf(log Log) {
|
||||
if l.level >= LevelWarn {
|
||||
logStdMessage(l.warn, log, CallDepth)
|
||||
}
|
||||
}
|
||||
|
||||
// Errorf performs a Printf() on the error logger
|
||||
func (l *Stdlog) Errorf(log Log) {
|
||||
if l.level >= LevelError {
|
||||
logStdMessage(l.err, log, CallDepth)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user