@@ -263,18 +263,6 @@ defmodule Sentry.LoggerHandler do
263263 @ doc false
264264 @ spec adding_handler ( :logger . handler_config ( ) ) :: { :ok , :logger . handler_config ( ) }
265265 def adding_handler ( config ) do
266- # If a user explicitly registers their own Sentry.LoggerHandler (i.e., not the
267- # auto-registered :sentry_log_handler), remove the auto-registered one to prevent
268- # duplicate log capture. We spawn a separate process to do the removal because
269- # adding_handler/1 is called from within the logger gen_server process, and calling
270- # :logger.remove_handler/1 directly would deadlock.
271- if config . id != :sentry_log_handler do
272- _ = spawn ( fn -> :logger . remove_handler ( :sentry_log_handler ) end )
273- :ok
274- else
275- :ok
276- end
277-
278266 # The :config key may not be here.
279267 sentry_config = Map . get ( config , :config , % { } )
280268
@@ -294,12 +282,26 @@ defmodule Sentry.LoggerHandler do
294282
295283 config = Map . put ( config , :config , handler_config )
296284
297- if rate_limiting_config = config . config . rate_limiting do
298- _ = RateLimiter . start_under_sentry_supervisor ( config . id , rate_limiting_config )
299- { :ok , config }
300- else
301- { :ok , config }
285+ result =
286+ if rate_limiting_config = config . config . rate_limiting do
287+ _ = RateLimiter . start_under_sentry_supervisor ( config . id , rate_limiting_config )
288+ { :ok , config }
289+ else
290+ { :ok , config }
291+ end
292+
293+ # If a user explicitly registers their own Sentry.LoggerHandler (i.e., not the
294+ # auto-registered :sentry_log_handler), remove the auto-registered one to prevent
295+ # duplicate log capture. We do this after all validation so that if config is
296+ # invalid (and adding_handler raises), the auto-handler stays active. We spawn a
297+ # separate process because adding_handler/1 is called from within the logger
298+ # gen_server process, and calling :logger.remove_handler/1 directly would deadlock.
299+ if config . id != :sentry_log_handler do
300+ _ = spawn ( fn -> :logger . remove_handler ( :sentry_log_handler ) end )
301+ :ok
302302 end
303+
304+ result
303305 end
304306
305307 # Callback for :logger handlers
0 commit comments