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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2023-08-23 11:16:23 +02:00
|
|
|
l.log.WithGroup(DirString).With(
|
2024-02-27 11:35:37 +01:00
|
|
|
slog.String(DirFromString, log.directionFrom()),
|
|
|
|
slog.String(DirToString, log.directionTo()),
|
|
|
|
).Debug(fmt.Sprintf(log.Format, log.Messages...))
|
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 {
|
2023-08-23 11:16:23 +02:00
|
|
|
l.log.WithGroup(DirString).With(
|
2024-02-27 11:35:37 +01:00
|
|
|
slog.String(DirFromString, log.directionFrom()),
|
|
|
|
slog.String(DirToString, log.directionTo()),
|
|
|
|
).Info(fmt.Sprintf(log.Format, log.Messages...))
|
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 {
|
2023-08-23 11:16:23 +02:00
|
|
|
l.log.WithGroup(DirString).With(
|
2024-02-27 11:35:37 +01:00
|
|
|
slog.String(DirFromString, log.directionFrom()),
|
|
|
|
slog.String(DirToString, log.directionTo()),
|
|
|
|
).Warn(fmt.Sprintf(log.Format, log.Messages...))
|
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 {
|
2023-08-23 11:16:23 +02:00
|
|
|
l.log.WithGroup(DirString).With(
|
2024-02-27 11:35:37 +01:00
|
|
|
slog.String(DirFromString, log.directionFrom()),
|
|
|
|
slog.String(DirToString, log.directionTo()),
|
|
|
|
).Error(fmt.Sprintf(log.Format, log.Messages...))
|
2023-08-23 11:16:23 +02:00
|
|
|
}
|
|
|
|
}
|