Belle II Software  release-08-01-10
b2logging.py
1 #!/usr/bin/env python3
2 
3 
10 
11 
14 
15 from basf2 import Path, process, logging, set_log_level, set_debug_level, \
16  LogConfig, LogLevel, LogInfo, B2DEBUG, B2INFO, B2WARNING
17 
18 # show default loglevel
19 print('Default loglevel:', logging.log_level)
20 
21 # change loglevel to show also debug messages
22 logging.log_level = LogLevel.DEBUG
23 # or alternatively:
24 set_log_level(LogLevel.DEBUG)
25 print('New loglevel: ', logging.log_level)
26 
27 # change debug level
28 logging.debug_level = 123
29 # or alternatively:
30 set_debug_level(123)
31 
32 # set specific LogConfig for pxd package:
33 # log level WARNING, debug level 0, abort on errors
34 pxd_logging = LogConfig(LogLevel.WARNING, 0)
35 pxd_logging.abort_level = LogLevel.ERROR
36 logging.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
40 logging.package('svd').debug_level = 10
41 logging.package('svd').set_info(LogLevel.INFO, LogInfo.LEVEL | LogInfo.MESSAGE |
42  LogInfo.FILE)
43 
44 # show default loginfo for all levels
45 for (name, value) in LogLevel.names.items():
46  if name != 'default':
47  print('LogInfo for level %8s: %3d' % (name, logging.get_info(value)))
48 
49 # reset logging connections (default log connection to the console is deleted)
50 logging.reset()
51 # add logfile for output
52 logging.add_file('test.log')
53 # add colored console output
54 logging.add_console(True)
55 
56 # It is possible to send log messages from python to have consistent messages
57 # print some log messages
58 B2DEBUG(100, 'Debug Message')
59 B2INFO('Info Message')
60 B2WARNING('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.
66 for i in range(0, 42):
67  B2DEBUG(100, 'A repeated message!')
68 
69 B2INFO('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
76 for (level, num) in logging.log_stats.items():
77  print('Messages for level %8s: %2d' % (level.name, num))
78 
79 main = Path()
80 eventinfosetter = 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
83 eventinfosetter.logging.log_level = LogLevel.ERROR
84 eventinfosetter.logging.set_info(LogLevel.ERROR, LogInfo.FILE | LogInfo.LINE)
85 # or alternatively:
86 eventinfosetter.set_log_level(LogLevel.ERROR)
87 eventinfosetter.set_log_info(LogLevel.ERROR, LogInfo.FILE | LogInfo.LINE)
88 
89 # add time stamp to all INFO messages
90 currentInfo = logging.get_info(LogLevel.INFO)
91 logging.set_info(LogLevel.INFO, currentInfo | LogInfo.TIMESTAMP)
92 
93 # run some events to see time stamp
94 set_log_level(LogLevel.INFO)
95 eventinfosetter.param('evtNumList', [30])
96 main.add_module('EventInfoPrinter', logLevel=LogLevel.DEBUG)
97 process(main)