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