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",# 日志器接收的最低日志级别
        }
    }
}
相关推荐
ma_de_hao_mei_le10 小时前
ntquerysystemiunfomation 数据传递
django
Muyuan199813 小时前
22.让 RAG Agent 更像真实产品:聊天页面优化、PDF 上传、知识库重建与检索片段展示
python·django·pdf·fastapi
Muyuan199814 小时前
25.Paper RAG Agent 优化记录:上传反馈、计算器安全与 Chunk 参数调整
python·安全·django·sqlite·fastapi
Muyuan199818 小时前
26.Paper RAG Agent 展示面收口:截图与项目表达更新记录
人工智能·python·django·fastapi
曲幽2 天前
用了loguru我才明白,Python日志还能这么写
python·logging·fastapi·web·async·loguru·handler·uvicorn
毕胜客源码2 天前
卷积神经网络的手势识别系统(有技术文档)深度学习 图像识别 卷积神经网络 Django python 人工智能
人工智能·python·深度学习·cnn·django
我叫Double3 天前
遗留-----
django
码农阿豪3 天前
Django接金仓数据库:我踩过的坑和填坑指南
数据库·python·django
神仙别闹3 天前
基于Python(Django)+MySQL 实现(Web)SQL智能检测系统的设计与实现
python·mysql·django
z小天才b4 天前
Django ORM、中间件与信号 — 完全指南
python·中间件·django