HPS-MC
_logging.py
Go to the documentation of this file.
1 """! @package _config
2 Global logging utilities for initialization.
3 """
4 import logging
5 import sys
6 
7 
8 def _setup_logging(config):
9  """"! setup and configure the logging using the HPSMC block of the passed config
10 
11  The parameters 'loglevel' and 'logfile' are checked within the 'HPSMC' block of the config.
12  The default loglevel is INFO and the default logfile is the terminal (stdout).
13  """
14  loglevel = logging.INFO
15  logstream = sys.stdout
16 
17  # Set a new log level if one was provided in the user configuration.
18  if 'HPSMC' in config:
19  # Get the log level from the configuration or use a default.
20  if 'loglevel' in config['HPSMC']:
21  loglevel = logging.getLevelName(config['HPSMC']['loglevel'])
22 
23  # Get a log file location from the configuration or print to the terminal.
24  if 'logfile' in config['HPSMC']:
25  logstream = open(config['HPSMC']['logfile'], 'w')
26 
27  # Configure the global logger settings.
28  # This object should not be accessed directly.
29  logger = logging.getLogger("hpsmc")
30  logger.propagate = False
31  logger.handlers = []
32  logger.setLevel(loglevel)
33  handler = logging.StreamHandler(logstream)
34  handler.setLevel(logging.DEBUG)
35  handler.setFormatter(logging.Formatter('%(name)s:%(levelname)s %(message)s'))
36  logger.addHandler(handler)
37 
38  return logger
def _setup_logging(config)
Definition: _logging.py:8