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("......")
相关推荐
AAA修煤气灶刘哥1 小时前
后端人速藏!数据库PD建模避坑指南
数据库·后端·mysql
RestCloud5 小时前
揭秘 CDC 技术:让数据库同步快人一步
数据库·api
得物技术8 小时前
MySQL单表为何别超2000万行?揭秘B+树与16KB页的生死博弈|得物技术
数据库·后端·mysql
可涵不会debug12 小时前
【IoTDB】时序数据库选型指南:工业大数据场景下的技术突围
数据库·时序数据库
ByteBlossom12 小时前
MySQL 面试场景题之如何处理 BLOB 和CLOB 数据类型?
数据库·mysql·面试
麦兜*12 小时前
MongoDB Atlas 云数据库实战:从零搭建全球多节点集群
java·数据库·spring boot·mongodb·spring·spring cloud
Slaughter信仰12 小时前
深入理解Java虚拟机:JVM高级特性与最佳实践(第3版)第十章知识点问答(10题)
java·jvm·数据库
麦兜*12 小时前
MongoDB 在物联网(IoT)中的应用:海量时序数据处理方案
java·数据库·spring boot·物联网·mongodb·spring
-Xie-13 小时前
Mysql杂志(十六)——缓存池
数据库·mysql·缓存