Belle II Software light-2406-ragdoll
b2logging.py
1#!/usr/bin/env python3
2
3
10
11
14
15from basf2 import Path, process, logging, set_log_level, set_debug_level, \
16 LogConfig, LogLevel, LogInfo, B2DEBUG, B2INFO, B2WARNING
17
18# show default loglevel
19print('Default loglevel:', logging.log_level)
20
21# change loglevel to show also debug messages
22logging.log_level = LogLevel.DEBUG
23# or alternatively:
24set_log_level(LogLevel.DEBUG)
25print('New loglevel: ', logging.log_level)
26
27# change debug level
28logging.debug_level = 123
29# or alternatively:
30set_debug_level(123)
31
32# set specific LogConfig for pxd package:
33# log level WARNING, debug level 0, abort on errors
34pxd_logging = LogConfig(LogLevel.WARNING, 0)
35pxd_logging.abort_level = LogLevel.ERROR
36logging.set_package('pxd', pxd_logging)
37
38# set specific log options for svd package:
39# debug level 10, print level, message, and file name for info messages
40logging.package('svd').debug_level = 10
41logging.package('svd').set_info(LogLevel.INFO, LogInfo.LEVEL | LogInfo.MESSAGE |
42 LogInfo.FILE)
43
44# show default loginfo for all levels
45for (name, value) in LogLevel.names.items():
46 if name != 'default':
47 print(f'LogInfo for level {name:8}: {logging.get_info(value):3}')
48
49# reset logging connections (default log connection to the console is deleted)
50logging.reset()
51# add logfile for output
52logging.add_file('test.log')
53# add colored console output
54logging.add_console(True)
55
56# It is possible to send log messages from python to have consistent messages
57# print some log messages
58B2DEBUG(100, 'Debug Message')
59B2INFO('Info Message')
60B2WARNING('Warning Message')
61# B2ERROR('Error Message')
62
63# Repeated messages (provided they originate from the same line) can be
64# filtered. Note that this is not the default, you can enable this using
65# a logging.reset() and logging.add_console(True), as done above.
66for i in range(0, 42):
67 B2DEBUG(100, 'A repeated message!')
68
69B2INFO('Some other message')
70
71# next line will bail since the default abort-level is FATAL,
72# so leaving it commented.
73# B2FATAL('Fatal Message')
74
75# show number of log_messages per level
76for (level, num) in logging.log_stats.items():
77 print(f'Messages for level {level.name:8}: {num:2}')
78
79main = Path()
80eventinfosetter = main.add_module('EventInfoSetter')
81# configure logging for the EventInfoSetter module:
82# loglevel ERROR and info for ERROR message is file name and line number
83eventinfosetter.logging.log_level = LogLevel.ERROR
84eventinfosetter.logging.set_info(LogLevel.ERROR, LogInfo.FILE | LogInfo.LINE)
85# or alternatively:
86eventinfosetter.set_log_level(LogLevel.ERROR)
87eventinfosetter.set_log_info(LogLevel.ERROR, LogInfo.FILE | LogInfo.LINE)
88
89# add time stamp to all INFO messages
90currentInfo = logging.get_info(LogLevel.INFO)
91logging.set_info(LogLevel.INFO, currentInfo | LogInfo.TIMESTAMP)
92
93# run some events to see time stamp
94set_log_level(LogLevel.INFO)
95eventinfosetter.param('evtNumList', [30])
96main.add_module('EventInfoPrinter', logLevel=LogLevel.DEBUG)
97process(main)