From 665717d9e7c2bd5e7ce9600537f440fe75c3c7af Mon Sep 17 00:00:00 2001 From: Piper Pentagram Date: Fri, 8 Nov 2024 14:33:57 -0800 Subject: [PATCH] Make logging package usage less tedious Before, we were exposing a Logger as logging.Log. This works, but results in code calling Logger.Log.Info() which is a little wordy. Instead, the logger is now kept private and we expose Info, Warn and Error as public functions that wrap logger.Info, logger.Warn and logger.Error. --- internal/logging/logging.go | 16 ++++++++++++++-- main.go | 8 ++++---- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/internal/logging/logging.go b/internal/logging/logging.go index 52302fe..4270f88 100644 --- a/internal/logging/logging.go +++ b/internal/logging/logging.go @@ -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...) +} diff --git a/main.go b/main.go index f7ee39e..cc1f1d6 100644 --- a/main.go +++ b/main.go @@ -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 }