certwarden-deploy/internal/logger/logger.go

38 lines
883 B
Go
Raw Normal View History

2024-06-28 15:28:54 +02:00
package logger
import (
"log/slog"
"os"
2024-07-03 10:00:21 +02:00
"strconv"
2024-06-30 08:55:07 +02:00
2024-07-03 10:00:21 +02:00
"code.lila.network/adoralaura/certwarden-deploy/internal/configuration"
2024-06-28 15:28:54 +02:00
)
2024-07-12 17:41:50 +02:00
// Initialize initializes a *slog.Logger with the right log level and options.
func Initialize() *slog.Logger {
2024-06-30 08:55:07 +02:00
logLevel := slog.LevelInfo
2024-07-03 10:00:21 +02:00
if configuration.VerboseLogging {
2024-06-30 08:55:07 +02:00
logLevel = slog.LevelDebug
}
2024-07-03 10:00:21 +02:00
if configuration.QuietLogging {
2024-06-30 08:55:07 +02:00
logLevel = slog.LevelError
}
2024-07-03 10:00:21 +02:00
if configuration.DryRun {
logLevel = slog.LevelDebug
}
2024-06-28 15:28:54 +02:00
opts := &slog.HandlerOptions{
Level: logLevel,
2024-06-28 15:28:54 +02:00
}
handler := slog.NewTextHandler(os.Stdout, opts)
2024-07-03 10:00:21 +02:00
log := slog.New(handler)
log.Debug("configuration.VerboseLogging is " + strconv.FormatBool(configuration.VerboseLogging))
log.Debug("configuration.QuietLogging is " + strconv.FormatBool(configuration.QuietLogging))
log.Debug("configuration.DryRun is " + strconv.FormatBool(configuration.DryRun))
2024-06-28 15:28:54 +02:00
2024-07-03 10:00:21 +02:00
return log
2024-06-28 15:28:54 +02:00
}