栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Django配置日志系统

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Django配置日志系统

Django配置日志系统

在项目主配置文件中加入本代码段

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,  # Disable an existing logger
    'formatters': {  # Log Information Format
        'standard': {  # Detailed log format
            'format': '[%(asctime)s][%(threadName)s:%(thread)d][task_id:%(name)s][%(filename)s:%(lineno)d]'
                      '[%(levelname)s][%(message)s]'
        },
        'simple': {  # Simple log format
            'format': '[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s'
        },
        'collect': {  # Define a special log format
            'format': '%(message)s'
        }
    },
    'filters': {  # Filter the logs
        'require_debug_true': {
            '()': 'django.utils.log.RequireDebugTrue',
        },
    },
    # Processor
    'handlers': {
        # Print at terminal
        'console': {
            'level': 'DEBUG',
            'filters': ['require_debug_true'],  # The log is printed on the screen only when Django debug is True
            'class': 'logging.StreamHandler',
            'formatter': 'simple',
        },
        # Default
        'default': {
            'level': 'INFO',
            'class': 'logging.handlers.RotatingFileHandler',  # Save to file
            'filename': os.path.join(base_DIR, "./project_information/logs/xzy_mall.log"),  # log_file Location
            'maxBytes': 1024 * 1024 * 30,  # log_size 30M
            'backupCount': 10,  # Maximum number of backups
            'formatter': 'standard',
            'encoding': 'utf-8',
        },
        # It is for error logging
        'error': {
            'level': 'ERROR',
            'class': 'logging.handlers.RotatingFileHandler',  # save to log file
            'filename': os.path.join(base_DIR, "./project_information/logs/xzy_mall.log"),  # log file
            'maxBytes': 1024 * 1024 * 30,  # log_size 30M
            'backupCount': 10,
            'formatter': 'standard',
            'encoding': 'utf-8',
        },
        # A specific log is defined to collect specific information
        'collect': {
            'level': 'INFO',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': os.path.join(base_DIR, "./project_information/logs/xzy_mall.log"),
            'maxBytes': 1024 * 1024 * 30,  # log_size 30M
            'backupCount': 10,
            'formatter': 'collect',
            'encoding': "utf-8"
        }
    },
    'loggers': {
        '': {  # The default logger application is configured as follows
            'handlers': ['default', 'console', 'error'],  # You can remove the console when you are online
            'level': 'DEBUG',
            'propagate': True,  # Pass to no higher level logger
        },
        'collect': {  # The 'collect' logger is also handled separately
            'handlers': ['console', 'collect'],
            'level': 'INFO',
        },
    },
}

最后在配置目录下创建文件夹logs与自定义日志文件(.log文件)

执行运行Django项目

成功则会显示如上日志信息

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/348513.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号