adding simple mqtt logger
This commit is contained in:
parent
f76b355c16
commit
ffbc76ff39
44
MyApp.log
Normal file
44
MyApp.log
Normal file
@ -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
|
6
MyApp.py
6
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")
|
||||
|
20
helpers/MessageHandler.py
Normal file
20
helpers/MessageHandler.py
Normal file
@ -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))
|
@ -6,5 +6,6 @@
|
||||
# @desc [description]
|
||||
|
||||
from .customLogger import customLogger
|
||||
from .customMQTTLogger import customMQTTLogger
|
||||
|
||||
__all__ = ['customLogger']
|
||||
__all__ = ['customLogger', 'customMQTTLogger']
|
Binary file not shown.
Binary file not shown.
@ -22,8 +22,8 @@ class customLogger(object):
|
||||
if cls._logger is None:
|
||||
cls._logger = logging.getLogger(name)
|
||||
# Define a Logging Format
|
||||
_format = _format = ColorizedArgsFormatter('%(asctime)s - %(levelname)-8s ' + name + ' %(filename)s in %(funcName)s:%(lineno)d %(message)s')
|
||||
_file_format = LogfileFormatter('%(asctime)s - %(levelname)-8s ' + name + ' %(filename)s in %(funcName)s:%(lineno)d %(message)s')
|
||||
_format = _format = ColorizedArgsFormatter('%(asctime)s - %(levelname)-8s ' + name + ' %(filename)s in function %(funcName)s:%(lineno)d %(message)s')
|
||||
_file_format = LogfileFormatter('%(asctime)s - %(levelname)-8s ' + name + ' %(filename)s in function %(funcName)s:%(lineno)d %(message)s')
|
||||
# Create Console Output
|
||||
_handler = logging.StreamHandler(sys.stdout)
|
||||
# Create Logfile Output
|
||||
@ -110,11 +110,10 @@ class ColorizedArgsFormatter(logging.Formatter):
|
||||
|
||||
class LogfileFormatter(logging.Formatter):
|
||||
|
||||
def __init__(self, fmt: str):
|
||||
def __init__(self, fmt:str):
|
||||
super().__init__()
|
||||
|
||||
_format = fmt
|
||||
self.formatter = logging.Formatter(_format)
|
||||
|
||||
self.formatter = logging.Formatter(fmt)
|
||||
|
||||
@staticmethod
|
||||
def rewrite_record(record: logging.LogRecord):
|
||||
|
23
helpers/customMQTTLogger.py
Normal file
23
helpers/customMQTTLogger.py
Normal file
@ -0,0 +1,23 @@
|
||||
import logging
|
||||
import bunyan
|
||||
from .MessageHandler import messageHandler
|
||||
|
||||
class customMQTTLogger():
|
||||
"""
|
||||
Description: Returns a logger with bunyan formatted output
|
||||
Logger emits the messages to a mqtt client
|
||||
|
||||
Function Call: customLogger(loggerName="", level="", TOPIC = "",
|
||||
BROKER="", PORT="", QoSLevel="")
|
||||
|
||||
Returns: A custom logger object similar to python logger.
|
||||
"""
|
||||
def __init__(self, loggerName="MESSAGE LOGGER", level="DEBUG", TOPIC = "",
|
||||
BROKER = "", PORT = 1883, QoSLevel = 1):
|
||||
self.logger = logging.getLogger(loggerName)
|
||||
self.logger.setLevel(level)
|
||||
handler = messageHandler(TOPIC=TOPIC, BROKER=BROKER,
|
||||
PORT=PORT, QoSLevel=QoSLevel)
|
||||
formatter = bunyan.BunyanFormatter()
|
||||
handler.setFormatter(formatter)
|
||||
self.logger.addHandler(handler)
|
Loading…
Reference in New Issue
Block a user