@@ -71,20 +71,14 @@ defmodule Logger.Config do
7171 { :ok , state }
7272 end
7373
74- def handle_event ( _event , state ) do
75- { :message_queue_len , len } = Process . info ( self ( ) , :message_queue_len )
76-
77- cond do
78- len > state . sync_threshold and state . mode == :async ->
79- state = % { state | mode: :sync }
80- persist ( state )
74+ def handle_event ( _event , % { mode: mode } = state ) do
75+ case compute_mode ( state ) do
76+ ^ mode ->
8177 { :ok , state }
82- len < state . async_threshold and state . mode == :sync ->
83- state = % { state | mode: :async }
78+ new_mode ->
79+ state = % { state | mode: new_mode }
8480 persist ( state )
8581 { :ok , state }
86- true ->
87- { :ok , state }
8882 end
8983 end
9084
@@ -121,6 +115,19 @@ defmodule Logger.Config do
121115
122116 ## Helpers
123117
118+ defp compute_mode ( state ) do
119+ { :message_queue_len , len } = Process . info ( self ( ) , :message_queue_len )
120+
121+ cond do
122+ len > state . sync_threshold and state . mode == :async ->
123+ :sync
124+ len < state . async_threshold and state . mode == :sync ->
125+ :async
126+ true ->
127+ state . mode
128+ end
129+ end
130+
124131 defp update_backends ( fun ) do
125132 backends = fun . ( Application . get_env ( :logger , :backends , [ ] ) )
126133 Application . put_env ( :logger , :backends , backends )
@@ -141,9 +148,16 @@ defmodule Logger.Config do
141148 sync_threshold = Application . get_env ( :logger , :sync_threshold )
142149 async_threshold = trunc ( sync_threshold * 0.75 )
143150
144- persist % { level: level , mode: mode , truncate: truncate ,
151+ state = % { level: level , mode: mode , truncate: truncate ,
145152 utc_log: utc_log , sync_threshold: sync_threshold ,
146153 async_threshold: async_threshold , translators: translators }
154+
155+ case compute_mode ( state ) do
156+ ^ mode ->
157+ persist ( state )
158+ new_mode ->
159+ persist ( % { state | mode: new_mode } )
160+ end
147161 end
148162
149163 defp persist ( state ) do
0 commit comments