0

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?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
BetaTester
  • 25
  • 8
  • Does this answer your question? [Logging to two files with different settings](https://stackoverflow.com/questions/11232230/logging-to-two-files-with-different-settings) – Abdul Aziz Barkat Sep 17 '21 at 10:49
  • @AbdulAzizBarkat thanks for suggesting. But this is a different case. We will have a new file for each day. An issue arrives when two users call one API at the same time. What I have observed is that the object which has been used in middleware is being overwritten by a new user. So is there any way I can make sure the object remains the same until the request ends for the first user? – BetaTester Sep 17 '21 at 11:04
  • `logging.basicConfig` configures the root logger, i.e. you are using the same logger and configuring it, of course race conditions will happen in this case. The above duplicate shows how to make multiple loggers that log to different files and hence does answer your question. – Abdul Aziz Barkat Sep 17 '21 at 11:13
  • You are right but in that case, for every user, I have to create a new logger object and have to call this object across the application. That's not the correct way I guess. – BetaTester Sep 17 '21 at 16:50

0 Answers0