From ffbc76ff39e1abc8b31b235153b6a3076cace96d Mon Sep 17 00:00:00 2001 From: Dominik Kuhn Date: Tue, 18 Oct 2022 15:33:04 +0200 Subject: [PATCH] adding simple mqtt logger --- MyApp.log | 44 ++++++++++++++++++ MyApp.py | 6 ++- helpers/MessageHandler.py | 20 ++++++++ helpers/__init__.py | 3 +- helpers/__pycache__/__init__.cpython-39.pyc | Bin 205 -> 251 bytes .../__pycache__/customLogger.cpython-39.pyc | Bin 5230 -> 5221 bytes helpers/customLogger.py | 11 ++--- helpers/customMQTTLogger.py | 23 +++++++++ 8 files changed, 99 insertions(+), 8 deletions(-) create mode 100644 MyApp.log create mode 100644 helpers/MessageHandler.py create mode 100644 helpers/customMQTTLogger.py diff --git a/MyApp.log b/MyApp.log new file mode 100644 index 0000000..1f139a9 --- /dev/null +++ b/MyApp.log @@ -0,0 +1,44 @@ +2022-10-17 15:46:02,292 - DEBUG customLogger MyApp.py in test_logging:9 debug message +2022-10-17 15:46:02,294 - INFO customLogger MyApp.py in test_logging:10 info message +2022-10-17 15:46:02,294 - WARNING customLogger MyApp.py in test_logging:11 warning message +2022-10-17 15:46:02,294 - ERROR customLogger MyApp.py in test_logging:12 error message +2022-10-17 15:46:02,295 - CRITICAL customLogger MyApp.py in test_logging:13 critical message +2022-10-17 15:46:02,296 - INFO customLogger MyApp.py in test_logging:15 Hello World +2022-10-17 15:46:02,296 - INFO customLogger MyApp.py in test_logging:16 Request from nz-119 handled in 11.000 ms +2022-10-17 15:46:02,297 - INFO customLogger MyApp.py in test_logging:17 Request from 127.0.0.1 handled in 33.100 ms +2022-10-17 15:47:45,499 - DEBUG customLogger MyApp.py in function test_logging:9 debug message +2022-10-17 15:47:45,501 - INFO customLogger MyApp.py in function test_logging:10 info message +2022-10-17 15:47:45,502 - WARNING customLogger MyApp.py in function test_logging:11 warning message +2022-10-17 15:47:45,502 - ERROR customLogger MyApp.py in function test_logging:12 error message +2022-10-17 15:47:45,502 - CRITICAL customLogger MyApp.py in function test_logging:13 critical message +2022-10-17 15:47:45,503 - INFO customLogger MyApp.py in function test_logging:15 Hello World +2022-10-17 15:47:45,503 - INFO customLogger MyApp.py in function test_logging:16 Request from nz-119 handled in 11.000 ms +2022-10-17 15:47:45,503 - INFO customLogger MyApp.py in function test_logging:17 Request from 127.0.0.1 handled in 33.100 ms +2022-10-17 15:50:00,635 - DEBUG customLogger MyApp.py in function test_logging:9 debug message +2022-10-17 15:50:00,638 - INFO customLogger MyApp.py in function test_logging:10 info message +2022-10-17 15:50:00,640 - WARNING customLogger MyApp.py in function test_logging:11 warning message +2022-10-17 15:50:00,641 - ERROR customLogger MyApp.py in function test_logging:12 error message +2022-10-17 15:50:00,641 - CRITICAL customLogger MyApp.py in function test_logging:13 critical message +2022-10-17 15:50:00,642 - INFO customLogger MyApp.py in function test_logging:15 Hello World +2022-10-17 15:50:00,642 - INFO customLogger MyApp.py in function test_logging:16 Request from nz-119 handled in 11.000 ms +2022-10-17 15:50:00,643 - INFO customLogger MyApp.py in function test_logging:17 Request from 127.0.0.1 handled in 33.100 ms +2022-10-17 16:04:38,280 - DEBUG customLogger MyApp.py in function test_logging:11 debug message +2022-10-17 16:07:00,329 - DEBUG customLogger MyApp.py in function test_logging:11 debug message +2022-10-17 16:09:09,439 - DEBUG customLogger MyApp.py in function test_logging:11 debug message +2022-10-17 16:09:46,014 - DEBUG customLogger MyApp.py in function test_logging:11 debug message +2022-10-17 16:11:15,239 - DEBUG customLogger MyApp.py in function test_logging:11 debug message +2022-10-17 16:11:15,280 - INFO customLogger MyApp.py in function test_logging:14 info message +2022-10-17 16:11:15,283 - WARNING customLogger MyApp.py in function test_logging:15 warning message +2022-10-17 16:11:15,292 - ERROR customLogger MyApp.py in function test_logging:16 error message +2022-10-17 16:11:15,295 - CRITICAL customLogger MyApp.py in function test_logging:17 critical message +2022-10-17 16:11:15,296 - INFO customLogger MyApp.py in function test_logging:19 Hello World +2022-10-17 16:11:15,298 - INFO customLogger MyApp.py in function test_logging:20 Request from nz-119 handled in 11.000 ms +2022-10-17 16:11:15,301 - INFO customLogger MyApp.py in function test_logging:21 Request from 127.0.0.1 handled in 33.100 ms +2022-10-17 16:11:32,442 - DEBUG customLogger MyApp.py in function test_logging:11 debug message +2022-10-17 16:11:32,466 - INFO customLogger MyApp.py in function test_logging:14 info message +2022-10-17 16:11:32,469 - WARNING customLogger MyApp.py in function test_logging:15 warning message +2022-10-17 16:11:32,474 - ERROR customLogger MyApp.py in function test_logging:16 error message +2022-10-17 16:11:32,477 - CRITICAL customLogger MyApp.py in function test_logging:17 critical message +2022-10-17 16:11:32,481 - INFO customLogger MyApp.py in function test_logging:19 Hello World +2022-10-17 16:11:32,483 - INFO customLogger MyApp.py in function test_logging:20 Request from nz-119 handled in 11.000 ms +2022-10-17 16:11:32,487 - INFO customLogger MyApp.py in function test_logging:21 Request from 127.0.0.1 handled in 33.100 ms diff --git a/MyApp.py b/MyApp.py index 3cd897b..8e9dd46 100644 --- a/MyApp.py +++ b/MyApp.py @@ -1,12 +1,16 @@ import socket -from helpers import customLogger +from helpers import customLogger, customMQTTLogger def test_logging(): logger = customLogger('customLogger','MyApp.log') + mqtt_logger = customMQTTLogger('customMQTTLogger', level='DEBUG', TOPIC='testLogging', BROKER='192.168.6.17') + logger.debug("debug message") + mqtt_logger.logger.debug("debug message") + logger.info("info message") logger.warning("warning message") logger.error("error message") diff --git a/helpers/MessageHandler.py b/helpers/MessageHandler.py new file mode 100644 index 0000000..383c837 --- /dev/null +++ b/helpers/MessageHandler.py @@ -0,0 +1,20 @@ +import logging +import paho.mqtt.client as mqttc + +class messageHandler(logging.Handler): + def __init__(self, TOPIC = "", BROKER = "", + PORT = "", QoSLevel = 1): + super(messageHandler, self).__init__() + self.TOPIC = TOPIC + self.BROKER = BROKER + self.PORT = PORT + self.QoSLevel = QoSLevel + + def emit(self, record): + logEntry = self.format(record) + logEntry = logEntry.replace(' ','') + logEntry = logEntry.replace('\n', '') + logEntry = logEntry.replace('\t','') + client = mqttc.Client("MessageHandler") + client.connect(self.BROKER, self.PORT, 60) + return(client.publish(self.TOPIC, bytes(logEntry + '\0', 'utf-8'), self.QoSLevel)) \ No newline at end of file diff --git a/helpers/__init__.py b/helpers/__init__.py index a79faf8..40684d4 100644 --- a/helpers/__init__.py +++ b/helpers/__init__.py @@ -6,5 +6,6 @@ # @desc [description] from .customLogger import customLogger +from .customMQTTLogger import customMQTTLogger -__all__ = ['customLogger'] \ No newline at end of file +__all__ = ['customLogger', 'customMQTTLogger'] \ No newline at end of file diff --git a/helpers/__pycache__/__init__.cpython-39.pyc b/helpers/__pycache__/__init__.cpython-39.pyc index 1a593e528fe502f524e6fd4eb02e2b5344c5f307..d23ca9e14cf58a2638b53078f1f611904bb913e2 100644 GIT binary patch delta 162 zcmX@h_?wY0k(ZZ?0SI=)`z9MQPUMrRR{`=;7*ZH>7;+h+7#KlprX0px#wbQ0n>mFg zohgbbg*BK#lkFuVP^l*4EuQ4k;*$JapZxUn)FL2X0K)eT3<-gV6)^!77BK?}KTT!? h=N5Z>d}2;ceEdp=B36(J5HYdVi-QG7^Dy!-0RWeSBXj@& delta 113 zcmey(c$Sebk(ZZ?0SKN*`6j<*n8+s+AO_^6Fr+Z%Fyt~sF)%WuFr_m_F{UsFGib8B zWCTiTGT!1zE-fy}&-KYqPfsl>Vgd^KX)=N7TkP@ii8(p(@hcgMSb*Y1EEAXcvVm0a GFaZFv))_(o diff --git a/helpers/__pycache__/customLogger.cpython-39.pyc b/helpers/__pycache__/customLogger.cpython-39.pyc index bfb0f72f33e0beb0e2c1c4b86e5b1469c4b100ea..7c17978630a5993b722fb50f289fd41d0fbf47cd 100644 GIT binary patch delta 200 zcmaE-@l=C1k(ZZ?0SIhkeUmFT^3G(Iv{O*kNXyJg%}dNp)ht%X%u`4!%}XxH%-_6? zxs!9Ui-5u8VqR&+m6NLkQDBn58n#n5sz3i+^j6$qM zl9ToeGCoz^!4(2<@SUXvXzn771@&^9nikeJFdMrR11we!#h%f>Y9h)Z#s4+73Ox_`Q mfpNj)PN4~Gfgrh{$%R6~tgb-jipl1}{fzCCj|