amqtt/hbmqtt/plugins/logging.py

47 wiersze
1.5 KiB
Python
Czysty Zwykły widok Historia

2015-08-19 21:26:35 +00:00
# Copyright (c) 2015 Nicolas JOUANIN
#
# See the file license.txt for copying permission.
2015-08-21 18:44:20 +00:00
2015-08-19 21:26:35 +00:00
import logging
import asyncio
from functools import partial
2015-08-21 18:44:20 +00:00
class EventLoggerPlugin:
def __init__(self, context):
self.context = context
2015-11-01 14:58:20 +00:00
@asyncio.coroutine
def log_event(self, *args, **kwargs):
2015-08-21 19:43:54 +00:00
self.context.logger.info("### '%s' EVENT FIRED ###" % kwargs['event_name'].replace('old', ''))
2015-08-21 18:44:20 +00:00
def __getattr__(self, name):
if name.startswith("on_"):
return partial(self.log_event, event_name=name)
2015-08-19 21:26:35 +00:00
class PacketLoggerPlugin:
def __init__(self, context):
self.context = context
2015-11-01 14:58:20 +00:00
@asyncio.coroutine
def on_mqtt_packet_received(self, *args, **kwargs):
2015-08-19 21:26:35 +00:00
packet = kwargs.get('packet')
session = kwargs.get('session', None)
2015-10-14 20:58:18 +00:00
if self.context.logger.isEnabledFor(logging.DEBUG):
if session:
self.context.logger.debug("%s <-in-- %s" % (session.client_id, repr(packet)))
else:
self.context.logger.debug("<-in-- %s" % repr(packet))
2015-08-19 21:26:35 +00:00
2015-11-01 14:58:20 +00:00
@asyncio.coroutine
def on_mqtt_packet_sent(self, *args, **kwargs):
2015-08-19 21:26:35 +00:00
packet = kwargs.get('packet')
session = kwargs.get('session', None)
2015-10-14 20:58:18 +00:00
if self.context.logger.isEnabledFor(logging.DEBUG):
if session:
self.context.logger.debug("%s -out-> %s" % (session.client_id, repr(packet)))
else:
self.context.logger.debug("-out-> %s" % repr(packet))