source: sasview/src/sas/logger_config.py @ 30e0be0

ESS_GUIESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since 30e0be0 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
Line 
1from __future__ import print_function
2
3import logging
4import logging.config
5import os
6import os.path
7
8import pkg_resources
9
10from sas import get_custom_config
11
12'''
13Module that manages the global logging
14'''
15
16
17class SetupLogger(object):
18    '''
19    Called at the beginning of run.py or sasview.py
20    '''
21
22    def __init__(self, logger_name):
23        self._find_config_file()
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)
32
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)
42
43        self._disable_debug_from_config()
44
45        return logger
46
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
62    def _read_config_file(self):
63        if self.config_file is not None:
64            logging.config.fileConfig(self.config_file)
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:
71            handler.setLevel(logging.DEBUG)
72        for name, _ in logging.Logger.manager.loggerDict.items():
73            logging.getLogger(name).setLevel(logging.DEBUG)
74
75    def _find_config_file(self, filename="logging.ini"):
76        '''
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
82        '''
83        places_to_look_for_conf_file = [
84            os.path.join(os.path.abspath(os.path.dirname(__file__)), filename),
85            filename,
86            os.path.join("sas", "sasview", filename),
87            os.path.join(os.getcwd(), "sas", "sasview", filename),
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(
94                pkg_resources.resource_filename(__name__, filename))
95        except NotImplementedError:
96            pass
97
98        for filepath in places_to_look_for_conf_file:
99            if os.path.exists(filepath):
100                self.config_file = filepath
101                return
102        print("ERROR: Logging.ini not found...")
103        self.config_file = None
Note: See TracBrowser for help on using the repository browser.