Skip to content

Commit

Permalink
feat(Logging): improved logger prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Kesuaheli committed Dec 26, 2024
1 parent 95d94a5 commit 9981be3
Show file tree
Hide file tree
Showing 26 changed files with 111 additions and 37 deletions.
4 changes: 2 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions data/lang/lang.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions database/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@
package database

import (
"cake4everybot/logger"
"database/sql"
"fmt"
logger "log"
"time"

// mysql driver used for database
_ "github.com/go-sql-driver/mysql"
"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 {
Expand Down
4 changes: 3 additions & 1 deletion event/command/commandBase.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion event/component/componentBase.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
4 changes: 2 additions & 2 deletions event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion event/modal/componentBase.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package modal

import (
"cake4everybot/logger"
"cake4everybot/modules/secretsanta"
"log"

"github.com/bwmarrin/discordgo"
)
Expand All @@ -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)
Expand Down
5 changes: 0 additions & 5 deletions event/twitch/messageHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ package twitch
import (
"cake4everybot/data/lang"
"cake4everybot/database"
"cake4everybot/tools/streamelements"
"encoding/json"
"fmt"
logger "log"
"math/rand"
"os"
"strings"
Expand All @@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions event/twitch/twitch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package twitch

import (
"cake4everybot/logger"
"cake4everybot/tools/streamelements"
)

var (
log *logger.Logger = logger.New("Event/Twitch")
se *streamelements.Streamelements
)
1 change: 0 additions & 1 deletion event/youtube/announce.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
webYT "cake4everybot/webserver/youtube"

"fmt"
"log"
"strings"

"github.com/bwmarrin/discordgo"
Expand Down
7 changes: 7 additions & 0 deletions event/youtube/youtube.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package youtube

import (
"cake4everybot/logger"
)

var log = logger.New("Event/YouTube")
59 changes: 59 additions & 0 deletions logger/logger.go
Original file line number Diff line number Diff line change
@@ -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)
}
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@ package main

import (
"context"
logger "log"
"os/signal"
"syscall"

"cake4everybot/config"
"cake4everybot/data/lang"
"cake4everybot/database"
"cake4everybot/event"
"cake4everybot/logger"
"cake4everybot/webserver"

"github.com/bwmarrin/discordgo"
"github.com/kesuaheli/twitchgo"
"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" +
Expand Down
4 changes: 2 additions & 2 deletions modules/adventcalendar/adventcalendarbase.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
package adventcalendar

import (
"cake4everybot/logger"
"cake4everybot/util"
logger "log"

"github.com/bwmarrin/discordgo"
)
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions modules/birthday/birthdaybase.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package birthday
import (
"cake4everybot/data/lang"
"cake4everybot/database"
"cake4everybot/logger"
"cake4everybot/util"
"fmt"
"reflect"
Expand All @@ -33,6 +34,8 @@ const (
tp = "discord.command.birthday."
)

var log = logger.New("Birthday")

type birthdayBase struct {
util.InteractionUtil
member *discordgo.Member
Expand Down
1 change: 0 additions & 1 deletion modules/birthday/handleCheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"cake4everybot/database"
"cake4everybot/util"
"fmt"
"log"
"time"

"github.com/bwmarrin/discordgo"
Expand Down
1 change: 0 additions & 1 deletion modules/birthday/handlerSubcommandAnnounce.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package birthday

import (
"log"
"time"

"github.com/bwmarrin/discordgo"
Expand Down
1 change: 0 additions & 1 deletion modules/birthday/handlerSubcommandList.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"cake4everybot/data/lang"
"cake4everybot/util"
"fmt"
"log"
"time"

"github.com/bwmarrin/discordgo"
Expand Down
1 change: 0 additions & 1 deletion modules/birthday/handlerSubcommandRemove.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"cake4everybot/data/lang"
"cake4everybot/util"
"fmt"
"log"
"strconv"

"github.com/bwmarrin/discordgo"
Expand Down
1 change: 0 additions & 1 deletion modules/birthday/handlerSubcommandSet.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"cake4everybot/data/lang"
"cake4everybot/util"
"fmt"
"log"
"strconv"
"time"

Expand Down
1 change: 0 additions & 1 deletion modules/birthday/handlerUserShow.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"cake4everybot/data/lang"
"cake4everybot/util"
"fmt"
"log"
"strconv"

"github.com/bwmarrin/discordgo"
Expand Down
4 changes: 2 additions & 2 deletions modules/secretsanta/secretsantabase.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package secretsanta

import (
"cake4everybot/data/lang"
"cake4everybot/logger"
"cake4everybot/util"
"encoding/json"
"fmt"
logger "log"
"os"

"github.com/bwmarrin/discordgo"
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions util/universal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions webserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
package webserver

import (
"cake4everybot/logger"
"cake4everybot/webserver/twitch"
"cake4everybot/webserver/youtube"
logger "log"
"net"
"net/http"
"time"
Expand All @@ -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()
Expand Down
Loading

0 comments on commit 9981be3

Please sign in to comment.