一. Django中使用日志
Django中使用日志其實非常簡單,只需要在項目使用的配置文件中(如果沒有自定義,那么就是settings.py中)加以下設置即可,同時可以根據自己的需求進行修改:
# 官網:https://docs.djangoproject.com # 中文loggin配置:https://docs.djangoproject.com/zh-hans/2.2/topics/logging/ LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'formatters': { 'verbose': { 'format': '%(levelname)s %(asctime)s %(module)s %(lineno)d %(message)s' }, 'simple': { 'format': '%(levelname)s %(module)s %(lineno)d %(message)s' }, }, 'filters': { 'require_debug_true': { '()': 'django.utils.log.RequireDebugTrue', }, }, 'handlers': { 'console': { 'level': 'DEBUG', 'filters': ['require_debug_true'], 'class': 'logging.StreamHandler', 'formatter': 'simple' }, 'file': { # 實際開發建議使用WARNING 'level': 'INFO', 'class': 'logging.handlers.RotatingFileHandler', # 日志位置,日志文件名,日志保存目錄必須手動創建,然后給對應的路徑即可 注:這里的文件路徑要注意BASE_DIR 'filename': os.path.join(os.path.dirname(BASE_DIR), "logs/manage.log"), # 日志文件的最大值,這里我們設置300M 'maxBytes': 300 * 1024 * 1024, # 日志文件的數量,設置最大日志數量為10 'backupCount': 10, # 日志格式:詳細格式 'formatter': 'verbose', # 設置日志中的編碼 'encoding': 'utf-8' }, }, # 日志對象 'loggers': { 'django': { 'handlers': ['console', 'file'], 'propagate': True, # 是否讓日志信息繼續冒泡給其他的日志處理系統 }, } }