可參考的鏈接 http://www.cnblogs.com/qwj-sysu/p/4218536.html http://davidbj.blog.51cto.com/4159484/1433741 import logging import django.utils.log import logging.handlershtml
LOGGING = { 'version': 1, 'disable_existing_loggers': True, 'formatters': { 'standard': { 'format': '%(asctime)s [%(threadName)s:%(thread)d] [%(name)s:%(lineno)d] [%(module)s:%(funcName)s] [%(levelname)s]- %(message)s'} #日誌格式 }, 'filters': { }, 'handlers': { 'mail_admins': { 'level': 'ERROR', 'class': 'django.utils.log.AdminEmailHandler', 'include_html': True, }, 'default': { 'level':'DEBUG', 'class':'logging.handlers.RotatingFileHandler', 'filename': 'log/all.log', #日誌輸出文件 'maxBytes': 102410245, #文件大小 'backupCount': 5, #備份份數 'formatter':'standard', #使用哪一種formatters日誌格式 }, 'error': { 'level':'ERROR', 'class':'logging.handlers.RotatingFileHandler', 'filename': 'log/error.log', 'maxBytes':102410245, 'backupCount': 5, 'formatter':'standard', }, 'console':{ 'level': 'DEBUG', 'class': 'logging.StreamHandler', 'formatter': 'standard' }, 'request_handler': { 'level':'DEBUG', 'class':'logging.handlers.RotatingFileHandler', 'filename': 'log/script.log', 'maxBytes': 102410245, 'backupCount': 5, 'formatter':'standard', }, 'scprits_handler': { 'level':'DEBUG', 'class':'logging.handlers.RotatingFileHandler', 'filename':'log/script.log', 'maxBytes': 102410245, 'backupCount': 5, 'formatter':'standard', } }, 'loggers': { 'django': { 'handlers': ['default', 'console'], 'level': 'DEBUG', 'propagate': False }, 'django.request': { 'handlers': ['request_handler'], 'level': 'DEBUG', 'propagate': False, }, 'scripts': { 'handlers': ['scprits_handler'], 'level': 'INFO', 'propagate': False }, 'blog.views': { 'handlers': ['default', 'error'], 'level': 'DEBUG', 'propagate': True }, 'blog.util':{ 'handlers': ['error'], 'level': 'ERROR', 'propagate': True } } } 日誌的保存路徑能夠寫相對路徑,也能夠寫絕對路徑 彙報編碼錯誤 添加 # -- coding: utf-8 -- 而後在views.py中定義日誌器 import logging logger = logging.getLogger(blog.views) 捕獲日誌 def index(request): try: with open('test.txt','r') as file: except Exception as e: logger.error(e) return render_to_response('index.html',context_instance=RequestContext(request))django