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",# 日志器接收的最低日志级别
        }
    }
}
相关推荐
B站_计算机毕业设计之家1 小时前
python汽车数据分析可视化系统 爬虫 懂车帝 毕业设计 Django框架 vue框架 大数据✅
爬虫·python·数据分析·django·汽车·推荐算法·懂车帝
yzx99101312 小时前
数据库完整指南:从基础到 Django 集成
数据库·python·django
蓑笠翁0011 天前
从零开始学习Python Django:从环境搭建到第一个 Web 应用
python·学习·django
yzx9910131 天前
Django 配置与安装完整指南
数据库·django·sqlite
m0_741585353 天前
Django开发环境
python·django
weixin_446260854 天前
Django - 让开发变得简单高效的Web框架
前端·数据库·django
闲人编程5 天前
使用Django从零开始构建一个个人博客系统
后端·python·django·接口·restful·web·个人博客
fangeqin5 天前
解决django.db.utils.OperationalError: attempt to write a readonly database错误
django·apache·sqlite3·selinux
做运维的阿瑞5 天前
从入门到精通:Django的深度探索之旅
开发语言·后端·python·系统架构·django
计算机毕设残哥6 天前
基于Hadoop+Spark的商店购物趋势分析与可视化系统技术实现
大数据·hadoop·python·scrapy·spark·django·dash