Skip to content

Commit

Permalink
Merge pull request #336 from sarff/log-opt
Browse files Browse the repository at this point in the history
code duplication reduction for jsonlog.go and stdlog.go
  • Loading branch information
wneessen authored Oct 16, 2024
2 parents e854b21 + 7b297d7 commit 9ae7681
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 24 deletions.
38 changes: 22 additions & 16 deletions log/jsonlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,42 +41,48 @@ func NewJSON(output io.Writer, level Level) *JSONlog {
}
}

// 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 {
l.log.WithGroup(DirString).With(
slog.String(DirFromString, log.directionFrom()),
slog.String(DirToString, log.directionTo()),
).Debug(fmt.Sprintf(log.Format, log.Messages...))
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 {
l.log.WithGroup(DirString).With(
slog.String(DirFromString, log.directionFrom()),
slog.String(DirToString, log.directionTo()),
).Info(fmt.Sprintf(log.Format, log.Messages...))
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 {
l.log.WithGroup(DirString).With(
slog.String(DirFromString, log.directionFrom()),
slog.String(DirToString, log.directionTo()),
).Warn(fmt.Sprintf(log.Format, log.Messages...))
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 {
l.log.WithGroup(DirString).With(
slog.String(DirFromString, log.directionFrom()),
slog.String(DirToString, log.directionTo()),
).Error(fmt.Sprintf(log.Format, log.Messages...))
logMessage(LevelError, l.log, log, fmt.Sprintf)
}
}
18 changes: 10 additions & 8 deletions log/stdlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,34 +35,36 @@ func New(output io.Writer, level Level) *Stdlog {
}
}

// 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 {
format := fmt.Sprintf("%s %s", log.directionPrefix(), log.Format)
_ = l.debug.Output(CallDepth, fmt.Sprintf(format, log.Messages...))
logStdMessage(l.debug, log, CallDepth)
}
}

// Infof performs a Printf() on the info logger
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...))
logStdMessage(l.info, log, CallDepth)
}
}

// Warnf performs a Printf() on the warn logger
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...))
logStdMessage(l.warn, log, CallDepth)
}
}

// Errorf performs a Printf() on the error logger
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...))
logStdMessage(l.err, log, CallDepth)
}
}

0 comments on commit 9ae7681

Please sign in to comment.