Home > Back-end >  Django Logging user id
Django Logging user id

Time:04-21

hi I'm wondering if there's any way to log user id in django logging

and I want to store logs only for one view requests (store in database).

CodePudding user response:

Yes, you can log anything you want. I always prefer file based logging. Here is a snippet of file based logging,

 import logging
    logger = logging.getLogger(__name__)
    logger.setLevel(logging.INFO)
    formatter = logging.Formatter('%(levelname)s: %(name)s: %(message)s')
    
    file_handler = logging.FileHandler('directory_name/filename.log') # try to save log files in one directory 
    file_handler.setFormatter(formatter)
    logger.addHandler(file_handler)

and then you can log anything in by using logger.info(f" user is is {request.user.id}")

have a quick look on python logging that django used python logging

  • Related