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("......")
相关推荐
ShineWinsu5 小时前
对于MySQL:数据库的操作的解析
linux·数据库·c++·mysql·面试·笔试·库的操作
‎ദ്ദിᵔ.˛.ᵔ₎5 小时前
MySQL 数据类型
数据库·mysql
这个DBA有点耶5 小时前
索引合并不是万能药:MySQL同时用两个索引,为什么比不用还慢?
数据库·mysql·dba
Aime_Perfect6 小时前
sqlserver AlwaysOn(一)
数据库·sqlserver
HAYDENR6 小时前
毛利率波动大可能是什么原因?毛利率变动怎么判断是否合理?
数据库
大牧师6 小时前
MySQL 学习教程
数据库·sql·mysql·docker·node·全栈·后端数据
Elastic 中国社区官方博客6 小时前
如何通过一条 ES|QL 查询为 Elasticsearch 中的每个指标构建指标图表
大数据·运维·数据库·elasticsearch·搜索引擎·全文检索·kibana
数据库小学妹7 小时前
MySQL redo刷盘实测:innodb_flush_log_at_trx_commit与故障矩阵
运维·数据库·mysql
ByteRock7 小时前
ClickHouse 表的生老“并”死:表实例、表元数据与并发 DDL
数据库
哈__7 小时前
破除数据库排障碎片化:一体化全链路故障根因诊断实践
数据库