From 34394b4fcec86ddc48178940ad359817538ad17b Mon Sep 17 00:00:00 2001 From: dd84ai Date: Wed, 1 Nov 2023 23:06:52 +0100 Subject: [PATCH] refactor: prints to log --- app/api/api_test.go | 7 +++---- app/api/web/server.go | 4 ++-- app/configurator/alerts_test.go | 5 +++-- app/configurator/channel_test.go | 3 ++- app/consoler/commands/root.go | 4 ++-- app/listener/main.go | 6 +++--- app/management/anounce.go | 4 ++-- app/management/check.go | 4 ++-- app/management/migrate.go | 4 ++-- app/settings/logus/params.go | 10 +++++++--- app/settings/utils/sleep.go | 4 ++-- app/settings/utils/tests.go | 4 ++-- app/viewer/templ/base_test.go | 2 +- app/viewer/templ/player.go | 19 ++++++++++--------- 14 files changed, 43 insertions(+), 37 deletions(-) diff --git a/app/api/api_test.go b/app/api/api_test.go index c83b455..345bca8 100644 --- a/app/api/api_test.go +++ b/app/api/api_test.go @@ -2,7 +2,6 @@ package api import ( "darkbot/app/settings/logus" - "fmt" "io" "net/http" "strings" @@ -23,7 +22,7 @@ func FixtureTestWebServer() { if body == "pong!" { break } - fmt.Println("sleeping to acquire server pong") + logus.Debug("sleeping to acquire server pong") } time.Sleep(50 * time.Millisecond) } @@ -43,7 +42,7 @@ func TestHomePage(t *testing.T) { FixtureTestWebServer() body := testQuery("/") - fmt.Println(body) + logus.Debug(body) if !strings.Contains(body, "Not found") { t.Error("") @@ -54,7 +53,7 @@ func TestPlayers(t *testing.T) { FixtureTestWebServer() body := testQuery("/players") - fmt.Println(body) + logus.Debug(body) if !strings.Contains(body, "Another page") { t.Error("") diff --git a/app/api/web/server.go b/app/api/web/server.go index 33f136f..6f36a76 100644 --- a/app/api/web/server.go +++ b/app/api/web/server.go @@ -1,7 +1,7 @@ package web import ( - "fmt" + "darkbot/app/settings/logus" "net/http" ) @@ -31,6 +31,6 @@ func (s *Server) GetMux() *http.ServeMux { for route, endpoint := range s.router { mux.Handle(string(route), endpoint) } - fmt.Println("started server") + logus.Info("started server") return mux } diff --git a/app/configurator/alerts_test.go b/app/configurator/alerts_test.go index 6d280f1..cd9a315 100644 --- a/app/configurator/alerts_test.go +++ b/app/configurator/alerts_test.go @@ -1,6 +1,7 @@ package configurator import ( + "darkbot/app/settings/logus" "darkbot/app/settings/types" "darkbot/app/settings/utils" "fmt" @@ -17,7 +18,7 @@ func TestAlertTreshold(t *testing.T) { cfg := NewCfgAlertNeutralPlayersGreaterThan(genericCfg) status, _ := cfg.Status(channelID) - fmt.Println("status=", status) + logus.Debug(fmt.Sprintf("status=%v", status)) assert.Nil(t, status, "status is not Nil. failed aert") cfg.Set(channelID, 5) @@ -38,7 +39,7 @@ func TestAlertBool(t *testing.T) { cfg := NewCfgAlertBaseIsUnderAttack(genericCfg) status, _ := cfg.Status(channelID) - fmt.Println("status=", status) + logus.Debug(fmt.Sprintf("status=%t", status)) assert.False(t, status, "status is not true. failed aert") cfg.Enable(channelID) diff --git a/app/configurator/channel_test.go b/app/configurator/channel_test.go index ff2afc2..c7d333c 100644 --- a/app/configurator/channel_test.go +++ b/app/configurator/channel_test.go @@ -1,6 +1,7 @@ package configurator import ( + "darkbot/app/settings/logus" "darkbot/app/settings/types" "fmt" "testing" @@ -18,7 +19,7 @@ func TestChannels(t *testing.T) { cg.Add("3") channels, _ := cg.List() - fmt.Println(channels) + logus.Debug("invoked List", logus.Items(channels, "channels")) assert.Len(t, channels, 3) cg.Remove("3") diff --git a/app/consoler/commands/root.go b/app/consoler/commands/root.go index 49e409d..8dde064 100644 --- a/app/consoler/commands/root.go +++ b/app/consoler/commands/root.go @@ -6,8 +6,8 @@ import ( "darkbot/app/consoler/commands/cmdgroup" "darkbot/app/consoler/consoler_types" "darkbot/app/settings" + "darkbot/app/settings/logus" "darkbot/app/settings/types" - "fmt" "github.com/spf13/cobra" ) @@ -186,7 +186,7 @@ func (r *rootCommands) CreatePing() { Use: "ping", Short: "Check stuff is working", Run: func(cmd *cobra.Command, args []string) { - fmt.Println("ping called with args=", args) + logus.Debug("ping called with args=", logus.Items(args, "args")) cmd.OutOrStdout().Write([]byte("Pong! from consoler")) }, } diff --git a/app/listener/main.go b/app/listener/main.go index a453fe1..bc77077 100644 --- a/app/listener/main.go +++ b/app/listener/main.go @@ -31,9 +31,9 @@ func Run() { logus.CheckFatal(err, "error opening connection,") defer dg.Close() - fmt.Println("Bot is now running. Press CTRL-C to exit.") + logus.Info("Bot is now running. Press CTRL-C to exit.") utils.SleepAwaitCtrlC() - fmt.Println("gracefully closed discord conn") + logus.Info("gracefully closed discord conn") } func allowedMessage(s *discordgo.Session, m *discordgo.MessageCreate) bool { @@ -103,5 +103,5 @@ func consolerHandler(s *discordgo.Session, m *discordgo.MessageCreate) { if rendered != "" { s.ChannelMessageSend(m.ChannelID, rendered) } - fmt.Println("ChannelID=", m.ChannelID) + logus.Debug("consolerHandler finished", logus.ChannelID(channelID)) } diff --git a/app/management/anounce.go b/app/management/anounce.go index 50d0586..d3a4a85 100644 --- a/app/management/anounce.go +++ b/app/management/anounce.go @@ -7,8 +7,8 @@ import ( "darkbot/app/configurator" "darkbot/app/discorder" "darkbot/app/settings" + "darkbot/app/settings/logus" "darkbot/app/viewer/templ" - "fmt" "strings" "github.com/spf13/cobra" @@ -19,7 +19,7 @@ var amounceCmd = &cobra.Command{ Use: "anounce", Short: "Anounce something", Run: func(cmd *cobra.Command, args []string) { - fmt.Println("Anounce is called with args=", args) + logus.Info("Anounce is called with args=", logus.Items(args, "args")) dg := discorder.NewClient() // go listener.Run() diff --git a/app/management/check.go b/app/management/check.go index 2168eb7..8e7ddb9 100644 --- a/app/management/check.go +++ b/app/management/check.go @@ -5,7 +5,7 @@ package management import ( "darkbot/app/discorder" - "fmt" + "darkbot/app/settings/logus" "github.com/spf13/cobra" ) @@ -15,7 +15,7 @@ var checkCmd = &cobra.Command{ Use: "check", Short: "Experimental command", Run: func(cmd *cobra.Command, args []string) { - fmt.Println("check called") + logus.Info("check called") // fmt.Println("result=", consoler.Consoler{}.New("").Execute().String()) // db := configurator.GetConnection() diff --git a/app/management/migrate.go b/app/management/migrate.go index e7838c5..b09a5e9 100644 --- a/app/management/migrate.go +++ b/app/management/migrate.go @@ -6,7 +6,7 @@ package management import ( "darkbot/app/configurator" "darkbot/app/settings" - "fmt" + "darkbot/app/settings/logus" "github.com/spf13/cobra" ) @@ -16,7 +16,7 @@ var migrateCmd = &cobra.Command{ Use: "migrate", Short: "Migrate db", Run: func(cmd *cobra.Command, args []string) { - fmt.Println("migrate called") + logus.Info("migrate called") configurator.NewConfigurator(settings.Dbpath).AutoMigrateSchema() }, } diff --git a/app/settings/logus/params.go b/app/settings/logus/params.go index eddefbb..36fd5ba 100644 --- a/app/settings/logus/params.go +++ b/app/settings/logus/params.go @@ -74,13 +74,17 @@ func Regex(value types.RegExp) slogParam { } } -func Records[T any](value []T) slogParam { +func Items[T any](value []T, item_name string) slogParam { return func(c *slogGroup) { - c.params["records"] = fmt.Sprintf("%v", value) - c.params["records_len"] = fmt.Sprintf("%d", len(value)) + c.params[item_name] = fmt.Sprintf("%v", value) + c.params[fmt.Sprintf("%s_len", item_name)] = fmt.Sprintf("%d", len(value)) } } +func Records[T any](value []T) slogParam { + return Items[T](value, "records") +} + func APIUrl(value types.APIurl) slogParam { return func(c *slogGroup) { c.params["api_url"] = string(value) diff --git a/app/settings/utils/sleep.go b/app/settings/utils/sleep.go index 60b3d94..02ab752 100644 --- a/app/settings/utils/sleep.go +++ b/app/settings/utils/sleep.go @@ -1,7 +1,7 @@ package utils import ( - "fmt" + "darkbot/app/settings/logus" "os" "os/signal" "sync" @@ -15,7 +15,7 @@ func SleepAwaitCtrlC() { } func SleepForever() { - fmt.Println("awaiting smth forever") + logus.Debug("awaiting smth forever in SleepForever") mu := sync.Mutex{} mu.Lock() mu.Lock() diff --git a/app/settings/utils/tests.go b/app/settings/utils/tests.go index 5efe884..82ded93 100644 --- a/app/settings/utils/tests.go +++ b/app/settings/utils/tests.go @@ -1,13 +1,13 @@ package utils import ( - "fmt" + "darkbot/app/settings/logus" "os" ) func RegenerativeTest(callback func() error) error { if os.Getenv("DARK_TEST_REGENERATE") != "true" { - fmt.Println("Skipping test data regenerative code") + logus.Debug("Skipping test data regenerative code") return nil } diff --git a/app/viewer/templ/base_test.go b/app/viewer/templ/base_test.go index 5309393..518e803 100644 --- a/app/viewer/templ/base_test.go +++ b/app/viewer/templ/base_test.go @@ -34,7 +34,7 @@ func TestBaseViewerMocked(t *testing.T) { render := NewTemplateBase(channelID, dbpath) render.Render() - fmt.Println(render.main.Content) + logus.Debug("render.main.Content" + render.main.Content) assert.NotEmpty(t, render.main.Content) assert.Empty(t, render.AlertHealthLowerThan.Content) diff --git a/app/viewer/templ/player.go b/app/viewer/templ/player.go index de2c11c..af73398 100644 --- a/app/viewer/templ/player.go +++ b/app/viewer/templ/player.go @@ -89,12 +89,13 @@ func (t *PlayersTemplates) Render() { regionTags, _ := t.API.Players.Regions.TagsList(t.API.ChannelID) friendTags, _ := t.API.Players.Friends.TagsList(t.API.ChannelID) enemyTags, _ := t.API.Players.Enemies.TagsList(t.API.ChannelID) - fmt.Println("systemTags=", systemTags) - fmt.Println("regionTags=", regionTags) - fmt.Println("friendTags=", friendTags) - fmt.Println("enemyTags=", enemyTags) - fmt.Println("record.List=", record.List) - + logus.Debug( + "PlayerTemplatesRender next", + logus.Items(systemTags, "systemTags"), + logus.Items(friendTags, "friendTags"), + logus.Items(enemyTags, "enemyTags"), + logus.Items(record.List, "record.List"), + ) neutralPlayers := []player.Player{} enemyPlayers := []player.Player{} friendPlayers := []player.Player{} @@ -117,9 +118,9 @@ func (t *PlayersTemplates) Render() { neutralPlayers = append(neutralPlayers, player) } - logus.Debug("friendPlayers=", logus.Records(friendPlayers)) - logus.Debug("enemyPlayers=", logus.Records(enemyPlayers)) - logus.Debug("neutralPlayers=", logus.Records(neutralPlayers)) + logus.Debug("friendPlayers=", logus.Items(friendPlayers, "friendPlayers")) + logus.Debug("enemyPlayers=", logus.Items(enemyPlayers, "enemyPlayers")) + logus.Debug("neutralPlayers=", logus.Items(neutralPlayers, "neutralPlayers")) if len(systemTags) > 0 || len(regionTags) > 0 { t.neutral.MainTable.Content = utils.TmpRender(playerTemplate, TemplateRendrerPlayerInput{