2023-02-03 10:19:26 +01:00
|
|
|
// SPDX-FileCopyrightText: Copyright (c) 2022-2023 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
|
|
|
|
|
2023-08-02 11:43:45 +02:00
|
|
|
const (
|
|
|
|
DirServerToClient Direction = iota // Server to Client communication
|
|
|
|
DirClientToServer // Client to Server communication
|
|
|
|
)
|
|
|
|
|
2023-08-15 11:59:16 +02:00
|
|
|
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
|
|
|
|
)
|
|
|
|
|
2023-08-23 11:16:23 +02:00
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2023-08-02 11:43:45 +02:00
|
|
|
// Direction is a type wrapper for the direction a debug log message goes
|
|
|
|
type Direction int
|
|
|
|
|
2023-08-15 11:59:16 +02:00
|
|
|
// Level is a type wrapper for an int
|
|
|
|
type Level int
|
|
|
|
|
2023-08-02 11:43:45 +02:00
|
|
|
// 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{}
|
|
|
|
}
|
|
|
|
|
2023-02-03 10:19:26 +01:00
|
|
|
// Logger is the log interface for go-mail
|
|
|
|
type Logger interface {
|
2023-08-02 11:43:45 +02:00
|
|
|
Debugf(Log)
|
|
|
|
Infof(Log)
|
|
|
|
Warnf(Log)
|
|
|
|
Errorf(Log)
|
2023-02-03 10:19:26 +01:00
|
|
|
}
|
2023-08-15 11:59:16 +02:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2023-08-23 11:16:23 +02:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|