Skip to content

Commit

Permalink
fix: misc logging improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
garethgeorge committed Aug 21, 2024
1 parent 80dbe91 commit 1879ddf
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 21 deletions.
15 changes: 10 additions & 5 deletions internal/hook/types/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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"`
Expand All @@ -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 {
Expand Down
14 changes: 6 additions & 8 deletions internal/hook/types/gotify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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"`
Expand All @@ -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)
Expand All @@ -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
}
Expand Down
8 changes: 5 additions & 3 deletions internal/hook/types/shoutrrr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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)
Expand Down
16 changes: 11 additions & 5 deletions internal/hook/types/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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"`
Expand All @@ -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 {
Expand Down

0 comments on commit 1879ddf

Please sign in to comment.