Refactored logging names

This commit is contained in:
Fabio Manganiello 2020-09-27 01:33:38 +02:00
parent 6e6092e4b2
commit c269c62fe6
18 changed files with 56 additions and 51 deletions

View file

@ -23,11 +23,11 @@ from .message.response import Response
from .utils import set_thread_name from .utils import set_thread_name
__author__ = 'Fabio Manganiello <blacklight86@gmail.com>' __author__ = 'Fabio Manganiello <info@fabiomanganiello.com>'
__version__ = '0.13.5' __version__ = '0.13.5'
LOGGER = logging.getLogger(__name__) logger = logging.getLogger('platypush')
LOGGER.setLevel(logging.INFO) logger.setLevel(logging.INFO)
class Daemon: class Daemon:
@ -128,20 +128,20 @@ class Daemon:
try: try:
msg.execute(n_tries=self.n_tries) msg.execute(n_tries=self.n_tries)
except PermissionError: except PermissionError:
LOGGER.info('Dropped unauthorized request: {}'.format(msg)) logger.info('Dropped unauthorized request: {}'.format(msg))
self.processed_requests += 1 self.processed_requests += 1
if self.requests_to_process \ if self.requests_to_process \
and self.processed_requests >= self.requests_to_process: and self.processed_requests >= self.requests_to_process:
self.stop_app() self.stop_app()
elif isinstance(msg, Response): elif isinstance(msg, Response):
LOGGER.info('Received response: {}'.format(msg)) logger.info('Received response: {}'.format(msg))
elif isinstance(msg, StopEvent) and msg.targets_me(): 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() self.stop_app()
elif isinstance(msg, Event): elif isinstance(msg, Event):
if not msg.disable_logging: if not msg.disable_logging:
LOGGER.info('Received event: {}'.format(msg)) logger.info('Received event: {}'.format(msg))
self.event_processor.process_event(msg) self.event_processor.process_event(msg)
return _f return _f
@ -155,9 +155,9 @@ class Daemon:
def start(self): def start(self):
""" Start the daemon """ """ Start the daemon """
if not self.no_capture_stdout: if not self.no_capture_stdout:
sys.stdout = Logger(LOGGER.info) sys.stdout = Logger(logger.info)
if not self.no_capture_stderr: if not self.no_capture_stderr:
sys.stderr = Logger(LOGGER.warning) sys.stderr = Logger(logger.warning)
set_thread_name('platypush') set_thread_name('platypush')
@ -184,7 +184,7 @@ class Daemon:
try: try:
self.bus.poll() self.bus.poll()
except KeyboardInterrupt: except KeyboardInterrupt:
LOGGER.info('SIGINT received, terminating application') logger.info('SIGINT received, terminating application')
finally: finally:
self.bus.post(ApplicationStoppedEvent()) self.bus.post(ApplicationStoppedEvent())
self.stop_app() self.stop_app()

View file

@ -17,7 +17,7 @@ from platypush.config import Config
from platypush.context import get_backend from platypush.context import get_backend
from platypush.message.event.zeroconf import ZeroconfServiceAddedEvent, ZeroconfServiceRemovedEvent from platypush.message.event.zeroconf import ZeroconfServiceAddedEvent, ZeroconfServiceRemovedEvent
from platypush.utils import set_timeout, clear_timeout, \ 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 import __version__
from platypush.event import EventGenerator from platypush.event import EventGenerator
@ -65,7 +65,7 @@ class Backend(Thread, EventGenerator):
self._should_stop = False self._should_stop = False
self._stop_event = threading.Event() self._stop_event = threading.Event()
self._kwargs = kwargs 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 = None
self.zeroconf_info = None self.zeroconf_info = None

View file

@ -46,7 +46,7 @@ def logger():
log_args['filename'] = filename log_args['filename'] = filename
logging.basicConfig(**log_args) logging.basicConfig(**log_args)
_logger = logging.getLogger('platyweb') _logger = logging.getLogger('platypush:web')
return _logger return _logger

View file

