码迷,mamicode.com
首页 > 其他好文 > 详细

django 日志

时间:2020-06-04 19:57:56      阅读:66      评论:0      收藏:0      [点我收藏+]

标签:bsp   error   form   标准   cname   default   sim   ogg   ror   

# settings.py

配置一

LOG_DIR = BASE_DIR + "/log"

LOGGING = {

    version: 1,

    disable_existing_loggers: True,

    formatters: {

        standard: {

            format: %(asctime)s FuncName:%(funcName)s LINE:%(lineno)d [%(levelname)s]- %(message)s

        },

        simple: {

            format: %(levelname)s %(message)s

        },

        verbose: {

            format: %(levelname)s %(asctime)s %(module)s %(funcName)s %(message)s

        },

    },

    filters: {

        require_debug_false: {

            (): django.utils.log.RequireDebugFalse

        }

    },

    handlers: {

        console: {

            level: DEBUG,

            class: logging.StreamHandler,

            formatter: standard

        },

        default: {

            level: INFO,

            class: logging.handlers.RotatingFileHandler,

            filename: os.path.join(LOG_DIR, info.log),

            maxBytes: 1024*1024*102400,   # 102400 MB

            backupCount: 2,

            formatter: standard,

        },

        default_debug: {

            level: DEBUG,

            class: logging.handlers.RotatingFileHandler,

            filename: os.path.join(LOG_DIR, debug.log),

            maxBytes: 1024*1024*102400,  # 102400 MB

            backupCount: 2,

            formatter: standard,

        },

        request_handler: {

            level: DEBUG,

            class: logging.handlers.RotatingFileHandler,

            filename: os.path.join(LOG_DIR, common.log),

            maxBytes: 1024*1024*102400,  # 102400 MB

            backupCount: 2,

            formatter: standard,

        },

        restful_api: {

            level: DEBUG,

            class: logging.handlers.RotatingFileHandler,

            filename: os.path.join(LOG_DIR, api.log),

            maxBytes: 1024*1024*102400,  # 102400 MB

            backupCount: 2,

            formatter: verbose,

        },

    },

    loggers: {

        django: {

            handlers: [console, default_debug],

            level: INFO,

            propagate: False

        },

        django.request: {

            handlers: [request_handler],

            level: INFO,

            propagate: False

        },

        common: {

            handlers: [default, console],

            level: INFO,

            propagate: True

        },

        api: {

            handlers: [restful_api],

            level: INFO,

            propagate: True

        },

    }

}

 

配置二

# 项目级别日志
BASE_LOG_DIR = os.path.join(BASE_DIR, "log")

LOGGING = {
    version: 1,  # 保留的参数,默认是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: {
        # 只有在deubug=True的过滤器
        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, "blueadmin_info.log"),  # 日志文件
            maxBytes: 1024 * 1024 * 50,  # 日志大小 50M
            backupCount: 5,  # 备份数为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, "blueadmin_info.log"),  # 日志文件
            backupCount: 5,  # 备份数为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, "blueadmin_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, "blueadmin_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‘的logger还单独处理
        collect: {
            handlers: [console, collect],
            level: INFO,
        }
    },
}

 

django 日志

标签:bsp   error   form   标准   cname   default   sim   ogg   ror   

原文地址:https://www.cnblogs.com/chongdongxiaoyu/p/13045363.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!