简体   繁体   中英

Django: How to create user specific logs?

I am developing a django web application supporting multiple users. One requirement of the application is to have user specific log files. As example, if there are users named 'xyz' and 'abc', then there should be log files corresponding to 'xyz.log' and 'abc.log'. As of now I am able to create a log file for the entire application by having the following additions in my settings.py :

import logging
logging.basicConfig(level=logging.DEBUG,format='%(asctime)s ::%(funcName)s ::[%(levelname)s] ::%(message)s',datefmt='%a, %d %b %Y %H:%M:%S',filename='/project.log', filemode='w')
.
.
.
//some other settings
.
.
.
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        },

        'console':{
            'level':'ERROR',
            'class':'logging.StreamHandler'
           }
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },

        'django': {
        'handlers':['console'],
        'propagate': True,
        'level':'ERROR',
    },
    }
}

How can I do the same for specific users? Following is my relevant views.py :

def login_check(request):
    if request.method == 'POST':
        username = request.POST['username']
        password = request.POST['password']
        try:
            user = Application_User.objects.get(user_name=username, passwd=password)
        except Application_User.DoesNotExist:
            error_message = "**Incorrect login. Please try again."
            context = {'error_message' : error_message}
            return render_to_response('vsawebauto/login.html',context, context_instance=RequestContext(request))
        else:
            //this is where I need to create the logger file and begin user specific logging

I guess you need to add a handler to the logger in the view. You might make a logger just for that handler, and use it for logging user-related events. The logging library is a standard python library, most of the documentation is for configuring at process startup time in a declarative way (eg, via data structure) but you can call functions to do the setup you want, which means you can do it in the view.

http://docs.python.org/2/library/logging.html#logging.Logger.addHandler
http://docs.python.org/2/library/logging.html#handler-objects
http://docs.python.org/2/library/logging.handlers.html#filehandler

Sorry, I don't know it well enough to give you a code sample, but at least I know where to find it in the documentation.

I'm not sure what will happen if there are simultaneous logins for the same user from different sessions though (eg, if a user has trouble logging in from one browser, and tries another).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM