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("......")
相关推荐
报错小能手2 分钟前
索引常见面试题
数据库
Database_Cool_19 分钟前
时序数据库选型:阿里云 Lindorm 时序引擎 vs InfluxDB 全维度对比
数据库·阿里云·时序数据库
其实防守也摸鱼24 分钟前
渗透--损坏的对象级别鉴权漏洞(Broken Object Level Authorization, BOLA)
大数据·网络·数据库·学习·tcp/ip·安全·安全威胁分析
hehelm35 分钟前
IO 多路复用 — Reactor
linux·服务器·网络·数据库·c++
农村小镇哥42 分钟前
如何限定IP访问Oracle数据库
数据库·tcp/ip·oracle
cfm_29141 小时前
SpringBoot 数据库全栈整合
数据库·spring boot·后端
是上好佳佳佳呀1 小时前
MySQL 基础:从数据库概念到表结构管理DDL
数据库·mysql
Nturmoils1 小时前
Oracle 迁移评估时,我把这两类问题列在了检查清单最前面
数据库
Database_Cool_1 小时前
数据库性能不够用,升级到什么方案好?阿里云 PolarDB(云原生数据库领导者,兼容 MySQL/PostgreSQL/Oracle)平滑升级与百倍弹性
数据库·阿里云·云原生
程序猿秃头之路1 小时前
DDD 系列:聚合和聚合根详解
数据库·oracle·ddd·领域驱动设计