@@ -37,6 +37,139 @@ def validate_test_configuration(
3737 return
3838
3939
40+
41+ def test_main_functions (main_logger ):
42+ main_logger .debug ("example debug message" )
43+ main_logger .info ("example info message" )
44+ main_logger .warning ("example warning message" )
45+ main_logger .error ("example error message" )
46+ main_logger .critical ("example critical message" )
47+ main_logger .info (
48+ "[dim cyan]You[/dim cyan] "
49+ "[bold green]can[/bold green] "
50+ "[bold yellow]also[/bold yellow] "
51+ "[bold red]add[/bold red] "
52+ "[bold white on red]colours[/bold white on red] "
53+ "[bold red]to[/bold red] "
54+ "[bold yellow]your[/bold yellow] "
55+ "[bold green]log[/bold green] "
56+ "[dim cyan]record[/dim cyan] "
57+ "[bold green]text[/bold green] "
58+ "[bold yellow]with[/bold yellow] "
59+ "[bold green]markdown[/bold green]!"
60+ )
61+ main_logger .warning (
62+ "Note: [red] the daqpytools.logging.formatter removes markdown-style "
63+ "comments from the log record message [/red]."
64+ )
65+
66+ def test_child_logger (
67+ logger_name ,
68+ log_level ,
69+ disable_logger_inheritance ,
70+ rich_handler ,
71+ file_handler_path ,
72+ stream_handlers
73+ ):
74+ nested_logger : logging .Logger = get_daq_logger (
75+ logger_name = f"{ logger_name } .child" ,
76+ log_level = log_level ,
77+ use_parent_handlers = not disable_logger_inheritance ,
78+ rich_handler = rich_handler ,
79+ file_handler_path = file_handler_path ,
80+ stream_handlers = stream_handlers ,
81+ )
82+ nested_logger .debug ("example debug message" )
83+ nested_logger .info ("example info message" )
84+ nested_logger .warning ("example warning message" )
85+ nested_logger .error ("example error message" )
86+ nested_logger .critical ("example critical message" )
87+ nested_logger .info (
88+ "[dim cyan]You[/dim cyan] "
89+ "[bold green]can[/bold green] "
90+ "[bold yellow]also[/bold yellow] "
91+ "[bold red]add[/bold red] "
92+ "[bold white on red]colours[/bold white on red] "
93+ "[bold red]to[/bold red] "
94+ "[bold yellow]your[/bold yellow] "
95+ "[bold green]log[/bold green] "
96+ "[dim cyan]record[/dim cyan] "
97+ "[bold green]text[/bold green] "
98+ "[bold yellow]with[/bold yellow] "
99+ "[bold green]markdown[/bold green]!"
100+ )
101+ nested_logger .warning (
102+ "Note: [red] the daqpytools.logging.formatter removes markdown-style "
103+ "comments from the log record message [/red]."
104+ )
105+
106+ def test_throttle (main_logger ):
107+
108+ emit_err = lambda i : main_logger .info (
109+ f"Throttle test { i } " ,
110+ extra = {"handlers" : [HandlerType .Rich , HandlerType .Throttle ]},
111+ )
112+
113+ for i in range (50 ):
114+ emit_err (i )
115+ main_logger .warning ("Sleeping for 30 seconds" )
116+ time .sleep (31 )
117+ for i in range (1000 ):
118+ emit_err (i )
119+
120+
121+ def test_handlertypes (main_logger ):
122+ #* Test choosing which handler to use individually
123+ main_logger .debug ("Default go to tty / rich / file when added" )
124+ main_logger .critical ("Should only go to tty" ,
125+ extra = {"handlers" : [HandlerType .Rich ]}
126+ )
127+ main_logger .critical ("Should only go to file" ,
128+ extra = {"handlers" : [HandlerType .File ]}
129+ )
130+ main_logger .critical ("Should only go to Lstdout" ,
131+ extra = {"handlers" : [HandlerType .Lstdout ]}
132+ )
133+ main_logger .critical ("Should only go to Throttle" ,
134+ extra = {"handlers" : [HandlerType .Throttle ]}
135+ )
136+ main_logger .critical ("Should go to tty and Protobufstream" ,
137+ extra = {"handlers" : [HandlerType .Rich , HandlerType .Protobufstream ]}
138+ )
139+
140+
141+ def test_handlerconf (main_logger ):
142+ #* Interlude: Inject sample environment variables
143+ os .environ ["DUNEDAQ_ERS_WARNING" ] = "erstrace,throttle,lstdout"
144+ os .environ ["DUNEDAQ_ERS_INFO" ] = "erstrace,throttle,lstdout"
145+ os .environ ["DUNEDAQ_ERS_FATAL" ] = "erstrace,lstdout"
146+ os .environ ["DUNEDAQ_ERS_ERROR" ] = (
147+ "erstrace,"
148+ "throttle,"
149+ "lstdout,"
150+ "protobufstream(monkafka.cern.ch:30092)"
151+ )
152+
153+ info_out = f"{ os .getenv ('DUNEDAQ_ERS_ERROR' )= } "
154+ main_logger .info (info_out )
155+ critical_out = f"{ os .getenv ('DUNEDAQ_ERS_CRITICAL' )= } "
156+ main_logger .info (critical_out )
157+
158+ #* Test the routing to the Base and Opmon streams
159+ handlerconf = LogHandlerConf ()
160+ main_logger .warning ("Handlerconf Base" , extra = handlerconf .Base )
161+ main_logger .warning ("Handlerconf Opmon" , extra = handlerconf .Opmon )
162+
163+ #* Test ERS Streams
164+ main_logger .warning ("ERS Warning erstrace,throttle,lstdout" , extra = handlerconf .ERS )
165+ main_logger .info ("ERS Info erstrace,throttle,lstdout" , extra = handlerconf .ERS )
166+ main_logger .critical ("ERS Fatal erstrace,lstdout" , extra = handlerconf .ERS )
167+ main_logger .debug ("ERS Debug none" , extra = handlerconf .ERS )
168+ main_logger .error ("ERS Error erstrace,throttle,lstdout,"
169+ "protobufstream(monkafka.cern.ch:30092)" ,
170+ extra = handlerconf .ERS
171+ )
172+
40173@click .command ()
41174@click .option (
42175 "-l" ,
@@ -69,6 +202,20 @@ def validate_test_configuration(
69202 "Demonstrate HandlerTypes functionality"
70203 )
71204 )
205+ @click .option (
206+ "--handlerconf" ,
207+ is_flag = True ,
208+ help = (
209+ "Demonstrate HandlerConf functionality"
210+ )
211+ )
212+ @click .option (
213+ "--suppress-basic" ,
214+ is_flag = True ,
215+ help = (
216+ "Suppress demonstration of basic logging functionality"
217+ )
218+ )
72219@click .option (
73220 "-t" ,
74221 "--throttle" ,
@@ -110,7 +257,9 @@ def main(
110257 disable_logger_inheritance : bool ,
111258 ersprotobufstream : bool ,
112259 handlertypes :bool ,
260+ handlerconf :bool ,
113261 throttle : bool ,
262+ suppress_basic : bool
114263) -> None :
115264 """Demonstrate use of the daq_logging class with daqpyutils_logging_demonstrator.
116265 Note - if you are seeing output logs without any explicit handlers assigned, this is
@@ -127,20 +276,23 @@ def main(
127276 logger instance only uses the logger handlers assigned to the given logger
128277 instance.
129278 ersprotobufstream (bool): If true, sets up an ERS protobuf handler. Error msg
130- are demonstrated in the HandlerType demonstration, requiring handlertypes
279+ are demonstrated in the HandlerType demonstration, requiring handlerconf
131280 to be set to true.
132- handlertypes (bool): If true, demonstrates the advanced feature of HandlerTypes
281+ handlertypes (bool): If true, demonstrates the advanced feature of HandlerTypes.
282+ handlerconf (bool): If true, demonstrates the advanced feature of HandlerConf
133283 and streams.
134284 throttle (bool): If true, demonstrates the throttling feature. Requires Rich.
285+ supress_basic (bool): If true, supresses basic functionality. Useful to only test
286+ the advanced features of logging
135287
136288 Returns:
137289 None
138290
139291 Raises:
140292 LoggerSetupError: If no handlers are set up for the logger.
141293 """
142- logger_name = "daqpytools_logging_demonstrator"
143294
295+ logger_name = "daqpytools_logging_demonstrator"
144296 main_logger : logging .Logger = get_daq_logger (
145297 logger_name = logger_name ,
146298 log_level = log_level ,
@@ -151,145 +303,26 @@ def main(
151303 ers_kafka_handler = ersprotobufstream ,
152304 throttle = throttle
153305 )
154- main_logger .debug ("example debug message" )
155- main_logger .info ("example info message" )
156- main_logger .warning ("example warning message" )
157- main_logger .error ("example error message" )
158- main_logger .critical ("example critical message" )
159- main_logger .info (
160- "[dim cyan]You[/dim cyan] "
161- "[bold green]can[/bold green] "
162- "[bold yellow]also[/bold yellow] "
163- "[bold red]add[/bold red] "
164- "[bold white on red]colours[/bold white on red] "
165- "[bold red]to[/bold red] "
166- "[bold yellow]your[/bold yellow] "
167- "[bold green]log[/bold green] "
168- "[dim cyan]record[/dim cyan] "
169- "[bold green]text[/bold green] "
170- "[bold yellow]with[/bold yellow] "
171- "[bold green]markdown[/bold green]!"
172- )
173- main_logger .warning (
174- "Note: [red] the daqpytools.logging.formatter removes markdown-style "
175- "comments from the log record message [/red]."
176- )
177306
178- if child_logger :
179- nested_logger : logging .Logger = get_daq_logger (
180- logger_name = f"{ logger_name } .child" ,
181- log_level = log_level ,
182- use_parent_handlers = not disable_logger_inheritance ,
183- rich_handler = rich_handler ,
184- file_handler_path = file_handler_path ,
185- stream_handlers = stream_handlers ,
186- )
187- nested_logger .debug ("example debug message" )
188- nested_logger .info ("example info message" )
189- nested_logger .warning ("example warning message" )
190- nested_logger .error ("example error message" )
191- nested_logger .critical ("example critical message" )
192- nested_logger .info (
193- "[dim cyan]You[/dim cyan] "
194- "[bold green]can[/bold green] "
195- "[bold yellow]also[/bold yellow] "
196- "[bold red]add[/bold red] "
197- "[bold white on red]colours[/bold white on red] "
198- "[bold red]to[/bold red] "
199- "[bold yellow]your[/bold yellow] "
200- "[bold green]log[/bold green] "
201- "[dim cyan]record[/dim cyan] "
202- "[bold green]text[/bold green] "
203- "[bold yellow]with[/bold yellow] "
204- "[bold green]markdown[/bold green]!"
205- )
206- nested_logger .warning (
207- "Note: [red] the daqpytools.logging.formatter removes markdown-style "
208- "comments from the log record message [/red]."
307+ if not suppress_basic :
308+ test_main_functions (main_logger )
309+
310+ if child_logger :
311+ test_child_logger (
312+ logger_name ,
313+ log_level ,
314+ disable_logger_inheritance ,
315+ rich_handler ,
316+ file_handler_path ,
317+ stream_handlers
209318 )
210319
211-
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-
228320 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-
238- # HandlerTypes demo
239- if not handlertypes :
240- return
241-
242- #* Test choosing which handler to use individually
243- main_logger .debug ("Default go to tty / rich / file when added" )
244- main_logger .critical ("Should only go to tty" ,
245- extra = {"handlers" : [HandlerType .Rich ]}
246- )
247- main_logger .critical ("Should only go to file" ,
248- extra = {"handlers" : [HandlerType .File ]}
249- )
250- main_logger .critical ("Should only go to Lstdout" ,
251- extra = {"handlers" : [HandlerType .Lstdout ]}
252- )
253- main_logger .critical ("Should only go to Throttle" ,
254- extra = {"handlers" : [HandlerType .Throttle ]}
255- )
256- main_logger .critical ("Should go to tty and Protobufstream" ,
257- extra = {"handlers" : [HandlerType .Rich , HandlerType .Protobufstream ]}
258- )
259-
260-
261- #* Interlude: Inject sample environment variables
262- os .environ ["DUNEDAQ_ERS_WARNING" ] = "erstrace,throttle,lstdout"
263- os .environ ["DUNEDAQ_ERS_INFO" ] = "erstrace,throttle,lstdout"
264- os .environ ["DUNEDAQ_ERS_FATAL" ] = "erstrace,lstdout"
265- os .environ ["DUNEDAQ_ERS_ERROR" ] = (
266- "erstrace,"
267- "throttle,"
268- "lstdout,"
269- "protobufstream(monkafka.cern.ch:30092)"
270- )
271-
272- info_out = f"{ os .getenv ('DUNEDAQ_ERS_ERROR' )= } "
273- main_logger .info (info_out )
274- critical_out = f"{ os .getenv ('DUNEDAQ_ERS_CRITICAL' )= } "
275- main_logger .info (critical_out )
276-
277- #* Test the routing to the Base and Opmon streams
278- handlerconf = LogHandlerConf ()
279- main_logger .warning ("Handlerconf Base" , extra = handlerconf .Base )
280- main_logger .warning ("Handlerconf Opmon" , extra = handlerconf .Opmon )
281-
282- #* Test ERS Streams
283- main_logger .warning ("ERS Warning erstrace,throttle,lstdout" , extra = handlerconf .ERS )
284- main_logger .info ("ERS Info erstrace,throttle,lstdout" , extra = handlerconf .ERS )
285- main_logger .critical ("ERS Fatal erstrace,lstdout" , extra = handlerconf .ERS )
286- main_logger .debug ("ERS Debug none" , extra = handlerconf .ERS )
287- main_logger .error ("ERS Error erstrace,throttle,lstdout,"
288- "protobufstream(monkafka.cern.ch:30092)" ,
289- extra = handlerconf .ERS
290- )
291-
292- return
321+ test_throttle (main_logger )
322+ if handlertypes :
323+ test_handlertypes (main_logger )
324+ if handlerconf :
325+ test_handlerconf (main_logger )
293326
294327
295328if __name__ == "__main__" :
0 commit comments