From 9981be3b4f669d5350a8968c8794277304e2d8ff Mon Sep 17 00:00:00 2001 From: Kesuaheli Date: Thu, 26 Dec 2024 20:17:11 +0100 Subject: [PATCH] feat(Logging): improved logger prefixes --- config/config.go | 4 +- data/lang/lang.go | 4 +- database/connection.go | 4 +- event/command/commandBase.go | 4 +- event/component/componentBase.go | 4 +- event/event.go | 4 +- event/modal/componentBase.go | 4 +- event/twitch/messageHandler.go | 5 -- event/twitch/twitch.go | 11 ++++ event/youtube/announce.go | 1 - event/youtube/youtube.go | 7 +++ logger/logger.go | 59 +++++++++++++++++++ main.go | 4 +- modules/adventcalendar/adventcalendarbase.go | 4 +- modules/birthday/birthdaybase.go | 3 + modules/birthday/handleCheck.go | 1 - modules/birthday/handlerSubcommandAnnounce.go | 1 - modules/birthday/handlerSubcommandList.go | 1 - modules/birthday/handlerSubcommandRemove.go | 1 - modules/birthday/handlerSubcommandSet.go | 1 - modules/birthday/handlerUserShow.go | 1 - modules/secretsanta/secretsantabase.go | 4 +- util/universal.go | 4 +- webserver/main.go | 4 +- webserver/twitch/api.go | 4 +- webserver/youtube/youtube.go | 4 +- 26 files changed, 111 insertions(+), 37 deletions(-) create mode 100644 event/twitch/twitch.go create mode 100644 event/youtube/youtube.go create mode 100644 logger/logger.go diff --git a/config/config.go b/config/config.go index ba0a861..dd3a401 100644 --- a/config/config.go +++ b/config/config.go @@ -15,12 +15,12 @@ package config import ( - logger "log" + "cake4everybot/logger" "github.com/spf13/viper" ) -var log = logger.New(logger.Writer(), "[Config] ", logger.LstdFlags|logger.Lmsgprefix) +var log = logger.New("Config") // Load loads the given configuration file as the global config. It // also loads: diff --git a/data/lang/lang.go b/data/lang/lang.go index 5998040..10573a6 100644 --- a/data/lang/lang.go +++ b/data/lang/lang.go @@ -15,13 +15,13 @@ package lang import ( - logger "log" + "cake4everybot/logger" "strings" "github.com/spf13/viper" ) -var log = logger.New(logger.Writer(), "[Config] ", logger.LstdFlags|logger.Lmsgprefix) +var log = logger.New("Data/Lang") var langsMap = map[string]*viper.Viper{} // Unify takes and returns a string wich defines a language, i.e. diff --git a/database/connection.go b/database/connection.go index 4a2d7f0..01648da 100644 --- a/database/connection.go +++ b/database/connection.go @@ -15,9 +15,9 @@ package database import ( + "cake4everybot/logger" "database/sql" "fmt" - logger "log" "time" // mysql driver used for database @@ -25,7 +25,7 @@ import ( "github.com/spf13/viper" ) -var log = logger.New(logger.Writer(), "[Config] ", logger.LstdFlags|logger.Lmsgprefix) +var log = logger.New("Config") var db *sql.DB type connectionConfig struct { diff --git a/event/command/commandBase.go b/event/command/commandBase.go index 2dc7ad4..ec6ea74 100644 --- a/event/command/commandBase.go +++ b/event/command/commandBase.go @@ -15,18 +15,20 @@ package command import ( + "cake4everybot/logger" "cake4everybot/modules/adventcalendar" "cake4everybot/modules/birthday" "cake4everybot/modules/info" "cake4everybot/modules/secretsanta" "cake4everybot/util" "fmt" - "log" "strings" "github.com/bwmarrin/discordgo" ) +var log = logger.New("Event/Command") + // Command is an interface wrapper for all commands. Including chat-comamnds (slash-commands), // message-commands, and user-commands. type Command interface { diff --git a/event/component/componentBase.go b/event/component/componentBase.go index f4d03ec..36f122a 100644 --- a/event/component/componentBase.go +++ b/event/component/componentBase.go @@ -1,13 +1,15 @@ package component import ( + "cake4everybot/logger" "cake4everybot/modules/adventcalendar" "cake4everybot/modules/secretsanta" - "log" "github.com/bwmarrin/discordgo" ) +var log = logger.New("Event/Component") + // Component is an interface wrapper for all message components. type Component interface { // Function of a component. diff --git a/event/event.go b/event/event.go index fa57ff1..0fe730e 100644 --- a/event/event.go +++ b/event/event.go @@ -19,13 +19,13 @@ import ( "cake4everybot/event/component" "cake4everybot/event/modal" "cake4everybot/event/twitch" - logger "log" + "cake4everybot/logger" "github.com/bwmarrin/discordgo" "github.com/kesuaheli/twitchgo" ) -var log = *logger.New(logger.Writer(), "[Events] ", logger.LstdFlags|logger.Lmsgprefix) +var log = logger.New("Event") // PostRegister registers all events, like commands after the bots are started. func PostRegister(dc *discordgo.Session, t *twitchgo.Session, guildID string) error { diff --git a/event/modal/componentBase.go b/event/modal/componentBase.go index 24ebf37..ed5def4 100644 --- a/event/modal/componentBase.go +++ b/event/modal/componentBase.go @@ -1,8 +1,8 @@ package modal import ( + "cake4everybot/logger" "cake4everybot/modules/secretsanta" - "log" "github.com/bwmarrin/discordgo" ) @@ -17,6 +17,8 @@ type Modal interface { ID() string } +var log = logger.New("Event/Modal") + // ModalMap holds all active modals. It maps them from a unique string identifier to the // corresponding [Modal]. var ModalMap = make(map[string]Modal) diff --git a/event/twitch/messageHandler.go b/event/twitch/messageHandler.go index ce78f23..5735973 100644 --- a/event/twitch/messageHandler.go +++ b/event/twitch/messageHandler.go @@ -17,10 +17,8 @@ package twitch import ( "cake4everybot/data/lang" "cake4everybot/database" - "cake4everybot/tools/streamelements" "encoding/json" "fmt" - logger "log" "math/rand" "os" "strings" @@ -32,9 +30,6 @@ import ( const tp string = "twitch.command." -var log logger.Logger = *logger.New(logger.Writer(), "[Twitch] ", logger.LstdFlags|logger.Lmsgprefix) -var se *streamelements.Streamelements - // MessageHandler handles new messages from the twitch chat(s). It will be called on every new // message. func MessageHandler(t *twitchgo.Session, channel string, user *twitchgo.IRCUser, message, msgID string, tags twitchgo.IRCMessageTags) { diff --git a/event/twitch/twitch.go b/event/twitch/twitch.go new file mode 100644 index 0000000..eb22d41 --- /dev/null +++ b/event/twitch/twitch.go @@ -0,0 +1,11 @@ +package twitch + +import ( + "cake4everybot/logger" + "cake4everybot/tools/streamelements" +) + +var ( + log *logger.Logger = logger.New("Event/Twitch") + se *streamelements.Streamelements +) diff --git a/event/youtube/announce.go b/event/youtube/announce.go index 5f23e0e..aace3a1 100644 --- a/event/youtube/announce.go +++ b/event/youtube/announce.go @@ -21,7 +21,6 @@ import ( webYT "cake4everybot/webserver/youtube" "fmt" - "log" "strings" "github.com/bwmarrin/discordgo" diff --git a/event/youtube/youtube.go b/event/youtube/youtube.go new file mode 100644 index 0000000..29be64b --- /dev/null +++ b/event/youtube/youtube.go @@ -0,0 +1,7 @@ +package youtube + +import ( + "cake4everybot/logger" +) + +var log = logger.New("Event/YouTube") diff --git a/logger/logger.go b/logger/logger.go new file mode 100644 index 0000000..7e2fd2d --- /dev/null +++ b/logger/logger.go @@ -0,0 +1,59 @@ +package logger + +import ( + "fmt" + "log" + "sync" +) + +// Logger is a wrapper around the standard [log.Logger] that adds a prefix to each +// loger instance. +type Logger struct { + *log.Logger +} + +const ( + prefixBrackets = "[%s]" + prefixFormat = "% -*s | " + prefixBracketsLength = len(prefixBrackets) - 2 +) + +var ( + loggers = make(map[string]*Logger) + loggersMux sync.Mutex + prefixLength int +) + +// New creates a new logger with the given prefix that writes to the standard +// logger destination. +func New(prefix string) *Logger { + loggersMux.Lock() + defer loggersMux.Unlock() + + if l, ok := loggers[prefix]; ok { + return l + } + + if len(prefix) > prefixLength { + prefixLength = len(prefix) + updatePrefixes() + } + + loggers[prefix] = &Logger{log.New( + log.Writer(), + getPrefix(prefix), + log.LstdFlags|log.Lmsgprefix, + )} + return loggers[prefix] +} + +func updatePrefixes() { + for prefix, l := range loggers { + l.SetPrefix(getPrefix(prefix)) + } +} + +func getPrefix(prefix string) string { + prefix = fmt.Sprintf(prefixBrackets, prefix) + return fmt.Sprintf(prefixFormat, prefixBracketsLength+prefixLength, prefix) +} diff --git a/main.go b/main.go index 0154b42..a8524c7 100644 --- a/main.go +++ b/main.go @@ -16,7 +16,6 @@ package main import ( "context" - logger "log" "os/signal" "syscall" @@ -24,6 +23,7 @@ import ( "cake4everybot/data/lang" "cake4everybot/database" "cake4everybot/event" + "cake4everybot/logger" "cake4everybot/webserver" "github.com/bwmarrin/discordgo" @@ -31,7 +31,7 @@ import ( "github.com/spf13/viper" ) -var log = logger.New(logger.Writer(), "[MAIN] ", logger.LstdFlags|logger.Lmsgprefix) +var log = logger.New("MAIN") const banner string = "\n" + " ______ __ __ __ ______ \n" + diff --git a/modules/adventcalendar/adventcalendarbase.go b/modules/adventcalendar/adventcalendarbase.go index b3cc27e..2e83f02 100644 --- a/modules/adventcalendar/adventcalendarbase.go +++ b/modules/adventcalendar/adventcalendarbase.go @@ -15,8 +15,8 @@ package adventcalendar import ( + "cake4everybot/logger" "cake4everybot/util" - logger "log" "github.com/bwmarrin/discordgo" ) @@ -27,7 +27,7 @@ const ( tp = "discord.command.adventcalendar." ) -var log = logger.New(logger.Writer(), "[Advent] ", logger.LstdFlags|logger.Lmsgprefix) +var log = logger.New("Advent") type adventcalendarBase struct { util.InteractionUtil diff --git a/modules/birthday/birthdaybase.go b/modules/birthday/birthdaybase.go index db95b0f..5af46c9 100644 --- a/modules/birthday/birthdaybase.go +++ b/modules/birthday/birthdaybase.go @@ -17,6 +17,7 @@ package birthday import ( "cake4everybot/data/lang" "cake4everybot/database" + "cake4everybot/logger" "cake4everybot/util" "fmt" "reflect" @@ -33,6 +34,8 @@ const ( tp = "discord.command.birthday." ) +var log = logger.New("Birthday") + type birthdayBase struct { util.InteractionUtil member *discordgo.Member diff --git a/modules/birthday/handleCheck.go b/modules/birthday/handleCheck.go index 02a57ef..13a8965 100644 --- a/modules/birthday/handleCheck.go +++ b/modules/birthday/handleCheck.go @@ -19,7 +19,6 @@ import ( "cake4everybot/database" "cake4everybot/util" "fmt" - "log" "time" "github.com/bwmarrin/discordgo" diff --git a/modules/birthday/handlerSubcommandAnnounce.go b/modules/birthday/handlerSubcommandAnnounce.go index 7aa0c9e..82d5a33 100644 --- a/modules/birthday/handlerSubcommandAnnounce.go +++ b/modules/birthday/handlerSubcommandAnnounce.go @@ -15,7 +15,6 @@ package birthday import ( - "log" "time" "github.com/bwmarrin/discordgo" diff --git a/modules/birthday/handlerSubcommandList.go b/modules/birthday/handlerSubcommandList.go index 230b264..77d377b 100644 --- a/modules/birthday/handlerSubcommandList.go +++ b/modules/birthday/handlerSubcommandList.go @@ -18,7 +18,6 @@ import ( "cake4everybot/data/lang" "cake4everybot/util" "fmt" - "log" "time" "github.com/bwmarrin/discordgo" diff --git a/modules/birthday/handlerSubcommandRemove.go b/modules/birthday/handlerSubcommandRemove.go index 80d378f..286f294 100644 --- a/modules/birthday/handlerSubcommandRemove.go +++ b/modules/birthday/handlerSubcommandRemove.go @@ -18,7 +18,6 @@ import ( "cake4everybot/data/lang" "cake4everybot/util" "fmt" - "log" "strconv" "github.com/bwmarrin/discordgo" diff --git a/modules/birthday/handlerSubcommandSet.go b/modules/birthday/handlerSubcommandSet.go index f40b2a7..1d8489b 100644 --- a/modules/birthday/handlerSubcommandSet.go +++ b/modules/birthday/handlerSubcommandSet.go @@ -18,7 +18,6 @@ import ( "cake4everybot/data/lang" "cake4everybot/util" "fmt" - "log" "strconv" "time" diff --git a/modules/birthday/handlerUserShow.go b/modules/birthday/handlerUserShow.go index 97a53ee..e565cde 100644 --- a/modules/birthday/handlerUserShow.go +++ b/modules/birthday/handlerUserShow.go @@ -18,7 +18,6 @@ import ( "cake4everybot/data/lang" "cake4everybot/util" "fmt" - "log" "strconv" "github.com/bwmarrin/discordgo" diff --git a/modules/secretsanta/secretsantabase.go b/modules/secretsanta/secretsantabase.go index 17eab14..693343b 100644 --- a/modules/secretsanta/secretsantabase.go +++ b/modules/secretsanta/secretsantabase.go @@ -2,10 +2,10 @@ package secretsanta import ( "cake4everybot/data/lang" + "cake4everybot/logger" "cake4everybot/util" "encoding/json" "fmt" - logger "log" "os" "github.com/bwmarrin/discordgo" @@ -19,7 +19,7 @@ const ( tp = "discord.command.secretsanta." ) -var log = logger.New(logger.Writer(), "[SecretSanta] ", logger.LstdFlags|logger.Lmsgprefix) +var log = logger.New("SecretSanta") type secretSantaBase struct { util.InteractionUtil diff --git a/util/universal.go b/util/universal.go index ba67796..2a6e882 100644 --- a/util/universal.go +++ b/util/universal.go @@ -15,10 +15,10 @@ package util import ( - logger "log" + "cake4everybot/logger" ) -var log = logger.New(logger.Writer(), "[Util] ", logger.LstdFlags|logger.Lmsgprefix) +var log = logger.New("Util") // ContainsInt reports whether at least one of num is at least once anywhere in i. func ContainsInt(i []int, num ...int) bool { diff --git a/webserver/main.go b/webserver/main.go index f9172d4..746cee3 100644 --- a/webserver/main.go +++ b/webserver/main.go @@ -15,9 +15,9 @@ package webserver import ( + "cake4everybot/logger" "cake4everybot/webserver/twitch" "cake4everybot/webserver/youtube" - logger "log" "net" "net/http" "time" @@ -26,7 +26,7 @@ import ( "github.com/spf13/viper" ) -var log = logger.New(logger.Writer(), "[WebServer] ", logger.LstdFlags|logger.Lmsgprefix) +var log = logger.New("WebServer") func initHTTP() http.Handler { r := mux.NewRouter() diff --git a/webserver/twitch/api.go b/webserver/twitch/api.go index b2cdd36..535cb39 100644 --- a/webserver/twitch/api.go +++ b/webserver/twitch/api.go @@ -1,13 +1,13 @@ package twitch import ( + "cake4everybot/logger" "crypto/hmac" "crypto/sha256" "encoding/hex" "encoding/json" "fmt" "io" - logger "log" "net/http" "slices" "time" @@ -31,7 +31,7 @@ type rawEvent struct { } var ( - log = logger.New(logger.Writer(), "[WebTwitch] ", logger.LstdFlags|logger.Lmsgprefix) + log = logger.New("Web/Twitch") lastMessages = make([]string, 10) ) diff --git a/webserver/youtube/youtube.go b/webserver/youtube/youtube.go index 2a21cdb..b3bc53e 100644 --- a/webserver/youtube/youtube.go +++ b/webserver/youtube/youtube.go @@ -15,17 +15,17 @@ package youtube import ( + "cake4everybot/logger" "encoding/json" "fmt" "io" - logger "log" "net/http" "time" "github.com/spf13/viper" ) -var log logger.Logger = *logger.New(logger.Writer(), "[WebYouTube] ", logger.LstdFlags|logger.Lmsgprefix) +var log logger.Logger = *logger.New("Web/YouTube") type listResponse struct { Item []item `json:"items,omitempty"`