mirror of
https://github.com/wneessen/go-mail.git
synced 2024-11-12 17:02:55 +01:00
Refactor variable names in log files
Updated variable names from abbreviations to full names for better code readability in the log and test log files. This modification provides a clearer understanding of variable roles and improves code maintainability in adherence to Go's best practices.
This commit is contained in:
parent
077cf47973
commit
6c9b875f58
4 changed files with 69 additions and 69 deletions
|
@ -15,68 +15,68 @@ import (
|
|||
|
||||
// JSONlog is the default structured JSON logger that satisfies the Logger interface
|
||||
type JSONlog struct {
|
||||
l Level
|
||||
log *slog.Logger
|
||||
level Level
|
||||
log *slog.Logger
|
||||
}
|
||||
|
||||
// NewJSON returns a new JSONlog type that satisfies the Logger interface
|
||||
func NewJSON(o io.Writer, l Level) *JSONlog {
|
||||
lo := slog.HandlerOptions{}
|
||||
switch l {
|
||||
func NewJSON(output io.Writer, level Level) *JSONlog {
|
||||
logOpts := slog.HandlerOptions{}
|
||||
switch level {
|
||||
case LevelDebug:
|
||||
lo.Level = slog.LevelDebug
|
||||
logOpts.Level = slog.LevelDebug
|
||||
case LevelInfo:
|
||||
lo.Level = slog.LevelInfo
|
||||
logOpts.Level = slog.LevelInfo
|
||||
case LevelWarn:
|
||||
lo.Level = slog.LevelWarn
|
||||
logOpts.Level = slog.LevelWarn
|
||||
case LevelError:
|
||||
lo.Level = slog.LevelError
|
||||
logOpts.Level = slog.LevelError
|
||||
default:
|
||||
lo.Level = slog.LevelDebug
|
||||
logOpts.Level = slog.LevelDebug
|
||||
}
|
||||
lh := slog.NewJSONHandler(o, &lo)
|
||||
logHandler := slog.NewJSONHandler(output, &logOpts)
|
||||
return &JSONlog{
|
||||
l: l,
|
||||
log: slog.New(lh),
|
||||
level: level,
|
||||
log: slog.New(logHandler),
|
||||
}
|
||||
}
|
||||
|
||||
// Debugf logs a debug message via the structured JSON logger
|
||||
func (l *JSONlog) Debugf(lo Log) {
|
||||
if l.l >= LevelDebug {
|
||||
func (l *JSONlog) Debugf(log Log) {
|
||||
if l.level >= LevelDebug {
|
||||
l.log.WithGroup(DirString).With(
|
||||
slog.String(DirFromString, lo.directionFrom()),
|
||||
slog.String(DirToString, lo.directionTo()),
|
||||
).Debug(fmt.Sprintf(lo.Format, lo.Messages...))
|
||||
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
|
||||
func (l *JSONlog) Infof(lo Log) {
|
||||
if l.l >= LevelInfo {
|
||||
func (l *JSONlog) Infof(log Log) {
|
||||
if l.level >= LevelInfo {
|
||||
l.log.WithGroup(DirString).With(
|
||||
slog.String(DirFromString, lo.directionFrom()),
|
||||
slog.String(DirToString, lo.directionTo()),
|
||||
).Info(fmt.Sprintf(lo.Format, lo.Messages...))
|
||||
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
|
||||
func (l *JSONlog) Warnf(lo Log) {
|
||||
if l.l >= LevelWarn {
|
||||
func (l *JSONlog) Warnf(log Log) {
|
||||
if l.level >= LevelWarn {
|
||||
l.log.WithGroup(DirString).With(
|
||||
slog.String(DirFromString, lo.directionFrom()),
|
||||
slog.String(DirToString, lo.directionTo()),
|
||||
).Warn(fmt.Sprintf(lo.Format, lo.Messages...))
|
||||
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
|
||||
func (l *JSONlog) Errorf(lo Log) {
|
||||
if l.l >= LevelError {
|
||||
func (l *JSONlog) Errorf(log Log) {
|
||||
if l.level >= LevelError {
|
||||
l.log.WithGroup(DirString).With(
|
||||
slog.String(DirFromString, lo.directionFrom()),
|
||||
slog.String(DirToString, lo.directionTo()),
|
||||
).Error(fmt.Sprintf(lo.Format, lo.Messages...))
|
||||
slog.String(DirFromString, log.directionFrom()),
|
||||
slog.String(DirToString, log.directionTo()),
|
||||
).Error(fmt.Sprintf(log.Format, log.Messages...))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,8 +30,8 @@ type jsonDir struct {
|
|||
func TestNewJSON(t *testing.T) {
|
||||
var b bytes.Buffer
|
||||
l := NewJSON(&b, LevelDebug)
|
||||
if l.l != LevelDebug {
|
||||
t.Error("Expected level to be LevelDebug, got ", l.l)
|
||||
if l.level != LevelDebug {
|
||||
t.Error("Expected level to be LevelDebug, got ", l.level)
|
||||
}
|
||||
if l.log == nil {
|
||||
t.Error("logger not initialized")
|
||||
|
@ -81,7 +81,7 @@ func TestJSONDebugf(t *testing.T) {
|
|||
}
|
||||
|
||||
b.Reset()
|
||||
l.l = LevelInfo
|
||||
l.level = LevelInfo
|
||||
l.Debugf(Log{Direction: DirServerToClient, Format: "test %s", Messages: []interface{}{"foo"}})
|
||||
if b.String() != "" {
|
||||
t.Error("Debug message was not expected to be logged")
|
||||
|
@ -131,7 +131,7 @@ func TestJSONDebugf_WithDefault(t *testing.T) {
|
|||
}
|
||||
|
||||
b.Reset()
|
||||
l.l = LevelInfo
|
||||
l.level = LevelInfo
|
||||
l.Debugf(Log{Direction: DirServerToClient, Format: "test %s", Messages: []interface{}{"foo"}})
|
||||
if b.String() != "" {
|
||||
t.Error("Debug message was not expected to be logged")
|
||||
|
@ -181,7 +181,7 @@ func TestJSONInfof(t *testing.T) {
|
|||
}
|
||||
|
||||
b.Reset()
|
||||
l.l = LevelWarn
|
||||
l.level = LevelWarn
|
||||
l.Infof(Log{Direction: DirServerToClient, Format: "test %s", Messages: []interface{}{"foo"}})
|
||||
if b.String() != "" {
|
||||
t.Error("Info message was not expected to be logged")
|
||||
|
@ -231,7 +231,7 @@ func TestJSONWarnf(t *testing.T) {
|
|||
}
|
||||
|
||||
b.Reset()
|
||||
l.l = LevelError
|
||||
l.level = LevelError
|
||||
l.Warnf(Log{Direction: DirServerToClient, Format: "test %s", Messages: []interface{}{"foo"}})
|
||||
if b.String() != "" {
|
||||
t.Error("Warn message was not expected to be logged")
|
||||
|
@ -281,7 +281,7 @@ func TestJSONErrorf(t *testing.T) {
|
|||
}
|
||||
|
||||
b.Reset()
|
||||
l.l = -99
|
||||
l.level = -99
|
||||
l.Errorf(Log{Direction: DirServerToClient, Format: "test %s", Messages: []interface{}{"foo"}})
|
||||
if b.String() != "" {
|
||||
t.Error("Error message was not expected to be logged")
|
||||
|
|
|
@ -12,7 +12,7 @@ import (
|
|||
|
||||
// Stdlog is the default logger that satisfies the Logger interface
|
||||
type Stdlog struct {
|
||||
l Level
|
||||
level Level
|
||||
err *log.Logger
|
||||
warn *log.Logger
|
||||
info *log.Logger
|
||||
|
@ -24,45 +24,45 @@ type Stdlog struct {
|
|||
const CallDepth = 2
|
||||
|
||||
// New returns a new Stdlog type that satisfies the Logger interface
|
||||
func New(o io.Writer, l Level) *Stdlog {
|
||||
func New(output io.Writer, level 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),
|
||||
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),
|
||||
}
|
||||
}
|
||||
|
||||
// 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...))
|
||||
func (l *Stdlog) Debugf(log Log) {
|
||||
if l.level >= LevelDebug {
|
||||
format := fmt.Sprintf("%s %s", log.directionPrefix(), log.Format)
|
||||
_ = l.debug.Output(CallDepth, fmt.Sprintf(format, log.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...))
|
||||
func (l *Stdlog) Infof(log Log) {
|
||||
if l.level >= LevelInfo {
|
||||
format := fmt.Sprintf("%s %s", log.directionPrefix(), log.Format)
|
||||
_ = l.info.Output(CallDepth, fmt.Sprintf(format, log.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...))
|
||||
func (l *Stdlog) Warnf(log Log) {
|
||||
if l.level >= LevelWarn {
|
||||
format := fmt.Sprintf("%s %s", log.directionPrefix(), log.Format)
|
||||
_ = l.warn.Output(CallDepth, fmt.Sprintf(format, log.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...))
|
||||
func (l *Stdlog) Errorf(log Log) {
|
||||
if l.level >= LevelError {
|
||||
format := fmt.Sprintf("%s %s", log.directionPrefix(), log.Format)
|
||||
_ = l.err.Output(CallDepth, fmt.Sprintf(format, log.Messages...))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,8 +13,8 @@ import (
|
|||
func TestNew(t *testing.T) {
|
||||
var b bytes.Buffer
|
||||
l := New(&b, LevelDebug)
|
||||
if l.l != LevelDebug {
|
||||
t.Error("Expected level to be LevelDebug, got ", l.l)
|
||||
if l.level != LevelDebug {
|
||||
t.Error("Expected level to be LevelDebug, got ", l.level)
|
||||
}
|
||||
if l.err == nil || l.warn == nil || l.info == nil || l.debug == nil {
|
||||
t.Error("Loggers not initialized")
|
||||
|
@ -37,7 +37,7 @@ func TestDebugf(t *testing.T) {
|
|||
}
|
||||
|
||||
b.Reset()
|
||||
l.l = LevelInfo
|
||||
l.level = LevelInfo
|
||||
l.Debugf(Log{Direction: DirServerToClient, Format: "test %s", Messages: []interface{}{"foo"}})
|
||||
if b.String() != "" {
|
||||
t.Error("Debug message was not expected to be logged")
|
||||
|
@ -60,7 +60,7 @@ func TestInfof(t *testing.T) {
|
|||
}
|
||||
|
||||
b.Reset()
|
||||
l.l = LevelWarn
|
||||
l.level = LevelWarn
|
||||
l.Infof(Log{Direction: DirServerToClient, Format: "test %s", Messages: []interface{}{"foo"}})
|
||||
if b.String() != "" {
|
||||
t.Error("Info message was not expected to be logged")
|
||||
|
@ -83,7 +83,7 @@ func TestWarnf(t *testing.T) {
|
|||
}
|
||||
|
||||
b.Reset()
|
||||
l.l = LevelError
|
||||
l.level = LevelError
|
||||
l.Warnf(Log{Direction: DirServerToClient, Format: "test %s", Messages: []interface{}{"foo"}})
|
||||
if b.String() != "" {
|
||||
t.Error("Warn message was not expected to be logged")
|
||||
|
@ -106,7 +106,7 @@ func TestErrorf(t *testing.T) {
|
|||
}
|
||||
|
||||
b.Reset()
|
||||
l.l = LevelError - 1
|
||||
l.level = LevelError - 1
|
||||
l.Errorf(Log{Direction: DirServerToClient, Format: "test %s", Messages: []interface{}{"foo"}})
|
||||
if b.String() != "" {
|
||||
t.Error("Error message was not expected to be logged")
|
||||
|
|
Loading…
Reference in a new issue