source: sasview/src/sas/logger_config.py @ a32c19c

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

make pkg_resources optional

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