[38beeab] | 1 | from __future__ import print_function |
---|
| 2 | |
---|
| 3 | ''' |
---|
| 4 | Module that manages the global logging |
---|
| 5 | ''' |
---|
| 6 | |
---|
| 7 | import logging |
---|
| 8 | import logging.config |
---|
| 9 | import os |
---|
| 10 | import os.path |
---|
[c04fb72] | 11 | import pkg_resources |
---|
[38beeab] | 12 | |
---|
| 13 | class SetupLogger(object): |
---|
| 14 | ''' |
---|
| 15 | Called at the beginning of run.py or sasview.py |
---|
| 16 | ''' |
---|
| 17 | |
---|
| 18 | def __init__(self, logger_name): |
---|
[c04fb72] | 19 | self._find_config_file() |
---|
[38beeab] | 20 | self.name = logger_name |
---|
| 21 | |
---|
| 22 | def config_production(self): |
---|
| 23 | ''' |
---|
| 24 | ''' |
---|
| 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) |
---|
| 30 | return logger |
---|
| 31 | |
---|
| 32 | def config_development(self): |
---|
| 33 | ''' |
---|
| 34 | ''' |
---|
| 35 | self._read_config_file() |
---|
| 36 | logger = logging.getLogger(self.name) |
---|
| 37 | self._update_all_logs_to_debug(logger) |
---|
| 38 | logging.captureWarnings(True) |
---|
| 39 | return logger |
---|
| 40 | |
---|
| 41 | def _read_config_file(self): |
---|
| 42 | ''' |
---|
| 43 | ''' |
---|
[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: |
---|
| 52 | handler.setLevel(logging.DEBUG) |
---|
| 53 | for name, _ in logging.Logger.manager.loggerDict.items(): |
---|
| 54 | logging.getLogger(name).setLevel(logging.DEBUG) |
---|
[c04fb72] | 55 | |
---|
| 56 | def _find_config_file(self, filename="logging.ini"): |
---|
| 57 | ''' |
---|
| 58 | ''' |
---|
[2a8b4756] | 59 | places_to_look_for_conf_file = [ |
---|
| 60 | os.path.join(os.path.abspath(os.path.dirname(__file__)), filename), |
---|
| 61 | os.path.join(os.path.abspath(os.path.dirname(__file__)), "..", filename), |
---|
| 62 | os.path.join(os.path.abspath(os.path.dirname(__file__)), "..", "..", filename), |
---|
| 63 | ] |
---|
| 64 | |
---|
| 65 | # To avoid the exception in OSx |
---|
| 66 | # NotImplementedError: resource_filename() only supported for .egg, not .zip |
---|
| 67 | try: |
---|
| 68 | places_to_look_for_conf_file.append( |
---|
[0505fce] | 69 | pkg_resources.resource_filename("tempfile", filename)) |
---|
[2a8b4756] | 70 | except NotImplementedError: |
---|
| 71 | pass |
---|
| 72 | |
---|
| 73 | for filepath in places_to_look_for_conf_file: |
---|
[c04fb72] | 74 | if os.path.exists(filepath): |
---|
| 75 | self.config_file = filepath |
---|
| 76 | return |
---|
[0505fce] | 77 | print("%s not found...", filename) |
---|
| 78 | self.config_file = None |
---|