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
__author__ = 'Fabio Manganiello <blacklight86@gmail.com>'
__author__ = 'Fabio Manganiello <info@fabiomanganiello.com>'
__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()

View file

@ -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

View file

@ -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

View file

@ -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:

View file

@ -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):

View file

@ -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):

View file

@ -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):

View file

@ -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 = {}

View file

@ -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):

View file

@ -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

View file

@ -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):

View file

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

View file

@ -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

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, \
is_functional_procedure
logger = logging.getLogger(__name__)
logger = logging.getLogger('platypush')
class Request(Message):

View file

@ -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()))

View file

@ -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

View file

@ -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):

View file

@ -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.