The logging module offers the possibility of using HTTPHandler, which does not align to my requirement due to the limitations with formatting.
As given in the documentation, https://docs.python.org/3/library/logging.handlers.html, Using setFormatter() to specify a Formatter for a HTTPHandler has no effect.
My aim is to Log events in my application, and collect them on a local server. I am using JSON-Server to mock REST API (https://github.com/typicode/json-server). I have referred to this link : How to set up HTTPHandler for python logging, as a possible solution but i am not able to get what is desired.
My code:
"""class CustomHandler(logging.handlers.HTTPHandler):
def __init__(self):
logging.handlers.HTTPHandler.__init__(self)
def emit(self, record):
log_entry = self.format(record)
# some code....
url = 'http://localhost:3000/posts'
# some code....
return requests.post(url, log_entry, json={"Content-type": "application/json"}).content """
def custom_logger(name):
logger = logging.getLogger(name)
formatter_json = jsonlogger.JsonFormatter(
fmt='%(asctime)s %(levelname)s %(name)s %(message)s')
requests.post('http://localhost:3000/posts', json= {"message" : "1" } )
filehandler_all = logging.FileHandler('test.log')
filehandler_all.setLevel(logging.DEBUG)
filehandler_all.setFormatter(formatter_json)
logger.addHandler(filehandler_all)
#http_handler = logging.handlers.HTTPHandler('http://localhost:3000' ,
#"/posts", "POST")
#
# http_handler = CustomHandler()
# http_handler.setFormatter(formatter_json)
# http_handler.setLevel(logging.DEBUG)
return logger
logger = custom_logger("http")
logger.exception("{'sample json message' : '2'}")
The comments are for testing, and ease of reproduction of code.
In the above code snippet, the filehandler handles the json files perfectly, but the HTTPHandler does not. I tried creating a CustomHandler as specified in the link which in principle should work but I am not able to figure it out the details.
Will constructing a CustomHandler with changes to the "mapLogRecord" and the "emit" methods make sense?.
The most important thing, is to get the data in JSON Format.
Any other ideas to approach this can also be helpful!