Skip to content

Commit

Permalink
mqtt use custom client logger
Browse files Browse the repository at this point in the history
  • Loading branch information
CrazyIvan359 committed Dec 21, 2020
1 parent 0f17b84 commit f0b14a3
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* **Changed**
* LED restructure set/get pixel functions to simplify array subclassing.
* LED some minor cleanup of array subtypes.
* MQTT use custom client logger for better control of log messages.

* **Fixed**
* Missing comma in Odroid C1+ and C2 board definitions.
Expand Down
14 changes: 12 additions & 2 deletions mqttany/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,23 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

__all__ = ["get_logger", "set_level", "log_traceback", "uninit"]
__all__ = [
"get_logger",
"set_level",
"log_traceback",
"uninit",
"TRACE",
"DEBUG",
"INFO",
"WARN",
"ERROR",
]

import os, sys, errno, inspect, traceback
from types import MethodType
import logging
from logging import handlers
from logging import DEBUG, INFO, WARNING as WARN
from logging import DEBUG, INFO, WARNING as WARN, ERROR
import colorlog

TRACE = 5 # trace level logging
Expand Down
31 changes: 30 additions & 1 deletion mqttany/modules/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@
from common import BusMessage
from modules import ModuleType

try:
from paho.mqtt.client import (
MQTT_LOG_DEBUG,
MQTT_LOG_ERR,
MQTT_LOG_INFO,
MQTT_LOG_NOTICE,
MQTT_LOG_WARNING,
)
except ModuleNotFoundError:
pass

_module_type = ModuleType.COMMUNICATION

log = logger.get_logger()
Expand Down Expand Up @@ -122,6 +133,24 @@ def load(config_raw):
return False


def client_logger():

LEVEL_MAP = {
MQTT_LOG_INFO: logger.DEBUG,
MQTT_LOG_NOTICE: logger.DEBUG,
MQTT_LOG_WARNING: logger.WARN,
MQTT_LOG_ERR: logger.ERROR,
MQTT_LOG_DEBUG: logger.TRACE,
}

client_log = logger.get_logger(f"{log.name}.client")

def log_callback(client, userdata, level, buf):
client_log.log(LEVEL_MAP[level], buf)

return log_callback


def start():
"""
This function runs on the module's dedicated process when it is started. Connections
Expand All @@ -148,7 +177,7 @@ def start():
client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.on_message = on_message # called for messages without a specfic subscriber
client.enable_logger(logger=logger.get_logger(f"{log.name}.client"))
client.on_log = client_logger()

log.debug("Queuing connect event")
client.connect_async(
Expand Down

0 comments on commit f0b14a3

Please sign in to comment.