go-mail/log/jsonlog.go
Winni Neessen 77d9e3d02a
#142 Add structured JSON logger and associated tests
This commit introduces a new type 'JSONlog' that satisfies the Logger interface for structured JSON logging. This includes new methods 'Debugf', 'Infof', 'Warnf' and 'Errorf' to log messages at different levels and an associated test 'jsonlog_test.go' to ensure correct functionality. This enhances the logging functionality by providing clarity in logs and eases debugging process.
2023-08-23 11:16:23 +02:00

82 lines
2 KiB
Go

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