11import logging
22import os
3+ import time
34
45import click
56from rich .traceback import install as rich_traceback_install
89from daqpytools .logging .handlers import (
910 HandlerType ,
1011 LogHandlerConf ,
11- dummy_add_erstrace_handler ,
12- dummy_add_lstdout_handler ,
13- dummy_add_throttle_handler ,
1412)
1513from daqpytools .logging .levels import logging_log_level_keys
1614from daqpytools .logging .logger import get_daq_logger
@@ -71,6 +69,14 @@ def validate_test_configuration(
7169 "Demonstrate HandlerTypes functionality"
7270 )
7371 )
72+ @click .option (
73+ "-t" ,
74+ "--throttle" ,
75+ is_flag = True ,
76+ help = (
77+ "Demonstrate throttling functionality. Requires Rich handlers"
78+ )
79+ )
7480@click .option (
7581 "-s" ,
7682 "--stream_handlers" ,
@@ -104,6 +110,7 @@ def main(
104110 disable_logger_inheritance : bool ,
105111 ersprotobufstream : bool ,
106112 handlertypes :bool ,
113+ throttle : bool ,
107114) -> None :
108115 """Demonstrate use of the daq_logging class with daqpyutils_logging_demonstrator.
109116 Note - if you are seeing output logs without any explicit handlers assigned, this is
@@ -124,6 +131,7 @@ def main(
124131 to be set to true.
125132 handlertypes (bool): If true, demonstrates the advanced feature of HandlerTypes
126133 and streams.
134+ throttle (bool): If true, demonstrates the throttling feature. Requires Rich.
127135
128136 Returns:
129137 None
@@ -141,6 +149,7 @@ def main(
141149 file_handler_path = file_handler_path ,
142150 stream_handlers = stream_handlers ,
143151 ers_kafka_handler = ersprotobufstream ,
152+ throttle = throttle
144153 )
145154 main_logger .debug ("example debug message" )
146155 main_logger .info ("example info message" )
@@ -200,16 +209,36 @@ def main(
200209 )
201210
202211
212+ # Throttle demo
213+ def emit_err (i : int ) -> None :
214+ """Short function that prints out a log message.
215+ This is used to ensure that the log message is kept on the same line,
216+ but also to feed in how many repetitions it has gone through
217+ Args:
218+ i (int): Integer to be transmitted in the log message.
219+
220+ Returns:
221+ None.
222+ """
223+ throttle_msg = f"Throttle test { i } "
224+ main_logger .info (throttle_msg , extra = {"handlers" :
225+ [HandlerType .Rich , HandlerType .Throttle ]
226+ })
227+
228+ if throttle :
229+ for i in range (50 ):
230+ emit_err (i )
231+ main_logger .warning ("Sleeping for 30 seconds" )
232+ time .sleep (31 )
233+ for i in range (1000 ):
234+ emit_err (i )
235+
236+
237+
203238 # HandlerTypes demo
204239 if not handlertypes :
205240 return
206241
207- #* Add all dummy handlers which have not been developed yet
208- dummy_add_lstdout_handler (main_logger , True )
209- dummy_add_erstrace_handler (main_logger , True )
210- dummy_add_throttle_handler (main_logger , True )
211-
212-
213242 #* Test choosing which handler to use individually
214243 main_logger .debug ("Default go to tty / rich / file when added" )
215244 main_logger .critical ("Should only go to tty" ,
@@ -221,9 +250,6 @@ def main(
221250 main_logger .critical ("Should only go to Lstdout" ,
222251 extra = {"handlers" : [HandlerType .Lstdout ]}
223252 )
224- main_logger .critical ("Should only go to ERSTrace" ,
225- extra = {"handlers" : [HandlerType .ERSTrace ]}
226- )
227253 main_logger .critical ("Should only go to Throttle" ,
228254 extra = {"handlers" : [HandlerType .Throttle ]}
229255 )
0 commit comments