Django系列之日志配置

如何配置

settings.py 文件中增加如下日志模块

python 复制代码
"""logger 配置"""
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,  # 是否去掉目前项目中其他地方中以及使用的日志功能,但是将来我们可能会引入第三方的模块,里面可能内置了日志功能,所以尽量不要关闭。
    'formatters': {
        'verbose': {
            'format': '{asctime} {levelname} PID:{process:d} TID:{thread:d} {filename} line:{lineno} {funcName} {message}',
            'style': '{',
        },
        'simple': {
            'format': '{asctime} {levelname} {filename} {lineno} {message}',
            'style': '{',
        },
    },
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse',
        },
        '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, "application.log"),  # 注意,你的文件应该有读写权限。
            'maxBytes': 300 * 1024 * 1024,  # 日志文件的最大值,这里我们设置300M
            'backupCount': 10,      # 日志文件的数量,设置最大日志数量为10
            'formatter': 'verbose',   # 日志格式:详细格式
            'encoding': 'utf-8',  # 设置默认编码,否则打印出来汉字乱码
        },
        'mail_admins': {
            'level': 'ERROR',
            'class': 'django.utils.log.AdminEmailHandler',
            'filters': ['require_debug_false']
        }
    },
    'loggers': {
        'django': {
            'handlers': ['console'],
            'propagate': True,
        },
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
        'application': {
            'handlers': ['console', 'file'],
            'level': 'INFO',
            'filters': ['require_debug_true']
        }
    }
}

如何调用

python 复制代码
import logging
logger = logging.getLogger("application")

logger.info("......")
相关推荐
Q_Q51100828544 分钟前
python+uniapp基于微信小程序团购系统
spring boot·python·微信小程序·django·uni-app·node.js·php
小糖学代码1 小时前
MySQL:14.mysql connect
android·数据库·mysql·adb
爬山算法2 小时前
Redis(69)Redis分布式锁的优点和缺点是什么?
数据库·redis·分布式
RestCloud2 小时前
从数据库到价值:ETL 工具如何打通南大通用数据库与企业应用
数据库
惜月_treasure2 小时前
Text2SQL与工作流实现:让数据库查询变得轻松又高效
数据库·人工智能·python
-睡到自然醒~3 小时前
[go 面试] 并发与数据一致性:事务的保障
数据库·面试·golang
为乐ovo3 小时前
19.DCL-用户管理
数据库
一个天蝎座 白勺 程序猿3 小时前
金仓数据库KingbaseES实现MongoDB平滑迁移全攻略:从架构适配到性能调优的完整实践
数据库·mongodb·数据迁移·kingbasees·金仓数据库
武子康3 小时前
Java-153 深入浅出 MongoDB 全面的适用场景分析与选型指南 场景应用指南
java·开发语言·数据库·mongodb·性能优化·系统架构·nosql
2401_837088503 小时前
Redis通用命令
数据库·redis·缓存