Django Django log of the logging logging logs

Django's logging logs

 

 

Brief introduction

Django uses python print comes as a log logging tool.

logging is thread-safe, which is mainly composed of four parts:

  1. Logger 
    direct interface used by the user, is transmitted to the Handler log
  2. Handler 
    control logging output to go, console, file ... 
    a logger can have multiple Handler
  3. Filter 
    which can control the flow of logs from the logger Handler
  4. Formatter 
    control the format of the log

use

Lane configuration items in sesetti.py

LOGGING Django by using customized log output (including definitions logger, handler, formatter, etc.) in the settings file

For example, settings file is defined as follows:

= The os.path.join BASE_LOG_DIR (base_dir, "log") 
the LOGGING = { 
    'Version':. 1, # reserved word 
    'disable_existing_loggers': False, logger Example # disable existing 
    # log file format 
    'formatters': { 
        # detailed log format 
        'Standard': { 
            'the format': '[% (the asctime) S] [% (threadName) S:% (Thread) D] [task_id:% (name) S] [% (filename) S: % (lineno) D] ' 
                      ' [% (levelname) S] [% (Message) S] ' 
        }, 
        # simple log format 
        ' simple ': { 
            ' the format ':' [% (levelname) S] [% ( the asctime) S] [% (filename) S:% (lineno) D]% (Message) S ' 
        }, 
        # define a special log format 
        ' collect ':{
            'format': '%(message)s'
        }
    }, 
    # Filter 
    'Filters': { 
        'require_debug_true': { 
            '()': 'django.utils.log.RequireDebugTrue', 
        }, 
    }, 
    # processor 
    'handlers': { 
        # print terminal 
        'console': { 
            'Level': 'DEBUG', 
            'Filters': [ 'require_debug_true'], # is only Django debug screen print log in if True 
            'class': 'logging.StreamHandler', # 
            'Formatter': 'the Simple' 
        }, 
        # default 
        'default': { 
            'Level': 'the INFO', 
            'class': 'logging.handlers.RotatingFileHandler', # saved to a file, automatic cut
            'filename': os.path.join (BASE_LOG_DIR, "xxx_info.log"), # log file join (BASE_LOG_DIR, "xxx_info.log") , # log file 
            'maxBytes': 1024 * 1024 * 50, # 50M log size
            'backupCount': 3, # backed up several 
            'Formatter': 'Standard', 
            'encoding': 'UTF-. 8' , 
        }, 
        # specifically referred to the error log 
        'error': { 
            'Level': 'eRROR', 
            'class': 'logging.handlers.RotatingFileHandler', # saved to a file, automatic cut 
            'filename': os.path. join (BASE_LOG_DIR, "xxx_err.log") , # log file 
            'maxBytes': 1024 * 1024 * 50, # log size 50M 
            ' BACKUPCOUNT ':. 5, 
            ' Formatter ':' Standard ', 
            'encoding ':' UTF-. 8 ', 
        }, 
        # specifically define a collection of specific information log 
        ' collect ': {
            'Level': 'the INFO',  
            'class': 'logging.handlers.RotatingFileHandler', # saved to a file, automatic cut
            'filename': the os.path.join (BASE_LOG_DIR, "xxx_collect.log"), 
            'MaxBytes': 1024 * 1024 * 50, # log size 50M 
            'BACKUPCOUNT':. 5, 
            'Formatter' : 'the collect', 
            'encoding': "UTF-. 8" 
        } 
    }, 
    'Loggers': { 
       # default logger application follows 
        '': { 
            'handlers': [ 'default', 'Console', 'error'] , may then be put on the line # 'Console' remove 
            'level': 'the DEBUG', 
            'Propagate': True, # logger not transmitted to the higher level 
        }, 
        # named 'collect 'is further processed separately logger 
        ' collect ': { 
            ' handlers': [ 'Console', 'collect'],collect': {
            'level': 'INFO',
        }
    },
}

views.py in use

