@@ -4,6 +4,7 @@ defmodule Logger.Config do
44 @ behaviour :gen_event
55 @ name __MODULE__
66 @ table __MODULE__
7+ @ check_discard { __MODULE__ , :check_discard }
78
89 def start_link do
910 GenServer . start_link ( __MODULE__ , :ok , name: @ name )
@@ -76,29 +77,7 @@ defmodule Logger.Config do
7677 end
7778
7879 def handle_event ( _event , { state , thresholds } ) do
79- % { mode: mode } = state
80-
81- case compute_mode ( mode , thresholds ) do
82- ^ mode ->
83- { :ok , { state , thresholds } }
84-
85- new_mode ->
86- if new_mode == :discard do
87- message =
88- "Logger has #{ message_queue_length ( ) } messages in its queue, " <>
89- "which is above :discard_threshold. Messages will be discarded " <>
90- "until the message queue goes back to 75% of the threshold size"
91-
92- log ( :warn , message , state )
93- end
94-
95- if mode == :discard do
96- log ( :warn , "Logger has stopped discarding messages" , state )
97- end
98-
99- state = persist ( % { state | mode: new_mode } )
100- { :ok , { state , thresholds } }
101- end
80+ { :ok , { compute_mode_and_persist_state ( state , thresholds ) , thresholds } }
10281 end
10382
10483 def handle_call ( { :configure , options } , { % { mode: mode } , _ } ) do
@@ -127,6 +106,16 @@ defmodule Logger.Config do
127106 { :ok , old , state }
128107 end
129108
109+ def handle_info ( @ check_discard , { % { mode: :discard } = state , thresholds } ) do
110+ state = compute_mode_and_persist_state ( state , thresholds )
111+
112+ if state . mode == :discard do
113+ Process . send_after ( self ( ) , @ check_discard , state . discard_threshold_periodic_check )
114+ end
115+
116+ { :ok , { state , thresholds } }
117+ end
118+
130119 def handle_info ( _msg , state ) do
131120 { :ok , state }
132121 end
@@ -139,6 +128,31 @@ defmodule Logger.Config do
139128 { :ok , state }
140129 end
141130
131+ defp compute_mode_and_persist_state ( % { mode: mode } = state , thresholds ) do
132+ case compute_mode ( mode , thresholds ) do
133+ ^ mode ->
134+ state
135+
136+ new_mode ->
137+ if new_mode == :discard do
138+ Process . send_after ( self ( ) , @ check_discard , state . discard_threshold_periodic_check )
139+
140+ message =
141+ "Logger has #{ message_queue_length ( ) } messages in its queue, " <>
142+ "which is above :discard_threshold. Messages will be discarded " <>
143+ "until the message queue goes back to 75% of the threshold size"
144+
145+ log ( :warn , message , state )
146+ end
147+
148+ if mode == :discard do
149+ log ( :warn , "Logger has stopped discarding messages" , state )
150+ end
151+
152+ persist ( % { state | mode: new_mode } )
153+ end
154+ end
155+
142156 ## Helpers
143157
144158 defp log ( level , message , state ) do
@@ -163,7 +177,9 @@ defmodule Logger.Config do
163177 level: Application . get_env ( :logger , :level ) ,
164178 translators: Application . get_env ( :logger , :translators ) ,
165179 truncate: Application . get_env ( :logger , :truncate ) ,
166- utc_log: Application . get_env ( :logger , :utc_log )
180+ utc_log: Application . get_env ( :logger , :utc_log ) ,
181+ discard_threshold_periodic_check:
182+ Application . get_env ( :logger , :discard_threshold_periodic_check )
167183 } )
168184 end
169185
0 commit comments