程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

【Django】打印日志logger-20220803

編輯:Python

效果:打印日志logger並形成log文件

思路:

1.先在settings.py設定

LOGGING = {

'version': 1,
'disable_existing_loggers': False,
'formatters': {

'simple': {
 # exact format is not important, this is the minimum information
'format': '%(asctime)s %(name)-12s %(lineno)d %(levelname)-8s %(message)s',
},
},
'handlers': {

'console': {

'class': 'logging.StreamHandler',
},
# 'mail_admins': { # Add Handler for mail_admins for `warning` and above
# 'level': 'ERROR',
# 'class': 'django.utils.log.AdminEmailHandler',
# },
'file': {

'class': 'logging.FileHandler',
'filename': os.path.join(LOG_DIR, 'admin.log'),
},
},
'root': {

'handlers': ['console', 'file'],
'level': 'INFO',
},
}

2.admin.py


def save_model(self, request, obj, form, change):
if change:
# print('self',self)
# print('request',request)
# print('obj',obj)
# print('form',form)
# print("form.cleaned_data['project_name']",form.cleaned_data['project_name'])
# print('change',change)
logger.info("%s %s modified the data of %s " % (datetime.now(),request.user.username, obj.project_name))
super().save_model(request, obj, form, change)

3.views.py


# dashboard
def dashboard(request):
# 篩選數據
sitedata = request.POST.get('sitedata', '')
statusdata = request.POST.get('statusdata', '')
project_all = project.objects.all()
project_items = project_all
if sitedata:
# 如果sitedata存在
if sitedata!='---':
project_items = project_items & project_all.filter(Q(project_dept=sitedata))
else:
project_items = project_items & project_all
else:
# 如果sitedata不存在
project_items = project_items & project_all
if statusdata:
# 如果statusdata存在
if statusdata!='---':
project_items = project_items & project_all.filter(Q(project_status=statusdata))
else:
project_items = project_items & project_all
else:
# 如果statusdata不存在
project_items = project_items & project_all
# 排序
project_items = project_items.order_by('-project_owner_name','project_plan_start_date')
logger.info("%s %s query %s pcs data" % (datetime.now(),request.user.username, len(project_items)))
return render(request, 'dashboard.html',locals())

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved