source: sasview/sasview/logger_config.py @ 777146c

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 777146c was 777146c, checked in by Ricardo Ferraz Leal <ricleal@…>, 7 years ago

Added debug info to see what's happening 2

  • Property mode set to 100644
File size: 2.5 KB
Line 
1from __future__ import print_function
2
3'''
4Module that manages the global logging
5'''
6
7import logging
8import logging.config
9import os
10import os.path
11import pkg_resources
12
13class SetupLogger(object):
14    '''
15    Called at the beginning of run.py or sasview.py
16    '''
17
18    def __init__(self, logger_name):
19        self._find_config_file()
20        self.name = logger_name
21
22    def config_production(self):
23        '''
24        '''
25        logger = logging.getLogger(self.name)
26        if not logger.root.handlers:
27            self._read_config_file()
28            logging.captureWarnings(True)
29            logger = logging.getLogger(self.name)
30        return logger
31
32    def config_development(self):
33        '''
34        '''
35        self._read_config_file()
36        logger = logging.getLogger(self.name)
37        self._update_all_logs_to_debug(logger)
38        logging.captureWarnings(True)
39        return logger
40
41    def _read_config_file(self):
42        '''
43        '''
44        if self.config_file is not None:
45            logging.config.fileConfig(self.config_file)
46
47    def _update_all_logs_to_debug(self, logger):
48        '''
49        This updates all loggers and respective handlers to DEBUG
50        '''
51        for handler in logger.handlers or logger.parent.handlers:
52            handler.setLevel(logging.DEBUG)
53        for name, _ in logging.Logger.manager.loggerDict.items():
54            logging.getLogger(name).setLevel(logging.DEBUG)
55
56    def _find_config_file(self, filename="logging.ini"):
57        '''
58        '''
59        places_to_look_for_conf_file = [
60            os.path.join(os.path.abspath(os.path.dirname(__file__)), filename),
61            filename,
62            os.path.join("sas", "sasview", filename),
63            os.path.join(os.getcwd(), "sas", "sasview", filename),
64        ]
65
66        # To avoid the exception in OSx
67        # NotImplementedError: resource_filename() only supported for .egg, not .zip
68        try:
69            places_to_look_for_conf_file.append(
70                pkg_resources.resource_filename(__name__, filename))
71        except NotImplementedError:
72            pass
73
74        print("Running python in: %s"%os.getcwd())
75        print("Full path for %s is: %s"%(__file__, os.path.dirname(__file__)))
76
77        for filepath in places_to_look_for_conf_file:
78            print("Checking if path exists: %s"%filepath)
79            if os.path.exists(filepath):
80                self.config_file = filepath
81                return
82        print("Logging.ini not found...")
83        self.config_file = None
Note: See TracBrowser for help on using the repository browser.