From 1879ddfa7991f44bd54d3de9d14d7b7c03472c78 Mon Sep 17 00:00:00 2001 From: garethgeorge Date: Tue, 20 Aug 2024 21:40:37 -0700 Subject: [PATCH] fix: misc logging improvements --- internal/hook/types/discord.go | 15 ++++++++++----- internal/hook/types/gotify.go | 14 ++++++-------- internal/hook/types/shoutrrr.go | 8 +++++--- internal/hook/types/slack.go | 16 +++++++++++----- 4 files changed, 32 insertions(+), 21 deletions(-) diff --git a/internal/hook/types/discord.go b/internal/hook/types/discord.go index 1c755ff4..584da8f4 100644 --- a/internal/hook/types/discord.go +++ b/internal/hook/types/discord.go @@ -10,6 +10,7 @@ import ( v1 "github.com/garethgeorge/backrest/gen/go/v1" "github.com/garethgeorge/backrest/internal/hook/hookutil" "github.com/garethgeorge/backrest/internal/orchestrator/tasks" + "go.uber.org/zap" ) type discordHandler struct{} @@ -24,9 +25,9 @@ func (discordHandler) Execute(ctx context.Context, h *v1.Hook, vars interface{}, return fmt.Errorf("template rendering: %w", err) } - writer := runner.RawLogWriter(ctx) - fmt.Fprintf(writer, "Sending discord message to %s\n", h.GetActionDiscord().GetWebhookUrl()) - fmt.Fprintf(writer, "---- payload ----\n%s\n", payload) + l := runner.Logger(ctx) + l.Sugar().Infof("Sending discord message to %s", h.GetActionDiscord().GetWebhookUrl()) + l.Debug("Sending discord message", zap.String("payload", payload)) type Message struct { Content string `json:"content"` @@ -37,8 +38,12 @@ func (discordHandler) Execute(ctx context.Context, h *v1.Hook, vars interface{}, } requestBytes, _ := json.Marshal(request) - _, err = hookutil.PostRequest(h.GetActionDiscord().GetWebhookUrl(), "application/json", bytes.NewReader(requestBytes)) - return err + body, err := hookutil.PostRequest(h.GetActionDiscord().GetWebhookUrl(), "application/json", bytes.NewReader(requestBytes)) + if err != nil { + return fmt.Errorf("sending discord message to %q: %w", h.GetActionDiscord().GetWebhookUrl(), err) + } + zap.S().Debug("Discord response", zap.String("body", body)) + return nil } func (discordHandler) ActionType() reflect.Type { diff --git a/internal/hook/types/gotify.go b/internal/hook/types/gotify.go index 54ce0e04..e14f59ba 100644 --- a/internal/hook/types/gotify.go +++ b/internal/hook/types/gotify.go @@ -12,6 +12,7 @@ import ( v1 "github.com/garethgeorge/backrest/gen/go/v1" "github.com/garethgeorge/backrest/internal/hook/hookutil" "github.com/garethgeorge/backrest/internal/orchestrator/tasks" + "go.uber.org/zap" ) type gotifyHandler struct{} @@ -33,7 +34,7 @@ func (gotifyHandler) Execute(ctx context.Context, h *v1.Hook, vars interface{}, return fmt.Errorf("title template rendering: %w", err) } - output := runner.RawLogWriter(ctx) + l := runner.Logger(ctx) message := struct { Message string `json:"message"` @@ -45,6 +46,9 @@ func (gotifyHandler) Execute(ctx context.Context, h *v1.Hook, vars interface{}, Message: payload, } + l.Sugar().Infof("Sending gotify message to %s", g.GetBaseUrl()) + l.Debug("Sending gotify message", zap.Any("message", message)) + b, err := json.Marshal(message) if err != nil { return fmt.Errorf("json marshal: %w", err) @@ -57,19 +61,13 @@ func (gotifyHandler) Execute(ctx context.Context, h *v1.Hook, vars interface{}, baseUrl, url.QueryEscape(g.GetToken())) - fmt.Fprintf(output, "Sending gotify message to %s\n", postUrl) - fmt.Fprintf(output, "---- payload ----\n") - output.Write(b) - body, err := hookutil.PostRequest(postUrl, "application/json", bytes.NewReader(b)) if err != nil { return fmt.Errorf("send gotify message: %w", err) } - if body != "" { - output.Write([]byte(body)) - } + l.Sugar().Debugf("Gotify response: %s", body) return nil } diff --git a/internal/hook/types/shoutrrr.go b/internal/hook/types/shoutrrr.go index 98eddfab..eba79b30 100644 --- a/internal/hook/types/shoutrrr.go +++ b/internal/hook/types/shoutrrr.go @@ -9,6 +9,7 @@ import ( v1 "github.com/garethgeorge/backrest/gen/go/v1" "github.com/garethgeorge/backrest/internal/hook/hookutil" "github.com/garethgeorge/backrest/internal/orchestrator/tasks" + "go.uber.org/zap" ) type shoutrrrHandler struct{} @@ -23,9 +24,10 @@ func (shoutrrrHandler) Execute(ctx context.Context, h *v1.Hook, vars interface{} return fmt.Errorf("template rendering: %w", err) } - writer := runner.RawLogWriter(ctx) - fmt.Fprintf(writer, "Sending shoutrrr message to %s\n", h.GetActionShoutrrr().GetShoutrrrUrl()) - fmt.Fprintf(writer, "---- payload ----\n%s\n", payload) + l := runner.Logger(ctx) + + l.Sugar().Infof("Sending shoutrrr message to %s", h.GetActionShoutrrr().GetShoutrrrUrl()) + l.Debug("Sending shoutrrr message", zap.String("payload", payload)) if err := shoutrrr.Send(h.GetActionShoutrrr().GetShoutrrrUrl(), payload); err != nil { return fmt.Errorf("sending shoutrrr message to %q: %w", h.GetActionShoutrrr().GetShoutrrrUrl(), err) diff --git a/internal/hook/types/slack.go b/internal/hook/types/slack.go index 995aa6bb..ccfacf70 100644 --- a/internal/hook/types/slack.go +++ b/internal/hook/types/slack.go @@ -10,6 +10,7 @@ import ( v1 "github.com/garethgeorge/backrest/gen/go/v1" "github.com/garethgeorge/backrest/internal/hook/hookutil" "github.com/garethgeorge/backrest/internal/orchestrator/tasks" + "go.uber.org/zap" ) type slackHandler struct{} @@ -24,9 +25,9 @@ func (slackHandler) Execute(ctx context.Context, cmd *v1.Hook, vars interface{}, return fmt.Errorf("template rendering: %w", err) } - writer := runner.RawLogWriter(ctx) - fmt.Fprintf(writer, "Sending slack message to %s\n", cmd.GetActionSlack().GetWebhookUrl()) - fmt.Fprintf(writer, "---- payload ----\n%s\n", payload) + l := runner.Logger(ctx) + l.Sugar().Infof("Sending slack message to %s", cmd.GetActionSlack().GetWebhookUrl()) + l.Debug("Sending slack message", zap.String("payload", payload)) type Message struct { Text string `json:"text"` @@ -38,8 +39,13 @@ func (slackHandler) Execute(ctx context.Context, cmd *v1.Hook, vars interface{}, requestBytes, _ := json.Marshal(request) - _, err = hookutil.PostRequest(cmd.GetActionSlack().GetWebhookUrl(), "application/json", bytes.NewReader(requestBytes)) - return err + body, err := hookutil.PostRequest(cmd.GetActionSlack().GetWebhookUrl(), "application/json", bytes.NewReader(requestBytes)) + if err != nil { + return fmt.Errorf("sending slack message to %q: %w", cmd.GetActionSlack().GetWebhookUrl(), err) + } + + l.Debug("Slack response", zap.String("body", body)) + return nil } func (slackHandler) ActionType() reflect.Type {