1 知识点
主要是使用日志记录各类运行信息,方便排错
2 日志设置并测试
在配置文件中定义handlers和loggers的类型【settings.py】
# 自定义日志输出信息
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'formatters': { # 日志格式
'standard': {
# 2016-12-24 14:51:19,888 [Thread-2:7120] [blog.views:28] [views:index] [ERROR]- [Errno 2] No such file or directory: 'ss.txt'
'format': '%(asctime)s [%(threadName)s:%(thread)d] [%(name)s:%(lineno)d] [%(module)s:%(funcName)s] [%(levelname)s]- %(message)s'}
},
'filters': { # Filter 用于对从logger 传递给handler 的日志记录进行额外的控制。
},
'handlers': {
'mail_admins': { # 它将用邮件发送ERROR(和更高级)的消息到站点管理员
'level': 'ERROR',
'class': 'django.utils.log.AdminEmailHandler',
'include_html': True,
},
'default': {
'level': 'DEBUG',
'class': 'logging.handlers.RotatingFileHandler',
'filename': 'log/all.log', # 日志输出文件位置
'maxBytes': 1024 * 1024 * 5, # 文件大小
'backupCount': 5, # 备份份数
'formatter': 'standard', # 使用哪种formatters日志格式
},
'error': {
'level': 'ERROR',
'class': 'logging.handlers.RotatingFileHandler',
'filename': 'log/error.log', # 日志输出文件位置
'maxBytes': 1024 * 1024 * 5, # 文件大小
'backupCount': 5, # 备份份数
'formatter': 'standard', # 这个handler 使用simple 输出格式。
},
'console': { # 一个StreamHandler,它将打印DEBUG(和更高级)的消息到stderr。
'level': 'DEBUG', # DEBUG:用于调试目的的底层系统信息
'class': 'logging.StreamHandler',
'formatter': 'standard' # 这个handler 使用simple 输出格式。
},
'request_handler': {
'level': 'DEBUG',
'class': 'logging.handlers.RotatingFileHandler',
'filename': 'log/script.log',
'maxBytes': 1024 * 1024 * 5,
'backupCount': 5,
'formatter': 'standard',
},
'scprits_handler': {
'level': 'DEBUG',
'class': 'logging.handlers.RotatingFileHandler',
'filename': 'log/script.log',
'maxBytes': 1024 * 1024 * 5,
'backupCount': 5,
'formatter': 'standard',
}
},
'loggers': {
'django': { # django将DEBUG级以上的内容交给default和console
'handlers': ['default', 'console'],
'level': 'DEBUG',
'propagate': False
},
'django.request': { # django.request将DEBUG级别以上的内容交给request_handler
'handlers': ['request_handler'],
'level': 'DEBUG',
'propagate': False,
},
'scripts': { # scripts将INFO级别以上的内容交给scprits_handler
'handlers': ['scprits_handler'],
'level': 'INFO',
'propagate': False
},
'blog.views': { # blog.views将DEBUG级别以上的内容交给default和error
'handlers': ['default', 'error'],
'level': 'DEBUG',
'propagate': True
},
}
}
在视图函数中使用并测试【views.py】
import logging
logging = logging.getLogger('blog.views')
正常启动
(env_py27_django_blog) D:\MaiZi_Edu\Dropbox\Maizi\Django_blog\django_blog>python manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
2017-01-02 11:40:30,528 [Dummy-1:10040] [django.db.backends:89] [utils:execute] [DEBUG]- (0.005) QUERY = "\n SELECT name, type FROM sqlite_master\n WHERE type in ('table', 'view') AND NOT name='sqlite_sequence'\n ORDER BY name" - PARAMS = (); args=None
2017-01-02 11:40:30,558 [Dummy-1:10040] [django.db.backends:89] [utils:execute] [DEBUG]- (0.000) QUERY = u'SELECT "django_migrations"."app", "django_migrations"."name" FROM "django_migrations"' - PARAMS = (); args=()
You have unapplied migrations; your app may not work properly until they are applied.
Run 'python manage.py migrate' to apply them.
January 02, 2017 - 11:40:30
Django version 1.8.2, using settings 'django_blog.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
3 测试异常捕获,并查看日志记录
import logging
from django.shortcuts import render
logging = logging.getLogger('blog.views')
# Create your views here.
def index(request):
try:
file = open('ss.txt', 'r')
except Exception as e:
logging.error(e)
return render(request, 'index.html', locals())
日志记录如下:
2016-12-24 14:47:51,405 [Thread-12:11256] [blog.views:28] [views:index] [ERROR]- [Errno 2] No such file or directory: 'ss.txt'
2016-12-24 14:48:47,892 [Thread-2:5416] [blog.views:28] [views:index] [ERROR]- [Errno 2] No such file or directory: 'ss.txt'
2016-12-24 14:51:19,888 [Thread-2:7120] [blog.views:28] [views:index] [ERROR]- [Errno 2] No such file or directory: 'ss.txt'
4 遇到的问题
4.1 文件夹权限不足,无法创建文件
(env_py27_django_blog) D:\MaiZi_Edu\Dropbox\Maizi\Django_blog\django_blog>python manage.py runserver
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "D:\virtualenv\env_py27_django_blog\lib\site-packages\django\core\management\__init__.py", line 338, in execute_from_command_line
utility.execute()
File "D:\virtualenv\env_py27_django_blog\lib\site-packages\django\core\management\__init__.py", line 312, in execute
django.setup()
File "D:\virtualenv\env_py27_django_blog\lib\site-packages\django\__init__.py", line 17, in setup
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
File "D:\virtualenv\env_py27_django_blog\lib\site-packages\django\utils\log.py", line 86, in configure_logging
logging_config_func(logging_settings)
File "C:\Python27\Lib\logging\config.py", line 794, in dictConfig
dictConfigClass(config).configure()
File "C:\Python27\Lib\logging\config.py", line 576, in configure
'%r: %s' % (name, e))
ValueError: Unable to configure handler 'default': [Errno 2] No such file or directory: 'D:\\MaiZi_Edu\\Dropbox\\Maizi\\Django_blog\\django_blog\\log\\all.log'
相关下载
欢迎留言,博文会持续更新~~