I have an application where I need to create a folder for each user in which they will have their day-wise logs. I have added created custom middleware which will call my custom_logger file for creating logs file for each user. Below is my middleware auth_middleware.py
from .custom_logger import LogClass
log_obj=LogClass()
def simple_middleware(get_response):
def middleware(request):
user_token=request.headers['userid']
user_name=request.headers['username']
page_name=request.headers['pagename']
log_obj.log_setting(str(user_name),str(page_name))
return response
return middleware
I am calling my custom_logger.py from middleware.
class LogClass:
def log_setting(self,user_name,page_name):
current_date = datetime.date.today() #set variable which store current date
log_path = './logs/' + user_name #log path of user folder
if os.path.exists(log_path): #check user folder is already exist or not
pass
else:
os.mkdir(log_path) #use to create user folder with username
filename=str(current_date) +'.log' #set file name with current date
logging.basicConfig(level=logging.DEBUG,filename=log_path+'/'+ filename,force=True, format='%(asctime)s : %(levelname)s : '+page_name+' : %(pathname)s : %(lineno)s: %(funcName)s : %(message)s')
And after this, I import logging in every file and it works fine if one user work at a time for more than one user concurrently it's not working.
If two users parallelly call one API the first user will have initial logs then all the logs of the first user and the second user will come under the second user.
Is there any way by which I can make sure that logs go in the right file?