From 51fd83796504760f5367a7490dc65dcef913a688 Mon Sep 17 00:00:00 2001 From: Austin Ziegler Date: Mon, 15 Apr 2019 22:06:55 -0400 Subject: [PATCH 1/3] Allow configuration of Sentry log level Fixes #304 --- .gitignore | 1 + README.md | 1 + lib/sentry.ex | 13 ++++++++++++- lib/sentry/client.ex | 9 ++++++--- lib/sentry/config.ex | 15 +++++++++++++++ 5 files changed, 35 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index b58f2abf..035ce58c 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ erl_crash.dump *.ez /doc .DS_Store +.elixir_ls/ diff --git a/README.md b/README.md index 6b7c61c7..46d62c61 100644 --- a/README.md +++ b/README.md @@ -118,6 +118,7 @@ For optional settings check the [docs](https://hexdocs.pm/sentry/readme.html). | `source_code_path_pattern` | False | `"**/*.ex"` | | | `filter` | False | | Module where the filter rules are defined (see [Filtering Exceptions](https://hexdocs.pm/sentry/Sentry.html#module-filtering-exceptions)) | | `json_library` | False | `Jason` | | +| `log_level` | False | `:warn` | | An example production config might look like this: diff --git a/lib/sentry.ex b/lib/sentry.ex index 0d23c5d8..11aaa5aa 100644 --- a/lib/sentry.ex +++ b/lib/sentry.ex @@ -104,6 +104,7 @@ defmodule Sentry do ] validate_json_config!() + validate_log_level_config!() opts = [strategy: :one_for_one, name: Sentry.Supervisor] Supervisor.start_link(children, opts) @@ -149,7 +150,7 @@ defmodule Sentry do def send_event(event, opts \\ []) def send_event(%Event{message: nil, exception: nil}, _opts) do - Logger.warn("Sentry: unable to parse exception") + Logger.log(Config.log_level(), "Sentry: unable to parse exception") :ignored end @@ -192,4 +193,14 @@ defmodule Sentry do end end end + + defp validate_log_level_config!() do + value = Config.log_level() + + if value in Config.permitted_log_level_values() do + :ok + else + raise ArgumentError.exception("#{inspect(value)} is not a valid :log_level configuration") + end + end end diff --git a/lib/sentry/client.ex b/lib/sentry/client.ex index bea228b6..c8fb0974 100644 --- a/lib/sentry/client.ex +++ b/lib/sentry/client.ex @@ -336,9 +336,12 @@ defmodule Sentry.Client do end defp log_api_error(body) do - Logger.warn(fn -> - ["Failed to send Sentry event.", ?\n, body] - end) + Logger.log( + Config.log_level(), + fn -> + ["Failed to send Sentry event.", ?\n, body] + end + ) end defp sleep(1), do: :timer.sleep(2000) diff --git a/lib/sentry/config.ex b/lib/sentry/config.ex index 82b5cdc0..7d5afc71 100644 --- a/lib/sentry/config.ex +++ b/lib/sentry/config.ex @@ -14,6 +14,11 @@ defmodule Sentry.Config do @default_context_lines 3 @default_sample_rate 1.0 + @permitted_log_level_atom_values ~w(debug info warn error)a + @permitted_log_level_string_values ~w(debug info warn error) + @permitted_log_level_values @permitted_log_level_atom_values ++ + @permitted_log_level_string_values + def validate_config! do end @@ -127,6 +132,16 @@ defmodule Sentry.Config do get_config(:json_library, default: Jason, check_dsn: false) end + def log_level do + case get_config(:log_level, default: :warn, check_dsn: false) do + value when value in @permitted_log_level_atom_values -> value + value when value in @permitted_log_level_string_values -> String.to_existing_atom(value) + value -> value + end + end + + def permitted_log_level_values, do: @permitted_log_level_values + defp get_config(key, opts \\ []) when is_atom(key) do default = Keyword.get(opts, :default) check_dsn = Keyword.get(opts, :check_dsn, true) From 37e3cc632794896c48bab0e5af59314c7c2f5f88 Mon Sep 17 00:00:00 2001 From: Austin Ziegler Date: Tue, 16 Apr 2019 14:39:53 -0400 Subject: [PATCH 2/3] Address requested review changes. --- .gitignore | 2 -- README.md | 2 +- lib/sentry/config.ex | 8 ++------ 3 files changed, 3 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 035ce58c..12d32ce7 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,3 @@ erl_crash.dump *.ez /doc -.DS_Store -.elixir_ls/ diff --git a/README.md b/README.md index 46d62c61..da512e5c 100644 --- a/README.md +++ b/README.md @@ -118,7 +118,7 @@ For optional settings check the [docs](https://hexdocs.pm/sentry/readme.html). | `source_code_path_pattern` | False | `"**/*.ex"` | | | `filter` | False | | Module where the filter rules are defined (see [Filtering Exceptions](https://hexdocs.pm/sentry/Sentry.html#module-filtering-exceptions)) | | `json_library` | False | `Jason` | | -| `log_level` | False | `:warn` | | +| `log_level` | False | `:warn` | This sets the log level used when Sentry fails to send an event due to an invalid event or API error. | An example production config might look like this: diff --git a/lib/sentry/config.ex b/lib/sentry/config.ex index 7d5afc71..f6a73cc0 100644 --- a/lib/sentry/config.ex +++ b/lib/sentry/config.ex @@ -14,10 +14,7 @@ defmodule Sentry.Config do @default_context_lines 3 @default_sample_rate 1.0 - @permitted_log_level_atom_values ~w(debug info warn error)a - @permitted_log_level_string_values ~w(debug info warn error) - @permitted_log_level_values @permitted_log_level_atom_values ++ - @permitted_log_level_string_values + @permitted_log_level_values ~w(debug info warn error)a def validate_config! do end @@ -134,8 +131,7 @@ defmodule Sentry.Config do def log_level do case get_config(:log_level, default: :warn, check_dsn: false) do - value when value in @permitted_log_level_atom_values -> value - value when value in @permitted_log_level_string_values -> String.to_existing_atom(value) + value when value in @permitted_log_level_values -> value value -> value end end From f4e42982fcea86ef6bb49727433466edd91912b4 Mon Sep 17 00:00:00 2001 From: Austin Ziegler Date: Tue, 16 Apr 2019 21:06:49 -0400 Subject: [PATCH 3/3] Without strings, simplify get_config call --- lib/sentry/config.ex | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/sentry/config.ex b/lib/sentry/config.ex index f6a73cc0..0e1b3809 100644 --- a/lib/sentry/config.ex +++ b/lib/sentry/config.ex @@ -130,10 +130,7 @@ defmodule Sentry.Config do end def log_level do - case get_config(:log_level, default: :warn, check_dsn: false) do - value when value in @permitted_log_level_values -> value - value -> value - end + get_config(:log_level, default: :warn, check_dsn: false) end def permitted_log_level_values, do: @permitted_log_level_values