Skip to content

Commit

Permalink
refactor: all fmt.prints to logus
Browse files Browse the repository at this point in the history
  • Loading branch information
dd84ai committed Nov 1, 2023
1 parent 2928b7b commit 4e61410
Show file tree
Hide file tree
Showing 16 changed files with 67 additions and 57 deletions.
5 changes: 2 additions & 3 deletions app/configurator/channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package configurator
import (
"darkbot/app/settings/logus"
"darkbot/app/settings/types"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -25,13 +24,13 @@ func TestChannels(t *testing.T) {
cg.Remove("3")

channels, _ = cg.List()
fmt.Println(channels)
logus.Debug("", logus.Items(channels, "channels"))
assert.Len(t, channels, 2)

cg.Add("3")

channels, _ = cg.List()
fmt.Println(channels)
logus.Debug("", logus.Items(channels, "channels"))
assert.Len(t, channels, 3)
})
}
4 changes: 2 additions & 2 deletions app/configurator/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package configurator

import (
"darkbot/app/settings"
"darkbot/app/settings/logus"
"darkbot/app/settings/types"
"darkbot/app/settings/utils"
"fmt"
"os"
)

Expand All @@ -17,7 +17,7 @@ func FixtureMigrator(callback func(dbpath types.Dbpath)) Configurator {
dbname := utils.TokenHex(8)
dbpath := types.Dbpath(settings.NewDBPath(dbname))
// setup
fmt.Println(dbpath)
logus.Debug("", logus.Dbpath(dbpath))
os.Remove(string(dbpath))
os.Remove(string(dbpath) + "-shm")
os.Remove(string(dbpath) + "-wal")
Expand Down
7 changes: 2 additions & 5 deletions app/configurator/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"darkbot/app/settings/logus"
"darkbot/app/settings/types"
"darkbot/app/settings/utils"
"fmt"
)

