mirror of
https://github.com/watn3y/steamsalty.git
synced 2025-09-08 01:02:24 +02:00
refactor: Standardized logging to be more uniform
This commit is contained in:
parent
99a81a201b
commit
38db97bdcf
8 changed files with 28 additions and 28 deletions
6
bot.go
6
bot.go
|
@ -18,10 +18,10 @@ func bot() {
|
|||
go steam.StartWatchers(bot)
|
||||
|
||||
for update := range updates {
|
||||
log.Debug().Interface("update", update).Msg("Received update")
|
||||
log.Debug().Interface("update", update).Msg("Update received")
|
||||
|
||||
if update.Message == nil || update.Message.Text == "" {
|
||||
log.Debug().Int("UpdateID", update.UpdateID).Msg("Unable to parse update")
|
||||
log.Debug().Int("UpdateID", update.UpdateID).Msg("Failed to parse update")
|
||||
continue
|
||||
}
|
||||
if update.Message.Time().UTC().Unix() < time.Now().UTC().Unix() {
|
||||
|
@ -30,7 +30,7 @@ func bot() {
|
|||
}
|
||||
|
||||
if update.Message.IsCommand() {
|
||||
log.Info().Int64("ChatID", update.Message.Chat.ID).Int64("UserID", update.Message.From.ID).Str("Text", update.Message.Text).Msg("Received Command")
|
||||
log.Info().Int64("ChatID", update.Message.Chat.ID).Int64("UserID", update.Message.From.ID).Str("Text", update.Message.Text).Msg("Command received")
|
||||
commands.Commands(update, bot)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,7 +9,7 @@ import (
|
|||
func Authenticate() (tgbotapi.UpdatesChannel, *tgbotapi.BotAPI) {
|
||||
bot, err := tgbotapi.NewBotAPI(config.BotConfig.TelegramAPIToken)
|
||||
if err != nil {
|
||||
log.Panic().Err(err).Msg("Failed to authenticate")
|
||||
log.Panic().Err(err).Msg("Failed to authenticate to Telegram")
|
||||
}
|
||||
|
||||
bot.Debug = false
|
||||
|
@ -20,7 +20,7 @@ func Authenticate() (tgbotapi.UpdatesChannel, *tgbotapi.BotAPI) {
|
|||
updates := tgbotapi.NewUpdate(0)
|
||||
updates.Timeout = 60
|
||||
|
||||
log.Info().Int64("ID", bot.Self.ID).Str("username", bot.Self.UserName).Msg("Authenticated to Telegram API")
|
||||
log.Info().Int64("ID", bot.Self.ID).Str("username", bot.Self.UserName).Msg("Authenticated to Telegram successfully")
|
||||
|
||||
return bot.GetUpdatesChan(updates), bot
|
||||
|
||||
|
|
|
@ -12,7 +12,7 @@ func SendMessage(message tgbotapi.MessageConfig, bot *tgbotapi.BotAPI) (result t
|
|||
return
|
||||
}
|
||||
|
||||
log.Info().Int64("chat", result.Chat.ID).Str("msg", result.Text).Msg("Sent message")
|
||||
log.Info().Int64("chat", result.Chat.ID).Str("msg", result.Text).Msg("Sent message successfully")
|
||||
log.Debug().Interface("msg", result).Msg("")
|
||||
|
||||
return result
|
||||
|
@ -25,7 +25,7 @@ func EditMessage(message tgbotapi.EditMessageTextConfig, bot *tgbotapi.BotAPI) (
|
|||
return
|
||||
}
|
||||
|
||||
log.Info().Int64("chat", result.Chat.ID).Str("msg", result.Text).Msg("Edited message")
|
||||
log.Info().Int64("chat", result.Chat.ID).Str("msg", result.Text).Msg("Edited message successfully")
|
||||
log.Debug().Interface("msg", result).Msg("")
|
||||
|
||||
return result
|
||||
|
@ -38,7 +38,7 @@ func SendVideo(message tgbotapi.VideoConfig, bot *tgbotapi.BotAPI) (result tgbot
|
|||
return
|
||||
}
|
||||
|
||||
log.Info().Int64("chat", result.Chat.ID).Msg("Sent video")
|
||||
log.Info().Int64("chat", result.Chat.ID).Msg("Sent video successfully")
|
||||
log.Debug().Interface("video", result).Msg("")
|
||||
|
||||
return result
|
||||
|
@ -51,7 +51,7 @@ func SendPhoto(message tgbotapi.PhotoConfig, bot *tgbotapi.BotAPI) (result tgbot
|
|||
return
|
||||
}
|
||||
|
||||
log.Info().Int64("chat", result.Chat.ID).Msg("Sent photo")
|
||||
log.Info().Int64("chat", result.Chat.ID).Msg("Sent photo successfully")
|
||||
log.Debug().Interface("photo", result).Msg("")
|
||||
|
||||
return result
|
||||
|
@ -64,7 +64,7 @@ func SendSticker(message tgbotapi.StickerConfig, bot *tgbotapi.BotAPI) (result t
|
|||
return
|
||||
}
|
||||
|
||||
log.Info().Int64("chat", result.Chat.ID).Msg("Sent sticker")
|
||||
log.Info().Int64("chat", result.Chat.ID).Msg("Sent sticker successfully")
|
||||
log.Debug().Interface("sticker", result).Msg("")
|
||||
|
||||
return result
|
||||
|
|
|
@ -22,11 +22,11 @@ func SetBotCommands(bot *tgbotapi.BotAPI) {
|
|||
|
||||
result, err := bot.Request(commands)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Failed to set own commands")
|
||||
log.Error().Err(err).Msg("Failed to publish commands to Telegram")
|
||||
return
|
||||
}
|
||||
|
||||
log.Debug().Interface("commands", result).Msg("Set own commands")
|
||||
log.Debug().Interface("commands", result).Msg("Published commands to Telegram successfully")
|
||||
}
|
||||
|
||||
func Commands(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
|
||||
|
|
2
main.go
2
main.go
|
@ -38,6 +38,6 @@ func configureLogger() {
|
|||
output := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.DateTime}
|
||||
|
||||
log.Logger = zerolog.New(output).With().Timestamp().Caller().Logger()
|
||||
log.Info().Msg("Started Logger")
|
||||
log.Info().Msg("Logger started successfully")
|
||||
|
||||
}
|
||||
|
|
|
@ -11,8 +11,8 @@ func GetPlayerDetails(steamID uint64) (summary steamapi.PlayerSummary) {
|
|||
|
||||
response, err := steamapi.GetPlayerSummaries([]uint64{steamID}, config.BotConfig.SteamAPIKey)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Failed to get Player Summary")
|
||||
log.Error().Err(err).Msg("Failed to retrive player summary")
|
||||
}
|
||||
log.Debug().Interface("Player", response[0]).Msg("Got PlayerSummary from Steam API")
|
||||
log.Debug().Interface("Player", response[0]).Msg("Retrived player summary from SteamAPI successfully")
|
||||
return response[0]
|
||||
}
|
||||
|
|
|
@ -19,7 +19,7 @@ func GetComments(steamID uint64, start int, count int) (page CommentsPage) {
|
|||
|
||||
url, err := url.Parse(baseURL + strconv.FormatUint(steamID, 10))
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Unable to Parse SteamID into URL")
|
||||
log.Error().Err(err).Msg("Failed to parse SteamID into URL")
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -30,7 +30,7 @@ func GetComments(steamID uint64, start int, count int) (page CommentsPage) {
|
|||
|
||||
resp, err := http.Get(url.String())
|
||||
if err != nil || resp.StatusCode != http.StatusOK {
|
||||
log.Error().Err(err).Int("Response Code", resp.StatusCode).Msg("Failed to get Comments")
|
||||
log.Error().Err(err).Int("Response Code", resp.StatusCode).Msg("Failed to retrieve comments")
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
@ -38,16 +38,16 @@ func GetComments(steamID uint64, start int, count int) (page CommentsPage) {
|
|||
// Read the response body
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Failed to parse Comments")
|
||||
log.Error().Err(err).Msg("Failed to parse comments")
|
||||
log.Trace().Interface("Body", resp.Body)
|
||||
}
|
||||
|
||||
err = json.Unmarshal(body, &page)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Failed to parse Comments as JSON")
|
||||
log.Error().Err(err).Msg("Failed to parse comments as JSON")
|
||||
}
|
||||
|
||||
log.Trace().Interface("CommentPage", page).Uint64("ProfileID", steamID).Msg("Got Comment Page")
|
||||
log.Trace().Interface("CommentPage", page).Uint64("ProfileID", steamID).Msg("Retrieved Comment Page successfully")
|
||||
|
||||
return page
|
||||
}
|
||||
|
@ -56,7 +56,7 @@ func parseComments(rawComments CommentsPage) (comments []Comment) {
|
|||
|
||||
doc, err := goquery.NewDocumentFromReader(strings.NewReader(rawComments.CommentsHTML))
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Error while parsing CommentsHTML")
|
||||
log.Error().Err(err).Msg("Failed to parse CommentsHTML")
|
||||
return
|
||||
}
|
||||
doc.Find(".commentthread_comment.responsive_body_text").Each(func(i int, s *goquery.Selection) {
|
||||
|
@ -64,14 +64,14 @@ func parseComments(rawComments CommentsPage) (comments []Comment) {
|
|||
|
||||
parsedID, err := strconv.ParseUint(strings.TrimPrefix(s.AttrOr("id", ""), "comment_"), 10, 64)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Error while parsing Comment ID")
|
||||
log.Error().Err(err).Msg("Failed to parse Comment ID")
|
||||
return
|
||||
}
|
||||
c.ID = parsedID
|
||||
|
||||
c.Timestamp, err = strconv.ParseInt(s.Find(".commentthread_comment_timestamp").AttrOr("data-timestamp", ""), 10, 64)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Error while parsing Comment Timestamp")
|
||||
log.Error().Err(err).Msg("Failed to parse Comment Timestamp")
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -85,6 +85,6 @@ func parseComments(rawComments CommentsPage) (comments []Comment) {
|
|||
})
|
||||
|
||||
slices.Reverse(comments)
|
||||
log.Trace().Interface("Comments", comments).Msg("Parsed Comment Page")
|
||||
log.Trace().Interface("Comments", comments).Msg("Parsed comment page successfully")
|
||||
return comments
|
||||
}
|
||||
|
|
|
@ -33,7 +33,7 @@ func StartWatchers(bot *tgbotapi.BotAPI) {
|
|||
}
|
||||
|
||||
func watcher(bot *tgbotapi.BotAPI, steamID uint64, sleeptime time.Duration) {
|
||||
log.Info().Uint64("SteamID", steamID).Msg("Started Watcher")
|
||||
log.Info().Uint64("SteamID", steamID).Msg("Starting Watcher")
|
||||
|
||||
var newestProcessedComment int64 = 0
|
||||
|
||||
|
@ -56,9 +56,9 @@ func watcher(bot *tgbotapi.BotAPI, steamID uint64, sleeptime time.Duration) {
|
|||
profileOwner := GetPlayerDetails(steamID)
|
||||
|
||||
for _, comment := range parseComments(currentCommentsPage) {
|
||||
log.Debug().Interface("Comment", comment).Msg("Processing Comment")
|
||||
log.Debug().Interface("Comment", comment).Msg("Processing comment")
|
||||
if comment.Timestamp <= newestProcessedComment {
|
||||
log.Debug().Uint64("CommentID", comment.ID).Msg("Skipping Comment")
|
||||
log.Debug().Uint64("CommentID", comment.ID).Msg("Skipping comment")
|
||||
continue
|
||||
}
|
||||
|
||||
|
@ -69,7 +69,7 @@ func watcher(bot *tgbotapi.BotAPI, steamID uint64, sleeptime time.Duration) {
|
|||
Text: fmt.Sprintf(`<b><a href="%s">%s</a> just commented on <a href="%s">%s</a>'s profile:</b>`, comment.AuthorProfileURL, comment.Author, profileOwner.ProfileURL, profileOwner.PersonaName) + "\n" +
|
||||
"<blockquote>" + comment.Text + "</blockquote>",
|
||||
}
|
||||
log.Info().Interface("Comment", comment).Msg("Notifying about new Comment")
|
||||
log.Info().Interface("Comment", comment).Msg("Notifying about new comment")
|
||||
botIO.SendMessage(msg, bot)
|
||||
time.Sleep(time.Minute / 20)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue