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