django日志

urls.py

python 复制代码
"""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

python 复制代码
# 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",# 日志器接收的最低日志级别
        }
    }
}
相关推荐
言乐64 小时前
Python游戏水平测试辅助系统2
开发语言·windows·python·游戏·django
vx-程序开发11 小时前
springboot旅游推介平台---附源码24175
java·spring boot·python·spring cloud·eclipse·django·idea
言乐614 小时前
Python游戏水平测试辅助系统
开发语言·python·游戏·django·pygame
小白勇闯网安圈20 小时前
Django 响应对象、文件上传与类视图
数据库·django·sqlite
小白勇闯网安圈1 天前
Django 模板复用、ORM 查询与多对多关系
数据库·python·django
vx-程序开发1 天前
django汽车租赁系统---附源码25360
java·javascript·spring boot·python·eclipse·django·php
SelectDB技术团队2 天前
阶跃星辰 Agent 可观测:Apache Doris / SelectDB 的技术能力与实践
django·apache·知识图谱
2401_868534783 天前
MATLAB:车牌识别
python·django
独码侠3 天前
Dify 知识库索引(中):打开源码,拆穿关于“经济索引”的三个谣言
后端·python·django·embedding·知识库·索引·dify
叫我王富贵i4 天前
0基础学Django
后端·python·django