Merge branch 'less-wordy-logger' into 'main'

Make logging package usage less tedious

See merge request whom/bingobot!7
This commit is contained in:
Ava Apples Affine 2024-11-08 22:36:10 +00:00
commit dd2d8aa9b3
2 changed files with 18 additions and 6 deletions

View file

@ -13,7 +13,7 @@ type Logger struct {
}
var (
Log Logger
logger Logger
)
func Init(appConfig *config.AppConfig) {
@ -26,7 +26,7 @@ func Init(appConfig *config.AppConfig) {
Compress: appConfig.LogCompression,
}
Log = Logger{
logger = Logger{
slog.New(
slog.NewJSONHandler(lj, &slog.HandlerOptions{
AddSource: appConfig.LogAddSource,
@ -34,3 +34,15 @@ func Init(appConfig *config.AppConfig) {
),
}
}
func Info(msg string, args ...any) {
logger.Info(msg, args...)
}
func Warn(msg string, args ...any) {
logger.Warn(msg, args...)
}
func Error(msg string, args ...any) {
logger.Error(msg, args...)
}

View file

@ -48,21 +48,21 @@ func startBot() error {
err := state.DiscordSession.Open()
if err != nil {
logging.Log.Error("could not open discord session", "type", "error", "error", err)
logging.Error("could not open discord session", "type", "error", "error", err)
return err
}
logging.Log.Info("shutting down gracefully", "type", "shutdown")
logging.Info("shutting down gracefully", "type", "shutdown")
sigch := make(chan os.Signal, 1)
signal.Notify(sigch, os.Interrupt)
<-sigch
logging.Log.Info("shutting down gracefully", "type", "shutdown")
logging.Info("shutting down gracefully", "type", "shutdown")
err = state.DiscordSession.Close()
if err != nil {
logging.Log.Error("could not close discord session gracefully", "type", "error", "error", err)
logging.Error("could not close discord session gracefully", "type", "error", "error", err)
return err
}