remove tesla from logger

This commit is contained in:
Owen
2024-12-18 12:00:52 +01:00
parent 32b5473222
commit cab8b052bf
3 changed files with 21 additions and 13 deletions
+1 -2
View File
@@ -26,8 +26,7 @@ defp deps do
{:image, "~> 0.55"}, {:image, "~> 0.55"},
{:jason, "~> 1.4"}, {:jason, "~> 1.4"},
{:plug_cowboy, "~> 2.7"}, {:plug_cowboy, "~> 2.7"},
{:quantum, "~> 3.5"}, {:quantum, "~> 3.5"}
{:tesla, "~> 1.13"}
] ]
end end
``` ```
+18 -8
View File
@@ -1,8 +1,5 @@
defmodule PlantIdDiscordBot.DiscordLogger do defmodule PlantIdDiscordBot.DiscordLogger do
require Logger require Logger
use Tesla
plug(Tesla.Middleware.JSON)
@behaviour :gen_event @behaviour :gen_event
@@ -40,13 +37,26 @@ defmodule PlantIdDiscordBot.DiscordLogger do
def log_to_discord(webhook_url, level, msg, ts, md) do def log_to_discord(webhook_url, level, msg, ts, md) do
formatted_msg = format_message(level, msg, ts, md) formatted_msg = format_message(level, msg, ts, md)
body = %{content: formatted_msg} body = %{content: formatted_msg} |> Jason.encode!()
headers = [{~c"Content-Type", ~c"application/json"}]
post(webhook_url, body) :httpc.request(
:post,
{webhook_url, headers, ~c"application/json", body},
[],
[]
)
|> case do |> case do
{:ok, %{status: status}} when status in 200..299 -> :ok {:ok, {{~c"HTTP/1.1", status, _}, _headers, _body}} when status in 200..299 ->
{:error, reason} -> Logger.error("Error sending log to Discord: #{inspect(reason)}") :ok
_any -> Logger.error("Error sending log to Discord")
{:ok, {{~c"HTTP/1.1", status, _}, _headers, response_body}} ->
Logger.error(
"Failed to send log to Discord: Status #{status}, Body: #{inspect(response_body)}"
)
{:error, reason} ->
Logger.error("Error sending log to Discord: #{inspect(reason)}")
end end
end end
+2 -3
View File
@@ -14,7 +14,7 @@ defmodule PlantidDiscordBot.MixProject do
# Run "mix help compile.app" to learn about applications. # Run "mix help compile.app" to learn about applications.
def application do def application do
[ [
extra_applications: [:logger], extra_applications: [:logger, :observer, :wx, :runtime_tools],
mod: {PlantIdDiscordBot.Application, []} mod: {PlantIdDiscordBot.Application, []}
] ]
end end
@@ -28,8 +28,7 @@ defmodule PlantidDiscordBot.MixProject do
{:image, "~> 0.55"}, {:image, "~> 0.55"},
{:jason, "~> 1.4"}, {:jason, "~> 1.4"},
{:plug_cowboy, "~> 2.7"}, {:plug_cowboy, "~> 2.7"},
{:quantum, "~> 3.5"}, {:quantum, "~> 3.5"}
{:tesla, "~> 1.13"}
] ]
end end
end end