zoukankan      html  css  js  c++  java
  • Django---logging

    import os
    
    当把django运行在后台,setsid ,保错OSError:这是因为代码有打印的代码,现在没有地方打印报错  ,在命令最后 > a.log。(这样做虽然可以避免报错,但最后获取不了打印内容)
    
    配置setting.py配置文件

      import os
         if not os.path.exists('foldername'):
         os.mkdir('foldername')

    
    BASE_LOG_DIR = os.path.join(BASE_DIR, "log")
    LOGGING = {
        'version': 1,  # 保留字
        'disable_existing_loggers': False,  # 禁用已经存在的logger实例
        # 日志文件的格式
        'formatters': {
            # 详细的日志格式
            'standard': {
                'format': '[%(asctime)s][%(threadName)s:%(thread)d][task_id:%(name)s][%(filename)s:%(lineno)d]'
                          '[%(levelname)s][%(message)s]'
            },
            # 简单的日志格式
            'simple': {
                'format': '[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s'
            },
            # 定义一个特殊的日志格式
            'collect': {
                'format': '%(message)s'
            }
        },
        # 过滤器
        'filters': {
            'require_debug_true': {
                '()': 'django.utils.log.RequireDebugTrue',
            },
        },
        # 处理器
        'handlers': {
            # 在终端打印
            'console': {
                'level': 'DEBUG',
                'filters': ['require_debug_true'],  # 只有在Django debug为True时才在屏幕打印日志
                'class': 'logging.StreamHandler',  #
                'formatter': 'simple'
            },
            # 默认的
            'default': {
                'level': 'INFO',
                'class': 'logging.handlers.RotatingFileHandler',  # 保存到文件,自动切
                'filename': os.path.join(BASE_LOG_DIR, "aidcs_info.log"),  # 日志文件
                'maxBytes': 1024 * 1024 * 50,  # 日志大小 50M
                'backupCount': 3,  # 最多备份几个
                'formatter': 'standard',
                'encoding': 'utf-8',
            },
            # 专门用来记错误日志
            'error': {
                'level': 'ERROR',
                'class': 'logging.handlers.RotatingFileHandler',  # 保存到文件,自动切
                'filename': os.path.join(BASE_LOG_DIR, "aidcs_err.log"),  # 日志文件
                'maxBytes': 1024 * 1024 * 50,  # 日志大小 50M
                'backupCount': 5,
                'formatter': 'standard',
                'encoding': 'utf-8',
            },
            # 专门定义一个收集特定信息的日志
            'collect': {
                'level': 'INFO',
                'class': 'logging.handlers.RotatingFileHandler',  # 保存到文件,自动切
                'filename': os.path.join(BASE_LOG_DIR, "aidcs_collect.log"),
                'maxBytes': 1024 * 1024 * 50,  # 日志大小 50M
                'backupCount': 5,
                'formatter': 'collect',
                'encoding': "utf-8"
            },
            # 用于文件输出
            'file_handler': {
                'level': 'INFO',
                'class': 'logging.handlers.TimedRotatingFileHandler',
                'filename':os.path.join(BASE_LOG_DIR, "aidcs_all.log"),
                'formatter':'standard'
             },
            'mail_admins': {
                 'level': 'ERROR',
                 'class': 'django.utils.log.AdminEmailHandler',
                  'formatter':'standard'
             },
    
        },
        'loggers': {
           # 默认的logger应用如下配置
            '': {
                'handlers': ['default', 'console', 'error'],  # 上线之后可以把'console'移除
                'level': 'DEBUG',
                'propagate': True,  # 向不向更高级别的logger传递
            },
            # 名为 'collect'的logger还单独处理
            'collect': {
                'handlers': ['console', 'collect'],
                'level': 'INFO',
            },
            #  注意:loggers类型为"django"这将处理所有类型日志。sourceDns.webdns.views 应用的py文件
            'django': {
                 'handlers': ['console','file_handler'],
                 'level':'DEBUG',
                 'propagate': True,
             },
             'django.request': {
                 'handlers': ['mail_admins'],
                 'level': 'ERROR',
                 'propagate': False,
             },
    
    
        },
    }
  • 相关阅读:
    还有更简单的不重复随机数生成方法吗?
    SqlServer数据插入性能小记
    html页面滚动时元素错位解决方案
    为Web页中的Table对象创建一个映射表
    js实现的快速排序
    webkit内核的浏览器为什么removeAttribute('style')会失效?
    setAttribute第三个参数
    Windows转到linux中,文件乱码,文件编码转换
    查看端口的占用
    sndfile
  • 原文地址:https://www.cnblogs.com/BlueFire-py/p/10050081.html
Copyright © 2011-2022 走看看