This commit is contained in:
sarff 2024-10-11 13:57:20 +03:00 committed by GitHub
commit bc9f6f4470
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 32 additions and 24 deletions

View file

@ -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)
}
}

View file

@ -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)
}
}