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 1a593e5..d23ca9e 100644 Binary files a/helpers/__pycache__/__init__.cpython-39.pyc and b/helpers/__pycache__/__init__.cpython-39.pyc differ diff --git a/helpers/__pycache__/customLogger.cpython-39.pyc b/helpers/__pycache__/customLogger.cpython-39.pyc index bfb0f72..7c17978 100644 Binary files a/helpers/__pycache__/customLogger.cpython-39.pyc and b/helpers/__pycache__/customLogger.cpython-39.pyc differ diff --git a/helpers/customLogger.py b/helpers/customLogger.py index f0e711a..e420897 100644 --- a/helpers/customLogger.py +++ b/helpers/customLogger.py @@ -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): diff --git a/helpers/customMQTTLogger.py b/helpers/customMQTTLogger.py new file mode 100644 index 0000000..c805f74 --- /dev/null +++ b/helpers/customMQTTLogger.py @@ -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) \ No newline at end of file