type IConfiguratorTags interface {
Expand Down Expand Up @@ -101,10 +100,8 @@ func (c ConfiguratorTags[T]) TagsList(channelID types.DiscordChannelID) ([]types
func (c ConfiguratorTags[T]) TagsClear(channelID types.DiscordChannelID) error {
tags := []T{}
result := c.db.Unscoped().Where("channel_id = ?", channelID).Find(&tags)
fmt.Println("Clear.Find.rowsAffected=", result.RowsAffected)
fmt.Println("Clear.Find.err=", result.Error)
logus.Debug("Clear.Find", logus.GormResult(result))
result = c.db.Unscoped().Delete(&tags)
fmt.Println("Clear.Delete.rowsAffected=", result.RowsAffected)
fmt.Println("Clear.Delete.err=", result.Error)
logus.Debug("Clear.Detete", logus.GormResult(result))
return result.Error
}
4 changes: 2 additions & 2 deletions app/configurator/tags_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package configurator

import (
"darkbot/app/settings/logus"
"darkbot/app/settings/types"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -44,7 +44,7 @@ func TestCanWriteRepeatedTagsPerChannels(t *testing.T) {

assert.Error(t, err, "expected error to get in test")
assert.Contains(t, err.Error(), "database already has those items")
fmt.Println("err=", err.Error())
logus.Debug("err=", logus.OptError(err))

// make a test to check? :thinking:
tags, _ = cg.TagsList("c2")
Expand Down
30 changes: 17 additions & 13 deletions app/consoler/commands/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"darkbot/app/configurator"
"darkbot/app/consoler/commands/cmdgroup"
"darkbot/app/consoler/printer"
"fmt"
"darkbot/app/settings/logus"
"strconv"
"strings"

Expand Down Expand Up @@ -33,17 +33,21 @@ func (t *alertThresholdCommands[T]) CreateSetAlertCmd() {
Short: "Set alert (Works as set {number})",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("CreateSetAlertCmd.consoler running with args=", args)
logus.Debug("CreateSetAlertCmd.consoler running with args=", logus.Args(args))
printer.Println(cmd, "Attempting to parse input into integer number")
rawInteger := args[0]
integer, err := strconv.Atoi(rawInteger)
if logus.CheckWarn(err, "Atoi result with warning", logus.OptError(err)) {
printer.Println(cmd, "failed to parse value to integer. Value="+rawInteger)
}

printer.Println(cmd, "Parsed integer = "+strconv.Itoa(integer))
err = t.cfgTags.Set(t.GetChannelID(), integer)
if err != nil {
cmd.OutOrStdout().Write([]byte("ERR msg=" + err.Error()))
return
}
fmt.Println(len(args))
logus.Debug("checking args again?", logus.Args(args))

printer.Println(cmd, "OK alert threshold is set")
},
Expand All @@ -56,7 +60,7 @@ func (t *alertThresholdCommands[T]) CreateUnsetCmd() {
Use: "unset",
Short: "Unsert alert / Clear alert",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("CreateUnsetCmd.consoler running with args=", args)
logus.Debug("CreateUnsetCmd.consoler running with args=", logus.Args(args))
err := t.cfgTags.Unset(t.GetChannelID())
if err != nil {
cmd.OutOrStdout().Write([]byte("ERR msg=" + err.Error()))
Expand All @@ -73,7 +77,7 @@ func (t *alertThresholdCommands[T]) CreateStatusCmd() {
Use: "status",
Short: "Status of alert",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("CreateStatusCmd.consoler running with args=", args)
logus.Debug("CreateStatusCmd.consoler running with args=", logus.Args(args))
integer, err := t.cfgTags.Status(t.GetChannelID())
if err != nil {
errMsg := err.Error()
Expand Down Expand Up @@ -114,13 +118,13 @@ func (t *AlertBoolCommands[T]) CreateEnableCmd() {
Use: "enable",
Short: "Enable alert",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("CreateEnableCmd.consoler running with args=", args)
logus.Debug("CreateEnableCmd.consoler running with args=", logus.Args(args))
err := t.cfgTags.Enable(t.GetChannelID())
if err != nil {
cmd.OutOrStdout().Write([]byte("ERR msg=" + err.Error()))
return
}
fmt.Println(len(args))
logus.Debug("Create Enable is finished", logus.Args(args))

printer.Println(cmd, "OK alert is enabled")
},
Expand All @@ -133,7 +137,7 @@ func (t *AlertBoolCommands[T]) CreateDisableCmd() {
Use: "disable",
Short: "Disable alert / Clear alert",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("CreateDisableCmd.consoler running with args=", args)
logus.Debug("CreateDisableCmd.consoler running with args=", logus.Args(args))
err := t.cfgTags.Disable(t.GetChannelID())
if err != nil {
cmd.OutOrStdout().Write([]byte("ERR msg=" + err.Error()))
Expand All @@ -150,7 +154,7 @@ func (t *AlertBoolCommands[T]) CreateStatusCmd() {
Use: "status",
Short: "Status of alert",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("CreateStatusCmd.consoler running with args=", args)
logus.Debug("CreateStatusCmd.consoler running with args=", logus.Args(args))
_, err := t.cfgTags.Status(t.GetChannelID())
if err != nil {
errMsg := err.Error()
Expand Down Expand Up @@ -194,14 +198,14 @@ func (t *AlertSetStringCommand[T]) CreateSetCmd() {
Short: "Set Value (provide 'set StringValue')",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("CreateSetAlertCmd.consoler running with args=", args)
logus.Debug("CreateSetAlertCmd.consoler running with args=", logus.Args(args))
str := args[0]
err := t.cfgTags.Set(t.GetChannelID(), str)
if err != nil {
cmd.OutOrStdout().Write([]byte("ERR msg=" + err.Error()))
return
}
fmt.Println(len(args))
logus.Debug("finished CreateSetCmd", logus.Args(args))

printer.Println(cmd, "OK value is set")
},
Expand All @@ -214,7 +218,7 @@ func (t *AlertSetStringCommand[T]) CreateUnsetCmd() {
Use: "unset",
Short: "Unsert / Clear ",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("CreateUnsetCmd.consoler running with args=", args)
logus.Debug("CreateUnsetCmd.consoler running with args=", logus.Args(args))
err := t.cfgTags.Unset(t.GetChannelID())
if err != nil {
cmd.OutOrStdout().Write([]byte("ERR msg=" + err.Error()))
Expand All @@ -231,7 +235,7 @@ func (t *AlertSetStringCommand[T]) CreateStatusCmd() {
Use: "status",
Short: "Status",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("CreateStatusCmd.consoler running with args=", args)
logus.Debug("CreateStatusCmd.consoler running with args=", logus.Args(args))
str, err := t.cfgTags.Status(t.GetChannelID())
if err != nil {
errMsg := err.Error()
Expand Down
2 changes: 1 addition & 1 deletion app/consoler/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func (r *rootCommands) CreatePing() {
Use: "ping",
Short: "Check stuff is working",
Run: func(cmd *cobra.Command, args []string) {
logus.Debug("ping called with args=", logus.Items(args, "args"))
logus.Debug("ping called with args=", logus.Args(args))
cmd.OutOrStdout().Write([]byte("Pong! from consoler"))
},
}
Expand Down
13 changes: 7 additions & 6 deletions app/consoler/commands/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"darkbot/app/configurator"
"darkbot/app/consoler/commands/cmdgroup"
"darkbot/app/consoler/printer"
"darkbot/app/settings/logus"
"darkbot/app/settings/types"
"fmt"
"strings"
Expand Down Expand Up @@ -31,13 +32,13 @@ func (t *tagCommands) CreateTagAdd() {
Short: "Add tags",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("CreateTagAdd.consoler running with args=", args)
logus.Debug("CreateTagAdd.consoler running with args=", logus.Args(args))
err := t.cfgTags.TagsAdd(t.GetChannelID(), types.Tag(strings.Join(args, " ")))
if err != nil {
cmd.OutOrStdout().Write([]byte("ERR msg=" + err.Error()))
return
}
fmt.Println(len(args))
logus.Debug("CreateTagAdd", logus.Args(args))

printer.Println(cmd, "OK tags are added")
},
Expand All @@ -51,7 +52,7 @@ func (t *tagCommands) CreateTagRemove() {
Short: "Remove tags",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("CreateTagRemove.consoler running with args=", args)
logus.Debug("CreateTagRemove.consoler running with args=", logus.Args(args))
err := t.cfgTags.TagsRemove(t.GetChannelID(), types.Tag(strings.Join(args, " ")))
if err != nil {
cmd.OutOrStdout().Write([]byte("ERR msg=" + err.Error()))
Expand All @@ -69,7 +70,7 @@ func (t *tagCommands) CreateTagClear() {
Use: "clear",
Short: "Clear tags",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("CreateTagClear.consoler running with args=", args)
logus.Debug("CreateTagClear.consoler running with args=", logus.Args(args))
err := t.cfgTags.TagsClear(t.GetChannelID())
if err != nil {
cmd.OutOrStdout().Write([]byte("ERR msg=" + err.Error()))
Expand All @@ -87,15 +88,15 @@ func (t *tagCommands) CreateTagList() {
Use: "list",
Short: "List tags",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("CreateTagList.consoler running with args=", args)
logus.Debug("CreateTagList.consoler running with args=", logus.Args(args))
tags, cfgErr := t.cfgTags.TagsList(t.GetChannelID())
err := cfgErr
if err != nil {
cmd.OutOrStdout().Write([]byte("ERR msg=" + err.Error()))
return
}

fmt.Println("tags=", tags)
logus.Debug("CreateTagList continuied", logus.Tags(tags))
var sb strings.Builder
for number, tag := range tags {
sb.WriteString(fmt.Sprintf("\"%s\"", tag))
Expand Down
2 changes: 1 addition & 1 deletion app/management/anounce.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var amounceCmd = &cobra.Command{
Use: "anounce",
Short: "Anounce something",
Run: func(cmd *cobra.Command, args []string) {
logus.Info("Anounce is called with args=", logus.Items(args, "args"))
logus.Info("Anounce is called with args=", logus.Args(args))
dg := discorder.NewClient()

// go listener.Run()
Expand Down
7 changes: 0 additions & 7 deletions app/management/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@ var checkCmd = &cobra.Command{
Short: "Experimental command",
Run: func(cmd *cobra.Command, args []string) {
logus.Info("check called")
// fmt.Println("result=", consoler.Consoler{}.New("").Execute().String())

// db := configurator.GetConnection()
// channel1 := configurator.Channel{gorm.Model{ID: 123}, ChannelID: "123"}
// db.FirstOrCreate(&channel1)
// fmt.Println(channel1)

dg := discorder.NewClient()
// dg.SengMessage("838802002582175756", "123message")
dg.GetLatestMessages("838802002582175756")
Expand Down
3 changes: 1 addition & 2 deletions app/scrappy/base/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package base

import (
"darkbot/app/settings/logus"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -20,7 +19,7 @@ func TestGetBases(t *testing.T) {
logus.CheckFatal(err, "not found latest base record")

assert.True(t, len(bases.List) > 0)
fmt.Println(bases.List)
logus.Debug("", logus.Items(bases.List, "bases.List"))
}

func TestAddManyRecords(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions app/scrappy/baseattack/api_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package baseattack

import (
"fmt"
"darkbot/app/settings/logus"
"strings"
"testing"

Expand All @@ -12,7 +12,7 @@ func TestAPI(t *testing.T) {
api := NewBaseAttackAPI()
result, _ := api.GetBaseAttackData()
data := string(result)
fmt.Println(data)
logus.Debug(data)
}

func TestDetectLPAttack(t *testing.T) {
Expand Down
3 changes: 1 addition & 2 deletions app/scrappy/player/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package player

import (
"darkbot/app/settings/logus"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -20,5 +19,5 @@ func TestGetPlayers(t *testing.T) {
logus.CheckFatal(err, "not found latest base record")

assert.True(t, len(bases.List) > 0)
fmt.Println(bases.List)
logus.Debug("", logus.Items(bases.List, "bases.List"))
}
18 changes: 18 additions & 0 deletions app/settings/logus/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"darkbot/app/settings/types"
"fmt"
"log/slog"

"gorm.io/gorm"
)

func logGroupFiles() slog.Attr {
Expand Down Expand Up @@ -85,6 +87,14 @@ func Records[T any](value []T) slogParam {
return Items[T](value, "records")
}

func Args(value []string) slogParam {
return Items[string](value, "args")
}

func Tags(value []types.Tag) slogParam {
return Items[types.Tag](value, "tags")
}

func APIUrl(value types.APIurl) slogParam {
return func(c *slogGroup) {
c.params["api_url"] = string(value)
Expand Down Expand Up @@ -151,3 +161,11 @@ func Tag(value types.Tag) slogParam {
c.params["tag"] = string(value)
}
}

func GormResult(result *gorm.DB) slogParam {
return func(c *slogGroup) {
c.params["result.rows_affected"] = fmt.Sprintf("%d", result.RowsAffected)
c.params["result.error_msg"] = fmt.Sprintf("%v", result.Error)
c.params["result.error_type"] = fmt.Sprintf("%T", result.Error)
}
}
2 changes: 1 addition & 1 deletion app/settings/utils/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func ShellRunArgs(program string, args ...string) {
fmt.Printf("OK attempting to run: %s %s\n", program, args)
logus.Debug(fmt.Sprintf("OK attempting to run: %s", program), logus.Args(args))
executable, _ := exec.LookPath(program)

args = append([]string{""}, args...)
Expand Down
2 changes: 1 addition & 1 deletion app/viewer/templ/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func TestBaseViewerRealData(t *testing.T) {

base := NewTemplateBase(channelID, dbpath)
base.Render()
fmt.Println(base.main.Content)
logus.Debug("base.main.Content=" + base.main.Content)
})
}

Expand Down
Loading

0 comments on commit 4e61410

Please sign in to comment.