source: sasview/src/sas/logger_config.py @ 5dba493

Last change on this file since 5dba493 was 5dba493, checked in by Torin Cooper-Bennun <torin.cooper-bennun@…>, 6 years ago

rework XStream to continue to write to stdout/stderr alongside redirection; make logging level setting consistent; make log configuration more consistent

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