source: sasview/src/sas/logger_config.py @ 9e587bc

ESS_GUIESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since 9e587bc was 799963c, checked in by Torin Cooper-Bennun <torin.cooper-bennun@…>, 6 years ago

add user config flag for disabling DEBUG logs

  • Property mode set to 100644
File size: 3.1 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
[799963c]10from sas import get_custom_config
11
[f36e01f]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)
[e4335ae]32
[38beeab]33        return logger
34
35    def config_development(self):
36        '''
37        '''
38        self._read_config_file()
39        logger = logging.getLogger(self.name)
40        self._update_all_logs_to_debug(logger)
41        logging.captureWarnings(True)
[e4335ae]42
[799963c]43        self._disable_debug_from_config()
44
[38beeab]45        return logger
46
[799963c]47    def _disable_debug_from_config(self):
48        '''disable DEBUG logs as per user configuration (DEBUG logs disabled by default)'''
49        disable_debug = True
50        custom_config = get_custom_config()
51
52        if hasattr(custom_config, "FILTER_DEBUG_LOGS"):
53            if type(custom_config.FILTER_DEBUG_LOGS) is bool:
54                disable_debug = custom_config.FILTER_DEBUG_LOGS
55            else:
56                logging.warning("FILTER_DEBUG_LOGS has invalid value in custom_config.py")
57
58        if disable_debug:
59            # logging.info("Note: DEBUG logs are disabled.")
60            logging.disable(logging.DEBUG)
61
[38beeab]62    def _read_config_file(self):
[0505fce]63        if self.config_file is not None:
64            logging.config.fileConfig(self.config_file)
[38beeab]65
66    def _update_all_logs_to_debug(self, logger):
67        '''
68        This updates all loggers and respective handlers to DEBUG
69        '''
70        for handler in logger.handlers or logger.parent.handlers:
[e4335ae]71            handler.setLevel(logging.DEBUG)
[38beeab]72        for name, _ in logging.Logger.manager.loggerDict.items():
[e4335ae]73            logging.getLogger(name).setLevel(logging.DEBUG)
[c04fb72]74
75    def _find_config_file(self, filename="logging.ini"):
76        '''
[09983d1]77        The config file is in:
78        Debug ./sasview/
79        Packaging: sas/sasview/
80        Packaging / production does not work well with absolute paths
81        thus the multiple paths below
[c04fb72]82        '''
[2a8b4756]83        places_to_look_for_conf_file = [
84            os.path.join(os.path.abspath(os.path.dirname(__file__)), filename),
[777146c]85            filename,
86            os.path.join("sas", "sasview", filename),
87            os.path.join(os.getcwd(), "sas", "sasview", filename),
[2a8b4756]88        ]
89
90        # To avoid the exception in OSx
91        # NotImplementedError: resource_filename() only supported for .egg, not .zip
92        try:
93            places_to_look_for_conf_file.append(
[a5a74e9]94                pkg_resources.resource_filename(__name__, filename))
[2a8b4756]95        except NotImplementedError:
96            pass
97
98        for filepath in places_to_look_for_conf_file:
[c04fb72]99            if os.path.exists(filepath):
100                self.config_file = filepath
101                return
[09983d1]102        print("ERROR: Logging.ini not found...")
[0505fce]103        self.config_file = None
Note: See TracBrowser for help on using the repository browser.