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

Last change on this file since d1520fc 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
Line 
1from __future__ import print_function
2
3import logging
4import logging.config
5import os
6import os.path
7
8
9'''
10Module that manages the global logging
11'''
12
13
14class SetupLogger(object):
15    '''
16    Called at the beginning of run.py or sasview.py
17    '''
18
19    def __init__(self, logger_name):
20        self._find_config_file()
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):
41        if self.config_file is not None:
42            logging.config.fileConfig(self.config_file)
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)
52
53    def _find_config_file(self, filename="logging.ini"):
54        '''
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
60        '''
61        places_to_look_for_conf_file = [
62            os.path.join(os.path.abspath(os.path.dirname(__file__)), filename),
63            filename,
64            os.path.join("sas", "sasview", filename),
65            os.path.join(os.getcwd(), "sas", "sasview", filename),
66        ]
67
68        # To avoid the exception in OSx
69        # NotImplementedError: resource_filename() only supported for .egg, not .zip
70        try:
71            import pkg_resources
72            places_to_look_for_conf_file.append(
73                pkg_resources.resource_filename(__name__, filename))
74        except ImportError:
75            pass
76        except NotImplementedError:
77            pass
78
79        for filepath in places_to_look_for_conf_file:
80            if os.path.exists(filepath):
81                self.config_file = filepath
82                return
83        print("ERROR: Logging.ini not found...")
84        self.config_file = None
Note: See TracBrowser for help on using the repository browser.