I have an HTTP Server class, supporting multithreading, that while writing back the result to the client, it writes some logs (Logging module) as well. It slows down the request, and since the POST/GET methods returns only after the 'return' occures, I have to write the log before i send the result. I wish to do it by opening another thread that will receive the message, and write the log. I wish to have my Logging object as 'static', or a class member which is one and only, and is being shared to all threads that can acquire a lock on it and use it.
import logging
LOG_FILENAME = 'babyServer.log'
class Logger(object):
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(levelname)-8s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
filename='serverLog.log',
filemode='wa')
@classmethod
def Write(cls,level,log):
level = level.lower()
if level == 'error':
logging.error(log)
if level == 'info':
logging.info(log)
if level == 'exception':
logging.exception(log)
So from somewhere outside of this file, I have currently a call such as Logger.Write('info', "some info") Instead, I wish that this call will actually open a new thread, that owns a reference to the single Logging I configured in the Logger class.