@@ -71,20 +71,14 @@ defmodule Logger.Config do
71
71
{ :ok , state }
72
72
end
73
73
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 ->
81
77
{ :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 }
84
80
persist ( state )
85
81
{ :ok , state }
86
- true ->
87
- { :ok , state }
88
82
end
89
83
end
90
84
@@ -121,6 +115,19 @@ defmodule Logger.Config do
121
115
122
116
## Helpers
123
117
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
+
124
131
defp update_backends ( fun ) do
125
132
backends = fun . ( Application . get_env ( :logger , :backends , [ ] ) )
126
133
Application . put_env ( :logger , :backends , backends )
@@ -141,9 +148,16 @@ defmodule Logger.Config do
141
148
sync_threshold = Application . get_env ( :logger , :sync_threshold )
142
149
async_threshold = trunc ( sync_threshold * 0.75 )
143
150
144
- persist % { level: level , mode: mode , truncate: truncate ,
151
+ state = % { level: level , mode: mode , truncate: truncate ,
145
152
utc_log: utc_log , sync_threshold: sync_threshold ,
146
153
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
147
161
end
148
162
149
163
defp persist ( state ) do
0 commit comments