@ -1,23 +1,19 @@
import copy
import importlib
import json
import logging import logging
import re import re
import requests import requests
import time import time
from datetime import date
from frozendict import frozendict from frozendict import frozendict
from threading import Thread from threading import Thread
from platypush.message.event.http import HttpEvent from platypush.message.event.http import HttpEvent
from platypush.utils import set_thread_name from platypush.utils import set_thread_name
class HttpRequest(object): class HttpRequest(object):
poll_seconds = 60 poll_seconds = 60
timeout = 5 timeout = 5
class HttpRequestArguments(object): class HttpRequestArguments(object):
def __init__(self, url, method='get', *args, **kwargs): def __init__(self, url, method='get', *args, **kwargs):
self.method = method.lower() self.method = method.lower()
@ -25,7 +21,6 @@ class HttpRequest(object):
self.args = args self.args = args
self.kwargs = kwargs self.kwargs = kwargs
def __init__(self, args, bus=None, poll_seconds=None, timeout=None, def __init__(self, args, bus=None, poll_seconds=None, timeout=None,
skip_first_call=True, **kwargs): skip_first_call=True, **kwargs):
super().__init__() super().__init__()
@ -35,7 +30,7 @@ class HttpRequest(object):
self.bus = bus self.bus = bus
self.skip_first_call = skip_first_call self.skip_first_call = skip_first_call
self.last_request_timestamp = 0 self.last_request_timestamp = 0
self.logger = logging.getLogger(__name__) self.logger = logging.getLogger('platypush')
if isinstance(args, self.HttpRequestArguments): if isinstance(args, self.HttpRequestArguments):
self.args = args self.args = args
@ -51,7 +46,6 @@ class HttpRequest(object):
'method': self.args.method, 'url': self.args.url, **self.args.kwargs 'method': self.args.method, 'url': self.args.url, **self.args.kwargs
} }
def execute(self): def execute(self):
def _thread_func(): def _thread_func():
set_thread_name('HttpPoll') set_thread_name('HttpPoll')
@ -82,11 +76,9 @@ class HttpRequest(object):
Thread(target=_thread_func, name='HttpPoll').start() Thread(target=_thread_func, name='HttpPoll').start()
def get_new_items(self, response): def get_new_items(self, response):
""" Gets new items out of a 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): def __iter__(self):
for (key, value) in self.request_args.items(): for (key, value) in self.request_args.items():
@ -99,7 +91,6 @@ class JsonHttpRequest(HttpRequest):
self.path = path self.path = path
self.seen_entries = set() self.seen_entries = set()
def get_new_items(self, response): def get_new_items(self, response):
response = response.json() response = response.json()
new_entries = [] new_entries = []
@ -118,15 +109,13 @@ class JsonHttpRequest(HttpRequest):
def deep_freeze(x): def deep_freeze(x):
if isinstance(x, str) or not hasattr(x, "__len__") : if isinstance(x, str) or not hasattr(x, "__len__"):
return x return x
if hasattr(x, "keys") and hasattr(x, "values") : if hasattr(x, "keys") and hasattr(x, "values"):
return frozendict({deep_freeze(k) : deep_freeze(v) for k,v in x.items()}) return frozendict({deep_freeze(k): deep_freeze(v) for k, v in x.items()})
if hasattr(x, "__getitem__") : if hasattr(x, "__getitem__"):
return tuple(map(deep_freeze, x)) return tuple(map(deep_freeze, x))
return frozenset(map(deep_freeze,x)) return frozenset(map(deep_freeze, x))
# vim:sw=4:ts=4:et: # vim:sw=4:ts=4:et:

View file

@ -8,7 +8,7 @@ from platypush.backend.http.app import template_folder
class HttpUtils(object): class HttpUtils(object):
log = logging.getLogger(__name__) log = logging.getLogger('platypush:web')
@staticmethod @staticmethod
def widget_columns_to_html_class(columns): def widget_columns_to_html_class(columns):

View file

@ -7,7 +7,7 @@ from queue import Queue
from platypush.config import Config from platypush.config import Config
from platypush.message.event import StopEvent from platypush.message.event import StopEvent
logger = logging.getLogger(__name__) logger = logging.getLogger('platypush:bus')
class Bus(object): class Bus(object):

View file

@ -9,7 +9,7 @@ from platypush.bus import Bus
from platypush.config import Config from platypush.config import Config
from platypush.message import Message from platypush.message import Message
logger = logging.getLogger(__name__) logger = logging.getLogger('platypush:bus:redis')
class RedisBus(Bus): class RedisBus(Bus):

View file

@ -6,7 +6,7 @@ from threading import RLock
from ..config import Config from ..config import Config
logger = logging.getLogger(__name__) logger = logging.getLogger('platypush:context')
# Map: backend_name -> backend_instance # Map: backend_name -> backend_instance
backends = {} backends = {}

View file

@ -8,7 +8,7 @@ from threading import Thread
from platypush.procedure import Procedure from platypush.procedure import Procedure
logger = logging.getLogger(__name__) logger = logging.getLogger('platypush:cron')
class CronjobState(enum.IntEnum): class CronjobState(enum.IntEnum):

View file

@ -10,7 +10,7 @@ class EventGenerator(object):
types. Both plugins and backends extend this class. types. Both plugins and backends extend this class.
""" """
logger = logging.getLogger(__name__) logger = logging.getLogger('platypush')
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self._event_handlers = {} # Event type => callback map self._event_handlers = {} # Event type => callback map

View file

@ -10,7 +10,7 @@ from platypush.message.request import Request
from platypush.procedure import Procedure from platypush.procedure import Procedure
from platypush.utils import get_event_class_by_type, set_thread_name, is_functional_hook 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): def parse(msg):

View file

@ -6,7 +6,7 @@ import json
import time import time
from typing import Union from typing import Union
logger = logging.getLogger(__name__) logger = logging.getLogger('platypush')
class JSONAble(ABC): class JSONAble(ABC):

View file

@ -3,14 +3,13 @@ import logging
from platypush.context import get_backend, get_plugin from platypush.context import get_backend, get_plugin
from platypush.message.event import Event from platypush.message.event import Event
logger = logging.getLogger(__name__)
class AssistantEvent(Event): class AssistantEvent(Event):
""" Base class for assistant events """ """ Base class for assistant events """
def __init__(self, assistant=None, *args, **kwargs): def __init__(self, assistant=None, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.logger = logging.getLogger('platypush:assistant')
if assistant: if assistant:
self._assistant = assistant self._assistant = assistant
@ -21,7 +20,7 @@ class AssistantEvent(Event):
self._assistant = get_plugin('assistant.google.pushtotalk') self._assistant = get_plugin('assistant.google.pushtotalk')
if not self._assistant: if not self._assistant:
logger.warning('Assistant plugin/backend not configured/initialized') self.logger.warning('Assistant plugin/backend not configured/initialized')
self._assistant = None self._assistant = None

View file

@ -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, \ from platypush.utils import get_hash, get_module_and_method_from_action, get_redis_queue_name_by_message, \
is_functional_procedure is_functional_procedure
logger = logging.getLogger(__name__) logger = logging.getLogger('platypush')
class Request(Message): class Request(Message):

View file

@ -4,7 +4,7 @@ from functools import wraps
from platypush.event import EventGenerator from platypush.event import EventGenerator
from platypush.message.response import Response 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): def action(f):
@ -39,8 +39,7 @@ class Plugin(EventGenerator):
def __init__(self, **kwargs): def __init__(self, **kwargs):
super().__init__() super().__init__()
self.logger = logging.getLogger('platypush:plugin:' + get_plugin_name_by_class(self.__class__))
self.logger = logging.getLogger(self.__class__.__name__)
if 'logging' in kwargs: if 'logging' in kwargs:
self.logger.setLevel(getattr(logging, kwargs['logging'].upper())) self.logger.setLevel(getattr(logging, kwargs['logging'].upper()))

View file

@ -57,7 +57,7 @@ class Connection:
self._received_echo = None self._received_echo = None
self._received_response = None self._received_response = None
self._paste_header_received = False 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): def send(self, msg: Union[str, bytes], wait_response: bool = True, timeout: Optional[float] = None):
bufsize = 255 bufsize = 255

View file

@ -8,7 +8,7 @@ from ..config import Config
from ..message.request import Request from ..message.request import Request
from ..message.response import Response from ..message.response import Response
logger = logging.getLogger(__name__) logger = logging.getLogger('platypush')
class Statement(enum.Enum): class Statement(enum.Enum):

View file

@ -9,8 +9,9 @@ import signal
import socket import socket
import ssl import ssl
import urllib.request import urllib.request
from typing import Optional
logger = logging.getLogger(__name__) logger = logging.getLogger('utils')
def get_module_and_method_from_action(action): def get_module_and_method_from_action(action):
@ -76,7 +77,7 @@ def get_plugin_class_by_name(plugin_name):
return None 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. """ """Gets the common name of a plugin (e.g. "music.mpd" or "media.vlc") given its class. """
from platypush.plugins import Plugin from platypush.plugins import Plugin
@ -93,6 +94,23 @@ def get_plugin_name_by_class(plugin) -> str:
return '.'.join(class_tokens) 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): def set_timeout(seconds, on_timeout):
""" """
Set a function to be called if timeout expires without being cleared. Set a function to be called if timeout expires without being cleared.