2023-08-23 11:16:23 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright (c) 2023 The go-mail Authors
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
//go:build go1.21
|
|
|
|
// +build go1.21
|
|
|
|
|
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log/slog"
|
|
|
|
)
|
|
|
|
|
|
|
|
// JSONlog is the default structured JSON logger that satisfies the Logger interface
|
|
|
|
type JSONlog struct {
|
2024-02-27 11:35:37 +01:00
|
|
|
level Level
|
|
|
|
log *slog.Logger
|
2023-08-23 11:16:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewJSON returns a new JSONlog type that satisfies the Logger interface
|
2024-02-27 11:35:37 +01:00
|
|
|
func NewJSON(output io.Writer, level Level) *JSONlog {
|
|
|
|
logOpts := slog.HandlerOptions{}
|
|
|
|
switch level {
|
2023-08-23 11:16:23 +02:00
|
|
|
case LevelDebug:
|
2024-02-27 11:35:37 +01:00
|
|
|
logOpts.Level = slog.LevelDebug
|
2023-08-23 11:16:23 +02:00
|
|
|
case LevelInfo:
|
2024-02-27 11:35:37 +01:00
|
|
|
logOpts.Level = slog.LevelInfo
|
2023-08-23 11:16:23 +02:00
|
|
|
case LevelWarn:
|
2024-02-27 11:35:37 +01:00
|
|
|
logOpts.Level = slog.LevelWarn
|
2023-08-23 11:16:23 +02:00
|
|
|
case LevelError:
|
2024-02-27 11:35:37 +01:00
|
|
|
logOpts.Level = slog.LevelError
|
2023-08-23 11:16:23 +02:00
|
|
|
default:
|
2024-02-27 11:35:37 +01:00
|
|
|
logOpts.Level = slog.LevelDebug
|
2023-08-23 11:16:23 +02:00
|
|
|
}
|
2024-02-27 11:35:37 +01:00
|
|
|
logHandler := slog.NewJSONHandler(output, &logOpts)
|
2023-08-23 11:16:23 +02:00
|
|
|
return &JSONlog{
|
2024-02-27 11:35:37 +01:00
|
|
|
level: level,
|
|
|
|
log: slog.New(logHandler),
|
2023-08-23 11:16:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-11 12:56:11 +02:00
|
|
|
// 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...))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-23 11:16:23 +02:00
|
|
|
// Debugf logs a debug message via the structured JSON logger
|
2024-02-27 11:35:37 +01:00
|
|
|
func (l *JSONlog) Debugf(log Log) {
|
|
|
|
if l.level >= LevelDebug {
|
2024-10-11 12:56:11 +02:00
|
|
|
logMessage(LevelDebug, l.log, log, fmt.Sprintf)
|
2023-08-23 11:16:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Infof logs a info message via the structured JSON logger
|
2024-02-27 11:35:37 +01:00
|
|
|
func (l *JSONlog) Infof(log Log) {
|
|
|
|
if l.level >= LevelInfo {
|
2024-10-11 12:56:11 +02:00
|
|
|
logMessage(LevelInfo, l.log, log, fmt.Sprintf)
|
2023-08-23 11:16:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Warnf logs a warn message via the structured JSON logger
|
2024-02-27 11:35:37 +01:00
|
|
|
func (l *JSONlog) Warnf(log Log) {
|
|
|
|
if l.level >= LevelWarn {
|
2024-10-11 12:56:11 +02:00
|
|
|
logMessage(LevelWarn, l.log, log, fmt.Sprintf)
|
2023-08-23 11:16:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Errorf logs a warn message via the structured JSON logger
|
2024-02-27 11:35:37 +01:00
|
|
|
func (l *JSONlog) Errorf(log Log) {
|
|
|
|
if l.level >= LevelError {
|
2024-10-11 12:56:11 +02:00
|
|
|
logMessage(LevelError, l.log, log, fmt.Sprintf)
|
2023-08-23 11:16:23 +02:00
|
|
|
}
|
|
|
|
}
|