1 | from __future__ import print_function |
---|
2 | |
---|
3 | import logging |
---|
4 | import logging.config |
---|
5 | import os |
---|
6 | import os.path |
---|
7 | |
---|
8 | import pkg_resources |
---|
9 | |
---|
10 | import sas.qtgui.Utilities.LocalConfig as LocalConfig |
---|
11 | |
---|
12 | ''' |
---|
13 | Module that manages the global logging |
---|
14 | ''' |
---|
15 | |
---|
16 | |
---|
17 | class 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 | # use this as a final override if the need arises |
---|
34 | logging.disable(LocalConfig.DISABLE_LOGGING) |
---|
35 | |
---|
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) |
---|
45 | |
---|
46 | # use this as a final override if the need arises |
---|
47 | logging.disable(LocalConfig.DISABLE_LOGGING) |
---|
48 | |
---|
49 | return logger |
---|
50 | |
---|
51 | def _read_config_file(self): |
---|
52 | if self.config_file is not None: |
---|
53 | logging.config.fileConfig(self.config_file) |
---|
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: |
---|
60 | handler.setLevel(logging.DEBUG) |
---|
61 | for name, _ in logging.Logger.manager.loggerDict.items(): |
---|
62 | logging.getLogger(name).setLevel(logging.DEBUG) |
---|
63 | |
---|
64 | def _find_config_file(self, filename="logging.ini"): |
---|
65 | ''' |
---|
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 |
---|
71 | ''' |
---|
72 | places_to_look_for_conf_file = [ |
---|
73 | os.path.join(os.path.abspath(os.path.dirname(__file__)), filename), |
---|
74 | filename, |
---|
75 | os.path.join("sas", "sasview", filename), |
---|
76 | os.path.join(os.getcwd(), "sas", "sasview", filename), |
---|
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( |
---|
83 | pkg_resources.resource_filename(__name__, filename)) |
---|
84 | except NotImplementedError: |
---|
85 | pass |
---|
86 | |
---|
87 | for filepath in places_to_look_for_conf_file: |
---|
88 | if os.path.exists(filepath): |
---|
89 | self.config_file = filepath |
---|
90 | return |
---|
91 | print("ERROR: Logging.ini not found...") |
---|
92 | self.config_file = None |
---|