django日志

django日志_第1张图片

urls.py 

"""data_analyse_web URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/3.2/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path

from django.http import HttpResponse


def log(request):
    # 1.导入
    import logging
    # 2.创建日志器
    logger = logging.getLogger("django")  # settings里设置的日志器的名字
    # 3.调用日志器的方法来保存日志
    logger.info("用户登陆了")
    logger.warning("redis缓存不足")
    logger.error("数据保存在")
    logger.debug("~~~~~~~~~~~")
    return HttpResponse("log")


urlpatterns = [
    path('admin/', admin.site.urls),
    path("log/", log)
]

settings.py

# logging settings
LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,  # 是否禁用已经存在的日志器 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": "INFO",
            "filters": ["require_debug_true"],
            "class": "logging.StreamHandler",
            "formatter": "simple"

        },
        "file": {  # 向文件输出日志
            "level": "INFO",
            "class": "logging.handlers.RotatingFileHandler",
            "filename": os.path.join(BASE_DIR, "logs/web.log"),  # 日志文件的位置
            "maxBytes": 300 * 1024 * 1024,
            "backupCount": 10,
            "formatter": "verbose"
        }
    },
    "loggers": {  # 日治器
        "django": {  # 定义了一个名为django的日志器
            "handlers": ["console", "file"],  # 可同时向终端和日志中输出日志
            "propagate": True,  # 是否继续传递日志信息
            "level":"INFO",# 日志器接收的最低日志级别
        }
    }
}

 django日志_第2张图片

 

 

 

你可能感兴趣的:(django,django,logging)