zoukankan      html  css  js  c++  java
  • Django 日志配置

    Django logging配置

     

    做开发离不开日志,以下是我在工作中写Django项目常用的logging配置。

    复制代码
    BASE_LOG_DIR = os.path.join(BASE_DIR, "log")
    LOGGING = {
        'version': 1,
        'disable_existing_loggers': False,
        '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'
            },
            'SF': {
                'level': 'INFO',
                'class': 'logging.handlers.RotatingFileHandler',  # 保存到文件,根据文件大小自动切
                'filename': os.path.join(BASE_LOG_DIR, "xxx_info.log"),  # 日志文件
                'maxBytes': 1024 * 1024 * 50,  # 日志大小 50M
                'backupCount': 3,  # 备份数为3  xx.log --> xx.log.1 --> xx.log.2 --> xx.log.3
                'formatter': 'standard',
                'encoding': 'utf-8',
            },
            'TF': {
                'level': 'INFO',
                'class': 'logging.handlers.TimedRotatingFileHandler',  # 保存到文件,根据时间自动切
                'filename': os.path.join(BASE_LOG_DIR, "xxx_info.log"),  # 日志文件
                'backupCount': 3,  # 备份数为3  xx.log --> xx.log.2018-08-23_00-00-00 --> xx.log.2018-08-24_00-00-00 --> ...
                'when': 'D',  # 每天一切, 可选值有S/秒 M/分 H/小时 D/天 W0-W6/周(0=周一) midnight/如果没指定时间就默认在午夜
                'formatter': 'standard',
                'encoding': 'utf-8',
            },
            'error': {
                'level': 'ERROR',
                'class': 'logging.handlers.RotatingFileHandler',  # 保存到文件,自动切
                'filename': os.path.join(BASE_LOG_DIR, "xxx_err.log"),  # 日志文件
                'maxBytes': 1024 * 1024 * 5,  # 日志大小 50M
                'backupCount': 5,
                'formatter': 'standard',
                'encoding': 'utf-8',
            },
            'collect': {
                'level': 'INFO',
                'class': 'logging.handlers.RotatingFileHandler',  # 保存到文件,自动切
                'filename': os.path.join(BASE_LOG_DIR, "xxx_collect.log"),
                'maxBytes': 1024 * 1024 * 50,  # 日志大小 50M
                'backupCount': 5,
                'formatter': 'collect',
                'encoding': "utf-8"
            }
        },
        'loggers': {
            '': {  # 默认的logger应用如下配置
                'handlers': ['SF', 'console', 'error'],  # 上线之后可以把'console'移除
                'level': 'DEBUG',
                'propagate': True,
            },
            'collect': {  # 名为 'collect'的logger还单独处理
                'handlers': ['console', 'collect'],
                'level': 'INFO',
            }
        },
    }
    复制代码

    附:Python logger流示图

     

  • 相关阅读:
    在TextBrowser显示中,如何让最新的数据永远出现在第一行或者是在窗口的最后显示信息
    在Qtlabel中显示数字十六进制和十进制都可以
    实现 在子界面的button按下,在主界面的label显示。
    今天在Qt子界面中的Button,转到槽转不过去,报错Qt The class containing 'Ui::MainWindow' could not be found in...
    Qt串口接收使用多个LCD控件显示不同的数据
    Qt图标自定义
    Qt绘制动态曲线
    3.3.2Qt的按钮部件
    Mesh Profile (5)启动配置(配网)
    SiliconLabs EFR32BG 定时器输入捕获和脉宽调制
  • 原文地址:https://www.cnblogs.com/xyhh/p/10839276.html
Copyright © 2011-2022 走看看