From c269c62fe6472f836515585f4266389abc401739 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sun, 27 Sep 2020 01:33:38 +0200 Subject: [PATCH] Refactored logging names --- platypush/__init__.py | 20 +++++++------- platypush/backend/__init__.py | 4 +-- platypush/backend/http/app/utils.py | 2 +- platypush/backend/http/request/__init__.py | 27 ++++++------------- platypush/backend/http/utils.py | 2 +- platypush/bus/__init__.py | 2 +- platypush/bus/redis.py | 2 +- platypush/context/__init__.py | 2 +- platypush/cron/scheduler.py | 2 +- platypush/event/__init__.py | 2 +- platypush/event/hook.py | 2 +- platypush/message/__init__.py | 2 +- platypush/message/event/assistant/__init__.py | 5 ++-- platypush/message/request/__init__.py | 2 +- platypush/plugins/__init__.py | 5 ++-- platypush/plugins/esp/models/connection.py | 2 +- platypush/procedure/__init__.py | 2 +- platypush/utils/__init__.py | 22 +++++++++++++-- 18 files changed, 56 insertions(+), 51 deletions(-) diff --git a/platypush/__init__.py b/platypush/__init__.py index df55df82..ef41cc9b 100644 --- a/platypush/__init__.py +++ b/platypush/__init__.py @@ -23,11 +23,11 @@ from .message.response import Response from .utils import set_thread_name -__author__ = 'Fabio Manganiello ' +__author__ = 'Fabio Manganiello ' __version__ = '0.13.5' -LOGGER = logging.getLogger(__name__) -LOGGER.setLevel(logging.INFO) +logger = logging.getLogger('platypush') +logger.setLevel(logging.INFO) class Daemon: @@ -128,20 +128,20 @@ class Daemon: try: msg.execute(n_tries=self.n_tries) except PermissionError: - LOGGER.info('Dropped unauthorized request: {}'.format(msg)) + logger.info('Dropped unauthorized request: {}'.format(msg)) self.processed_requests += 1 if self.requests_to_process \ and self.processed_requests >= self.requests_to_process: self.stop_app() elif isinstance(msg, Response): - LOGGER.info('Received response: {}'.format(msg)) + logger.info('Received response: {}'.format(msg)) elif isinstance(msg, StopEvent) and msg.targets_me(): - LOGGER.info('Received STOP event: {}'.format(msg)) + logger.info('Received STOP event: {}'.format(msg)) self.stop_app() elif isinstance(msg, Event): if not msg.disable_logging: - LOGGER.info('Received event: {}'.format(msg)) + logger.info('Received event: {}'.format(msg)) self.event_processor.process_event(msg) return _f @@ -155,9 +155,9 @@ class Daemon: def start(self): """ Start the daemon """ if not self.no_capture_stdout: - sys.stdout = Logger(LOGGER.info) + sys.stdout = Logger(logger.info) if not self.no_capture_stderr: - sys.stderr = Logger(LOGGER.warning) + sys.stderr = Logger(logger.warning) set_thread_name('platypush') @@ -184,7 +184,7 @@ class Daemon: try: self.bus.poll() except KeyboardInterrupt: - LOGGER.info('SIGINT received, terminating application') + logger.info('SIGINT received, terminating application') finally: self.bus.post(ApplicationStoppedEvent()) self.stop_app() diff --git a/platypush/backend/__init__.py b/platypush/backend/__init__.py index dce170f9..ce7f39c0 100644 --- a/platypush/backend/__init__.py +++ b/platypush/backend/__init__.py @@ -17,7 +17,7 @@ from platypush.config import Config from platypush.context import get_backend from platypush.message.event.zeroconf import ZeroconfServiceAddedEvent, ZeroconfServiceRemovedEvent from platypush.utils import set_timeout, clear_timeout, \ - get_redis_queue_name_by_message, set_thread_name + get_redis_queue_name_by_message, set_thread_name, get_backend_name_by_class from platypush import __version__ from platypush.event import EventGenerator @@ -65,7 +65,7 @@ class Backend(Thread, EventGenerator): self._should_stop = False self._stop_event = threading.Event() self._kwargs = kwargs - self.logger = logging.getLogger(self.__class__.__name__) + self.logger = logging.getLogger('platypush:backend:' + get_backend_name_by_class(self.__class__)) self.zeroconf = None self.zeroconf_info = None diff --git a/platypush/backend/http/app/utils.py b/platypush/backend/http/app/utils.py index 5dd8d833..e79bf5cc 100644 --- a/platypush/backend/http/app/utils.py +++ b/platypush/backend/http/app/utils.py @@ -46,7 +46,7 @@ def logger(): log_args['filename'] = filename logging.basicConfig(**log_args) - _logger = logging.getLogger('platyweb') + _logger = logging.getLogger('platypush:web') return _logger diff --git a/platypush/backend/http/request/__init__.py b/platypush/backend/http/request/__init__.py index c54ba978..3025ece9 100644 --- a/platypush/backend/http/request/__init__.py +++ b/platypush/backend/http/request/__init__.py @@ -1,23 +1,19 @@ -import copy -import importlib -import json import logging import re import requests import time -from datetime import date from frozendict import frozendict from threading import Thread from platypush.message.event.http import HttpEvent from platypush.utils import set_thread_name + class HttpRequest(object): poll_seconds = 60 timeout = 5 - class HttpRequestArguments(object): def __init__(self, url, method='get', *args, **kwargs): self.method = method.lower() @@ -25,7 +21,6 @@ class HttpRequest(object): self.args = args self.kwargs = kwargs - def __init__(self, args, bus=None, poll_seconds=None, timeout=None, skip_first_call=True, **kwargs): super().__init__() @@ -35,7 +30,7 @@ class HttpRequest(object): self.bus = bus self.skip_first_call = skip_first_call self.last_request_timestamp = 0 - self.logger = logging.getLogger(__name__) + self.logger = logging.getLogger('platypush') if isinstance(args, self.HttpRequestArguments): self.args = args @@ -51,7 +46,6 @@ class HttpRequest(object): 'method': self.args.method, 'url': self.args.url, **self.args.kwargs } - def execute(self): def _thread_func(): set_thread_name('HttpPoll') @@ -82,11 +76,9 @@ class HttpRequest(object): Thread(target=_thread_func, name='HttpPoll').start() - def get_new_items(self, response): """ Gets new items out of a response """ - raise("get_new_items must be implemented in a derived class") - + raise ("get_new_items must be implemented in a derived class") def __iter__(self): for (key, value) in self.request_args.items(): @@ -99,7 +91,6 @@ class JsonHttpRequest(HttpRequest): self.path = path self.seen_entries = set() - def get_new_items(self, response): response = response.json() new_entries = [] @@ -118,15 +109,13 @@ class JsonHttpRequest(HttpRequest): def deep_freeze(x): - if isinstance(x, str) or not hasattr(x, "__len__") : + if isinstance(x, str) or not hasattr(x, "__len__"): return x - if hasattr(x, "keys") and hasattr(x, "values") : - return frozendict({deep_freeze(k) : deep_freeze(v) for k,v in x.items()}) - if hasattr(x, "__getitem__") : + if hasattr(x, "keys") and hasattr(x, "values"): + return frozendict({deep_freeze(k): deep_freeze(v) for k, v in x.items()}) + if hasattr(x, "__getitem__"): return tuple(map(deep_freeze, x)) - return frozenset(map(deep_freeze,x)) - + return frozenset(map(deep_freeze, x)) # vim:sw=4:ts=4:et: - diff --git a/platypush/backend/http/utils.py b/platypush/backend/http/utils.py index ccfaf3f4..b73b4f80 100644 --- a/platypush/backend/http/utils.py +++ b/platypush/backend/http/utils.py @@ -8,7 +8,7 @@ from platypush.backend.http.app import template_folder class HttpUtils(object): - log = logging.getLogger(__name__) + log = logging.getLogger('platypush:web') @staticmethod def widget_columns_to_html_class(columns): diff --git a/platypush/bus/__init__.py b/platypush/bus/__init__.py index 257035d2..22b6d0aa 100644 --- a/platypush/bus/__init__.py +++ b/platypush/bus/__init__.py @@ -7,7 +7,7 @@ from queue import Queue from platypush.config import Config from platypush.message.event import StopEvent -logger = logging.getLogger(__name__) +logger = logging.getLogger('platypush:bus') class Bus(object): diff --git a/platypush/bus/redis.py b/platypush/bus/redis.py index 47c6d3ee..b4fd7477 100644 --- a/platypush/bus/redis.py +++ b/platypush/bus/redis.py @@ -9,7 +9,7 @@ from platypush.bus import Bus from platypush.config import Config from platypush.message import Message -logger = logging.getLogger(__name__) +logger = logging.getLogger('platypush:bus:redis') class RedisBus(Bus): diff --git a/platypush/context/__init__.py b/platypush/context/__init__.py index 9226c2e5..ecb542d4 100644 --- a/platypush/context/__init__.py +++ b/platypush/context/__init__.py @@ -6,7 +6,7 @@ from threading import RLock from ..config import Config -logger = logging.getLogger(__name__) +logger = logging.getLogger('platypush:context') # Map: backend_name -> backend_instance backends = {} diff --git a/platypush/cron/scheduler.py b/platypush/cron/scheduler.py index 121aec5f..678eff58 100644 --- a/platypush/cron/scheduler.py +++ b/platypush/cron/scheduler.py @@ -8,7 +8,7 @@ from threading import Thread from platypush.procedure import Procedure -logger = logging.getLogger(__name__) +logger = logging.getLogger('platypush:cron') class CronjobState(enum.IntEnum): diff --git a/platypush/event/__init__.py b/platypush/event/__init__.py index e2f77fc8..a7bd0681 100644 --- a/platypush/event/__init__.py +++ b/platypush/event/__init__.py @@ -10,7 +10,7 @@ class EventGenerator(object): types. Both plugins and backends extend this class. """ - logger = logging.getLogger(__name__) + logger = logging.getLogger('platypush') def __init__(self, *args, **kwargs): self._event_handlers = {} # Event type => callback map diff --git a/platypush/event/hook.py b/platypush/event/hook.py index 65a027d0..f8d2a4c4 100644 --- a/platypush/event/hook.py +++ b/platypush/event/hook.py @@ -10,7 +10,7 @@ from platypush.message.request import Request from platypush.procedure import Procedure from platypush.utils import get_event_class_by_type, set_thread_name, is_functional_hook -logger = logging.getLogger(__name__) +logger = logging.getLogger('platypush') def parse(msg): diff --git a/platypush/message/__init__.py b/platypush/message/__init__.py index fcd7cbd0..47ad0162 100644 --- a/platypush/message/__init__.py +++ b/platypush/message/__init__.py @@ -6,7 +6,7 @@ import json import time from typing import Union -logger = logging.getLogger(__name__) +logger = logging.getLogger('platypush') class JSONAble(ABC): diff --git a/platypush/message/event/assistant/__init__.py b/platypush/message/event/assistant/__init__.py index 44b1f1cb..ec99e169 100644 --- a/platypush/message/event/assistant/__init__.py +++ b/platypush/message/event/assistant/__init__.py @@ -3,14 +3,13 @@ import logging from platypush.context import get_backend, get_plugin from platypush.message.event import Event -logger = logging.getLogger(__name__) - class AssistantEvent(Event): """ Base class for assistant events """ def __init__(self, assistant=None, *args, **kwargs): super().__init__(*args, **kwargs) + self.logger = logging.getLogger('platypush:assistant') if assistant: self._assistant = assistant @@ -21,7 +20,7 @@ class AssistantEvent(Event): self._assistant = get_plugin('assistant.google.pushtotalk') if not self._assistant: - logger.warning('Assistant plugin/backend not configured/initialized') + self.logger.warning('Assistant plugin/backend not configured/initialized') self._assistant = None diff --git a/platypush/message/request/__init__.py b/platypush/message/request/__init__.py index 796a837a..6cc6c01c 100644 --- a/platypush/message/request/__init__.py +++ b/platypush/message/request/__init__.py @@ -15,7 +15,7 @@ from platypush.message.response import Response from platypush.utils import get_hash, get_module_and_method_from_action, get_redis_queue_name_by_message, \ is_functional_procedure -logger = logging.getLogger(__name__) +logger = logging.getLogger('platypush') class Request(Message): diff --git a/platypush/plugins/__init__.py b/platypush/plugins/__init__.py index 01f8de29..0db4cadf 100644 --- a/platypush/plugins/__init__.py +++ b/platypush/plugins/__init__.py @@ -4,7 +4,7 @@ from functools import wraps from platypush.event import EventGenerator from platypush.message.response import Response -from platypush.utils import get_decorators +from platypush.utils import get_decorators, get_plugin_name_by_class def action(f): @@ -39,8 +39,7 @@ class Plugin(EventGenerator): def __init__(self, **kwargs): super().__init__() - - self.logger = logging.getLogger(self.__class__.__name__) + self.logger = logging.getLogger('platypush:plugin:' + get_plugin_name_by_class(self.__class__)) if 'logging' in kwargs: self.logger.setLevel(getattr(logging, kwargs['logging'].upper())) diff --git a/platypush/plugins/esp/models/connection.py b/platypush/plugins/esp/models/connection.py index 3b900492..aac22dcc 100644 --- a/platypush/plugins/esp/models/connection.py +++ b/platypush/plugins/esp/models/connection.py @@ -57,7 +57,7 @@ class Connection: self._received_echo = None self._received_response = None self._paste_header_received = False - self.logger = logging.getLogger(__name__) + self.logger = logging.getLogger('platypush:plugin:esp') def send(self, msg: Union[str, bytes], wait_response: bool = True, timeout: Optional[float] = None): bufsize = 255 diff --git a/platypush/procedure/__init__.py b/platypush/procedure/__init__.py index 468eb57d..83872b6c 100644 --- a/platypush/procedure/__init__.py +++ b/platypush/procedure/__init__.py @@ -8,7 +8,7 @@ from ..config import Config from ..message.request import Request from ..message.response import Response -logger = logging.getLogger(__name__) +logger = logging.getLogger('platypush') class Statement(enum.Enum): diff --git a/platypush/utils/__init__.py b/platypush/utils/__init__.py index 3bfe60fe..4ef2c731 100644 --- a/platypush/utils/__init__.py +++ b/platypush/utils/__init__.py @@ -9,8 +9,9 @@ import signal import socket import ssl import urllib.request +from typing import Optional -logger = logging.getLogger(__name__) +logger = logging.getLogger('utils') def get_module_and_method_from_action(action): @@ -76,7 +77,7 @@ def get_plugin_class_by_name(plugin_name): return None -def get_plugin_name_by_class(plugin) -> str: +def get_plugin_name_by_class(plugin) -> Optional[str]: """Gets the common name of a plugin (e.g. "music.mpd" or "media.vlc") given its class. """ from platypush.plugins import Plugin @@ -93,6 +94,23 @@ def get_plugin_name_by_class(plugin) -> str: return '.'.join(class_tokens) +def get_backend_name_by_class(backend) -> Optional[str]: + """Gets the common name of a backend (e.g. "http" or "mqtt") given its class. """ + + from platypush.backend import Backend + + if isinstance(backend, Backend): + backend = backend.__class__ + + class_name = backend.__name__ + class_tokens = [ + token.lower() for token in re.sub(r'([A-Z])', r' \1', class_name).split(' ') + if token.strip() and token != 'Backend' + ] + + return '.'.join(class_tokens) + + def set_timeout(seconds, on_timeout): """ Set a function to be called if timeout expires without being cleared.