block-accounting/backend/internal/pkg/logger/logger.go

88 lines
1.4 KiB
Go
Raw Permalink Normal View History

2024-05-24 17:44:24 +00:00
package logger
import (
"io"
"log/slog"
"os"
)
type LoggerBuilder struct {
local bool
addSource bool
lvl slog.Level
writers []io.Writer
}
func (b *LoggerBuilder) WithWriter(w io.Writer) *LoggerBuilder {
b.writers = append(b.writers, w)
return b
}
func (b *LoggerBuilder) WithLevel(l slog.Level) *LoggerBuilder {
b.lvl = l
return b
}
func (b *LoggerBuilder) Local() *LoggerBuilder {
b.local = true
return b
}
func (b *LoggerBuilder) WithSource() *LoggerBuilder {
b.addSource = true
return b
}
func (b *LoggerBuilder) Build() *slog.Logger {
2024-05-27 18:08:19 +00:00
if len(b.writers) == 0 {
b.writers = append(b.writers, os.Stdout)
}
2024-05-24 17:44:24 +00:00
w := io.MultiWriter(b.writers...)
if b.local {
opts := PrettyHandlerOptions{
SlogOpts: &slog.HandlerOptions{
Level: b.lvl,
AddSource: b.addSource,
},
}
handler := opts.NewPrettyHandler(w)
return slog.New(handler)
}
2024-05-27 18:08:19 +00:00
return newLogger(b.lvl, w)
2024-05-24 17:44:24 +00:00
}
func newLogger(lvl slog.Level, w io.Writer) *slog.Logger {
return slog.New(
2024-05-27 18:08:19 +00:00
slog.NewJSONHandler(w, &slog.HandlerOptions{Level: lvl}),
2024-05-24 17:44:24 +00:00
)
}
func Err(err error) slog.Attr {
return slog.Attr{
Key: "error",
Value: slog.StringValue(err.Error()),
}
}
func MapLevel(lvl string) slog.Level {
switch lvl {
case "dev", "local", "debug":
return slog.LevelDebug
case "warn":
return slog.LevelWarn
case "error":
return slog.LevelError
default:
return slog.LevelInfo
}
}