我正在開發一個支持多個用戶的django web應用程序。應用程序的一個要求是具有用戶特定的日誌文件。例如,如果有用戶名爲'xyz'和'abc',那麼應該有對應'xyz.log'和'abc.log'的日誌文件。截至目前,我可以通過在我的settings.py增加了以下創建爲整個應用程序日誌文件:Django:如何創建用戶特定的日誌?
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',
},
}
}
我怎樣才能做同樣的爲特定用戶?以下是我的相關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