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:
Winni Neessen 2024-02-27 11:35:37 +01:00
parent 077cf47973
commit 6c9b875f58
Signed by: wneessen
GPG key ID: 385AC9889632126E
4 changed files with 69 additions and 69 deletions

View file

@ -15,68 +15,68 @@ import (
// JSONlog is the default structured JSON logger that satisfies the Logger interface // JSONlog is the default structured JSON logger that satisfies the Logger interface
type JSONlog struct { type JSONlog struct {
l Level level Level
log *slog.Logger log *slog.Logger
} }
// NewJSON returns a new JSONlog type that satisfies the Logger interface // NewJSON returns a new JSONlog type that satisfies the Logger interface
func NewJSON(o io.Writer, l Level) *JSONlog { func NewJSON(output io.Writer, level Level) *JSONlog {
lo := slog.HandlerOptions{} logOpts := slog.HandlerOptions{}
switch l { switch level {
case LevelDebug: case LevelDebug:
lo.Level = slog.LevelDebug logOpts.Level = slog.LevelDebug
case LevelInfo: case LevelInfo:
lo.Level = slog.LevelInfo logOpts.Level = slog.LevelInfo
case LevelWarn: case LevelWarn:
lo.Level = slog.LevelWarn logOpts.Level = slog.LevelWarn
case LevelError: case LevelError:
lo.Level = slog.LevelError logOpts.Level = slog.LevelError
default: default:
lo.Level = slog.LevelDebug logOpts.Level = slog.LevelDebug
} }
lh := slog.NewJSONHandler(o, &lo) logHandler := slog.NewJSONHandler(output, &logOpts)
return &JSONlog{ return &JSONlog{
l: l, level: level,
log: slog.New(lh), log: slog.New(logHandler),
} }
} }
// Debugf logs a debug message via the structured JSON logger // Debugf logs a debug message via the structured JSON logger
func (l *JSONlog) Debugf(lo Log) { func (l *JSONlog) Debugf(log Log) {
if l.l >= LevelDebug { if l.level >= LevelDebug {
l.log.WithGroup(DirString).With( l.log.WithGroup(DirString).With(
slog.String(DirFromString, lo.directionFrom()), slog.String(DirFromString, log.directionFrom()),
slog.String(DirToString, lo.directionTo()), slog.String(DirToString, log.directionTo()),
).Debug(fmt.Sprintf(lo.Format, lo.Messages...)) ).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(lo Log) { func (l *JSONlog) Infof(log Log) {
if l.l >= LevelInfo { if l.level >= LevelInfo {
l.log.WithGroup(DirString).With( l.log.WithGroup(DirString).With(
slog.String(DirFromString, lo.directionFrom()), slog.String(DirFromString, log.directionFrom()),
slog.String(DirToString, lo.directionTo()), slog.String(DirToString, log.directionTo()),
).Info(fmt.Sprintf(lo.Format, lo.Messages...)) ).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(lo Log) { func (l *JSONlog) Warnf(log Log) {
if l.l >= LevelWarn { if l.level >= LevelWarn {
l.log.WithGroup(DirString).With( l.log.WithGroup(DirString).With(
slog.String(DirFromString, lo.directionFrom()), slog.String(DirFromString, log.directionFrom()),
slog.String(DirToString, lo.directionTo()), slog.String(DirToString, log.directionTo()),
).Warn(fmt.Sprintf(lo.Format, lo.Messages...)) ).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(lo Log) { func (l *JSONlog) Errorf(log Log) {
if l.l >= LevelError { if l.level >= LevelError {
l.log.WithGroup(DirString).With( l.log.WithGroup(DirString).With(
slog.String(DirFromString, lo.directionFrom()), slog.String(DirFromString, log.directionFrom()),
slog.String(DirToString, lo.directionTo()), slog.String(DirToString, log.directionTo()),
).Error(fmt.Sprintf(lo.Format, lo.Messages...)) ).Error(fmt.Sprintf(log.Format, log.Messages...))
} }
} }

View file

@ -30,8 +30,8 @@ type jsonDir struct {
func TestNewJSON(t *testing.T) { func TestNewJSON(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
l := NewJSON(&b, LevelDebug) l := NewJSON(&b, LevelDebug)
if l.l != LevelDebug { if l.level != LevelDebug {
t.Error("Expected level to be LevelDebug, got ", l.l) t.Error("Expected level to be LevelDebug, got ", l.level)
} }
if l.log == nil { if l.log == nil {
t.Error("logger not initialized") t.Error("logger not initialized")
@ -81,7 +81,7 @@ func TestJSONDebugf(t *testing.T) {
} }
b.Reset() b.Reset()
l.l = LevelInfo l.level = LevelInfo
l.Debugf(Log{Direction: DirServerToClient, Format: "test %s", Messages: []interface{}{"foo"}}) l.Debugf(Log{Direction: DirServerToClient, Format: "test %s", Messages: []interface{}{"foo"}})
if b.String() != "" { if b.String() != "" {
t.Error("Debug message was not expected to be logged") t.Error("Debug message was not expected to be logged")
@ -131,7 +131,7 @@ func TestJSONDebugf_WithDefault(t *testing.T) {
} }
b.Reset() b.Reset()
l.l = LevelInfo l.level = LevelInfo
l.Debugf(Log{Direction: DirServerToClient, Format: "test %s", Messages: []interface{}{"foo"}}) l.Debugf(Log{Direction: DirServerToClient, Format: "test %s", Messages: []interface{}{"foo"}})
if b.String() != "" { if b.String() != "" {
t.Error("Debug message was not expected to be logged") t.Error("Debug message was not expected to be logged")
@ -181,7 +181,7 @@ func TestJSONInfof(t *testing.T) {
} }
b.Reset() b.Reset()
l.l = LevelWarn l.level = LevelWarn
l.Infof(Log{Direction: DirServerToClient, Format: "test %s", Messages: []interface{}{"foo"}}) l.Infof(Log{Direction: DirServerToClient, Format: "test %s", Messages: []interface{}{"foo"}})
if b.String() != "" { if b.String() != "" {
t.Error("Info message was not expected to be logged") t.Error("Info message was not expected to be logged")
@ -231,7 +231,7 @@ func TestJSONWarnf(t *testing.T) {
} }
b.Reset() b.Reset()
l.l = LevelError l.level = LevelError
l.Warnf(Log{Direction: DirServerToClient, Format: "test %s", Messages: []interface{}{"foo"}}) l.Warnf(Log{Direction: DirServerToClient, Format: "test %s", Messages: []interface{}{"foo"}})
if b.String() != "" { if b.String() != "" {
t.Error("Warn message was not expected to be logged") t.Error("Warn message was not expected to be logged")
@ -281,7 +281,7 @@ func TestJSONErrorf(t *testing.T) {
} }
b.Reset() b.Reset()
l.l = -99 l.level = -99
l.Errorf(Log{Direction: DirServerToClient, Format: "test %s", Messages: []interface{}{"foo"}}) l.Errorf(Log{Direction: DirServerToClient, Format: "test %s", Messages: []interface{}{"foo"}})
if b.String() != "" { if b.String() != "" {
t.Error("Error message was not expected to be logged") t.Error("Error message was not expected to be logged")

View file

@ -12,7 +12,7 @@ import (
// Stdlog is the default logger that satisfies the Logger interface // Stdlog is the default logger that satisfies the Logger interface
type Stdlog struct { type Stdlog struct {
l Level level Level
err *log.Logger err *log.Logger
warn *log.Logger warn *log.Logger
info *log.Logger info *log.Logger
@ -24,45 +24,45 @@ type Stdlog struct {
const CallDepth = 2 const CallDepth = 2
// New returns a new Stdlog type that satisfies the Logger interface // 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 lf := log.Lmsgprefix | log.LstdFlags
return &Stdlog{ return &Stdlog{
l: l, level: level,
err: log.New(o, "ERROR: ", lf), err: log.New(output, "ERROR: ", lf),
warn: log.New(o, " WARN: ", lf), warn: log.New(output, " WARN: ", lf),
info: log.New(o, " INFO: ", lf), info: log.New(output, " INFO: ", lf),
debug: log.New(o, "DEBUG: ", lf), debug: log.New(output, "DEBUG: ", lf),
} }
} }
// Debugf performs a Printf() on the debug logger // Debugf performs a Printf() on the debug logger
func (l *Stdlog) Debugf(lo Log) { func (l *Stdlog) Debugf(log Log) {
if l.l >= LevelDebug { if l.level >= LevelDebug {
f := fmt.Sprintf("%s %s", lo.directionPrefix(), lo.Format) format := fmt.Sprintf("%s %s", log.directionPrefix(), log.Format)
_ = l.debug.Output(CallDepth, fmt.Sprintf(f, lo.Messages...)) _ = 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(lo Log) { func (l *Stdlog) Infof(log Log) {
if l.l >= LevelInfo { if l.level >= LevelInfo {
f := fmt.Sprintf("%s %s", lo.directionPrefix(), lo.Format) format := fmt.Sprintf("%s %s", log.directionPrefix(), log.Format)
_ = l.info.Output(CallDepth, fmt.Sprintf(f, lo.Messages...)) _ = 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(lo Log) { func (l *Stdlog) Warnf(log Log) {
if l.l >= LevelWarn { if l.level >= LevelWarn {
f := fmt.Sprintf("%s %s", lo.directionPrefix(), lo.Format) format := fmt.Sprintf("%s %s", log.directionPrefix(), log.Format)
_ = l.warn.Output(CallDepth, fmt.Sprintf(f, lo.Messages...)) _ = 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(lo Log) { func (l *Stdlog) Errorf(log Log) {
if l.l >= LevelError { if l.level >= LevelError {
f := fmt.Sprintf("%s %s", lo.directionPrefix(), lo.Format) format := fmt.Sprintf("%s %s", log.directionPrefix(), log.Format)
_ = l.err.Output(CallDepth, fmt.Sprintf(f, lo.Messages...)) _ = l.err.Output(CallDepth, fmt.Sprintf(format, log.Messages...))
} }
} }

View file

@ -13,8 +13,8 @@ import (
func TestNew(t *testing.T) { func TestNew(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
l := New(&b, LevelDebug) l := New(&b, LevelDebug)
if l.l != LevelDebug { if l.level != LevelDebug {
t.Error("Expected level to be LevelDebug, got ", l.l) t.Error("Expected level to be LevelDebug, got ", l.level)
} }
if l.err == nil || l.warn == nil || l.info == nil || l.debug == nil { if l.err == nil || l.warn == nil || l.info == nil || l.debug == nil {
t.Error("Loggers not initialized") t.Error("Loggers not initialized")
@ -37,7 +37,7 @@ func TestDebugf(t *testing.T) {
} }
b.Reset() b.Reset()
l.l = LevelInfo l.level = LevelInfo
l.Debugf(Log{Direction: DirServerToClient, Format: "test %s", Messages: []interface{}{"foo"}}) l.Debugf(Log{Direction: DirServerToClient, Format: "test %s", Messages: []interface{}{"foo"}})
if b.String() != "" { if b.String() != "" {
t.Error("Debug message was not expected to be logged") t.Error("Debug message was not expected to be logged")
@ -60,7 +60,7 @@ func TestInfof(t *testing.T) {
} }
b.Reset() b.Reset()
l.l = LevelWarn l.level = LevelWarn
l.Infof(Log{Direction: DirServerToClient, Format: "test %s", Messages: []interface{}{"foo"}}) l.Infof(Log{Direction: DirServerToClient, Format: "test %s", Messages: []interface{}{"foo"}})
if b.String() != "" { if b.String() != "" {
t.Error("Info message was not expected to be logged") t.Error("Info message was not expected to be logged")
@ -83,7 +83,7 @@ func TestWarnf(t *testing.T) {
} }
b.Reset() b.Reset()
l.l = LevelError l.level = LevelError
l.Warnf(Log{Direction: DirServerToClient, Format: "test %s", Messages: []interface{}{"foo"}}) l.Warnf(Log{Direction: DirServerToClient, Format: "test %s", Messages: []interface{}{"foo"}})
if b.String() != "" { if b.String() != "" {
t.Error("Warn message was not expected to be logged") t.Error("Warn message was not expected to be logged")
@ -106,7 +106,7 @@ func TestErrorf(t *testing.T) {
} }
b.Reset() b.Reset()
l.l = LevelError - 1 l.level = LevelError - 1
l.Errorf(Log{Direction: DirServerToClient, Format: "test %s", Messages: []interface{}{"foo"}}) l.Errorf(Log{Direction: DirServerToClient, Format: "test %s", Messages: []interface{}{"foo"}})
if b.String() != "" { if b.String() != "" {
t.Error("Error message was not expected to be logged") t.Error("Error message was not expected to be logged")