logging Import 
# generation logger instance called the name of the current file 
logger = logging.getLogger (__ name__) 
# generate a logger named collect examples 
collect_logger = logging.getLogger ( "collect") 

DEF index (Request): 
    logger.debug ( "Meng Meng a request coming ....") 
    logger.info ( "Moe a more requests coming ....") 
    logger.debug ( "app01 inside which is a function of the index view") 

    collect_logger. info ( "user 1: Beijing") 

    return HttpResponse ( "the OK")

Process

 

 
 
 
 
 
 

 

Brief introduction

Django uses python print comes as a log logging tool.

logging is thread-safe, which is mainly composed of four parts:

  1. Logger 
    direct interface used by the user, is transmitted to the Handler log
  2. Handler 
    control logging output to go, console, file ... 
    a logger can have multiple Handler
  3. Filter 
    which can control the flow of logs from the logger Handler
  4. Formatter 
    control the format of the log

use

Lane configuration items in sesetti.py

Django通过在settings文件中使用LOGGING来定制日志输出(包括定义logger, handler, formatter等)

例如,settings文件中定义如下:

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, "xxx_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, "xxx_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, "xxx_collect.log"),
            'maxBytes': 1024 * 1024 * 50,  # 日志大小 50M
            'backupCount': 5,
            'formatter': 'collect',
            'encoding': "utf-8"
        }
    },
    'loggers': {
       # 默认的logger应用如下配置
        '': {
            'handlers': ['default', 'console', 'error'],  # 上线之后可以把'console'移除
            'level': 'DEBUG',
            'propagate': True,  # 向不向更高级别的logger传递
        },
        # 名为 'collect'的logger还单独处理
        'collect': {
            'handlers': ['console', 'collect'],
            'level': 'INFO',
        }
    },
}

views.py里使用

import logging
# 生成一个以当前文件名为名字的logger实例
logger = logging.getLogger(__name__)
# 生成一个名为collect的logger实例
collect_logger = logging.getLogger("collect")

def index(request):
    logger.debug("一个萌萌的请求过来了。。。。")
    logger.info("一个更萌的请求过来了。。。。")
    logger.debug("这是app01里面的index视图函数")

    collect_logger.info("用户1:北京")

    return HttpResponse("OK")

流程

 

 
 
 

简介

Django使用python自带的logging 作为日志打印工具。

logging 是线程安全的,其主要由4部分组成:

  1. Logger 
    用户使用的直接接口,将日志传递给Handler
  2. Handler 
    控制日志输出到哪里,console,file… 
    一个logger可以有多个Handler
  3. Filter 
    控制哪些日志可以从logger流向Handler
  4. Formatter 
    控制日志的格式

使用

项目里sesetti.py里配置

Django通过在settings文件中使用LOGGING来定制日志输出(包括定义logger, handler, formatter等)

例如,settings文件中定义如下:

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, "xxx_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, "xxx_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, "xxx_collect.log"),
            'maxBytes': 1024 * 1024 * 50,  # 日志大小 50M
            'backupCount': 5,
            'formatter': 'collect',
            'encoding': "utf-8"
        }
    },
    'loggers': {
       # 默认的logger应用如下配置
        '': {
            'handlers': ['default', 'console', 'error'],  # 上线之后可以把'console'移除
            'level': 'DEBUG',
            'propagate': True,  # 向不向更高级别的logger传递
        },
        # 名为 'collect'的logger还单独处理
        'collect': {
            'handlers': ['console', 'collect'],
            'level': 'INFO',
        }
    },
}

views.py里使用

import logging
# 生成一个以当前文件名为名字的logger实例
logger = logging.getLogger(__name__)
# 生成一个名为collect的logger实例
collect_logger = logging.getLogger("collect")

def index(request):
    logger.debug("一个萌萌的请求过来了。。。。")
    logger.info("一个更萌的请求过来了。。。。")
    logger.debug("这是app01里面的index视图函数")

    collect_logger.info("用户1:北京")

    return HttpResponse("OK")

流程

 

Guess you like

Origin www.cnblogs.com/taosiyu/p/11220327.html