I have a Django Application whose each API calls are associated with a transaction_id. I want to create separate log files for each transactions_id. In simple words I want to have multiple files which I will be using for logging.
How can i do this using Django's built in logging system ?
I can have multiple handlers in a single logger. But as per my requirement FileHandlers has to added in run time whose file name will be the transaction_id. This can be done. But the problem is if I have 4 transactions running at a time, 4 handlers will be added to the same logger, and according to documentation logs will be sent to each handlers resulting in 1 transaction log file logging the rest 3 transaction's logs as well.
Following is what I have come with:
class TransactionLogger:
def __init__(self, transaction_id):
self.logger = logging.getLogger('transaction_logger')
logger = self.logger
fileHandler = logging.FileHandler(transaction_id, mode='a')
formatter = logging.Formatter('%(levelname)s %(asctime)s %(filename)s:%(lineno)s - %(funcName)s() ] %(message)s')
fileHandler.setFormatter(formatter)
self.logger.addHandler(fileHandler)
self.logger.propagate = False
At the beginning of each transactions I instantiate the logger as :
logger = TransactionLogger(transaction_id).logger
and log as follows:
logger.debug("Hello World")
How can I maintain n number of log files which will be generated dynamically and log into each file based on transaction_id without interfering with other files.
Any help is appreciated.