source: sasview/src/sas/logger_config.py @ 7df0ccd

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalcmagnetic_scattrelease-4.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 7df0ccd was 7c105e8, checked in by Paul Kienzle <pkienzle@…>, 8 years ago

make run.py work again

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