The logging module in Python is encapsulated to record bug logs and log levels

  • Is the log generated by the program
  • Programmer defined
  • Output at the collector and channel level
  • The role of logs and reports:
    • The focus of the report is on the execution results (successful execution failure, how many use cases are covered), and the returned results
    • The focus of the log is on the execution process, exception points, where to fail, where to report errors, and diagnostic codes

Basic use of logging module

import logging  # Python Built in log module

# 1. Define log collector,Use.getLogger Method, name of collector case
case_logger = logging.getLogger("case")  # Will create a Logger object

# 2. Specifies the log level of the log collector, using the logging In object.setLevel Method
# NOTSET(0), DEBUG(10), INFO(20)--, WARNING(30), ERROR(40), CRITICAL(50)

# Method 1 setLevel(Seth. Leo
# case_logger.setLevel(logging.DEBUG)   # Only logs of current level and above can be collected
# Method two setLevel(Seth. Leo

# 3. Define log output channel
# Output to console, using logging(Lao Gen).StreamHandler(Tap, wood, sweat)
console_handle = logging.StreamHandler()     # Handler object

# Output to file, using logging(Lao Gen).FileHandler(Hair mate. Bean) method,(Specify file name, default a In addition,)
file_handle = logging.FileHandler("cases.log", encoding="utf-8")

# 4. Specify the log level of the log output channel, and use the object.setLevel(Seth. Leo) method

# 5. Define the format of the log display, logging(Lao Gen) Formatter(Farmer met) function
# Format of simple points
simple_formatter = logging.Formatter('%(asctime)s - [%(levelname)s] - [msg]: %(message)s')
# Detailed format
verbose_formatter = logging.Formatter('%(asctime)s - [%(levelname)s] - [msg]: %(message)s - %(name)s - %(lineno)d')

# The console displays simple logs, setFormatter(Seth nemet) function
# Show detailed log in log file

# 6. Docking, Connect the log collector with the output channel, in the collector addHandler(Press, bean) function

Log level

  • NOTSET(0):
  • DEBUG(10): in the debugging stage, the business system is not online yet
  • INFO(20): normal prompt in formation
  • WARNING(30): warning, there is no error, it will not cause system interruption, the program is running normally, and a warning will appear
  • ERROR(40): system error, affecting system function
  • CRITICAL(50):

Format of log display

  • %(ASCII) s: display time
  • %(created)f
  • %(filename)s: the name of the log
  • %(funcName)s: the name of the function
  • %(levelname)s: log level
  • %(levelno)s
  • %(lineno)d: wrong line number
  • %(message)s: specific log details
  • %(module)s: error reporting of that module
  • %(name)s: name of collector

Encapsulation of logging module

# Encapsulates the class that processes the log
import logging
from danyuan.handle_config import do_config     # Get from profile

class HandleLog:
    //Encapsulates the class that processes the log
    def __init__(self):
        # 1. Define log collector
        self.case_logger = logging.getLogger(do_config.get_value("log", "logger_name"))

        # 2. Specify the log level of the log collector
        self.case_logger.setLevel(do_config.get_value("log", "logger_level"))

        # 3. Define log output channel
        console_handle = logging.StreamHandler()  # Define log output to console
        file_handle = logging.FileHandler(do_config.get_value("log", "log_filename"), encoding="utf-8")     # Define log output to file

        # 4. Specify the log level of the log output channel
        console_handle.setLevel(do_config.get_value("log", "console_level"))
        file_handle.setLevel(do_config.get_value("log", "file_level"))

        # 5. Define the format of log display
        simple_formatter = logging.Formatter(do_config.get_value("log", "simple_formatter"))    # Format of simple points
        verbose_formatter = logging.Formatter(do_config.get_value("log", "verbose_formatter"))  # Detailed format

        console_handle.setFormatter(simple_formatter)   # The console displays simple logs
        file_handle.setFormatter(verbose_formatter)     # Show detailed log in log file

        # 6. Docking, Connect the log collector with the output channel

    def get_logger(self):
        //Get logger log object
        return self.case_logger

# do_log = HandleLog().get_logger()       # Instanced object

if __name__ == '__main__':
    do_log = HandleLog()
    case_logger = do_log.get_logger()
    case_logger.debug("This is a debug Level log")  # Manual logging"This is a info Level log")
    case_logger.warning("This is a warning Level log")
    case_logger.error("This is a error Level log")
    case_logger.critical("This is a critical Level log")



*******Please respect the original, if you want to reprint, please indicate the source: reprint from: Thank you!! * * * * * *

Tags: Python encoding ascii

Posted on Mon, 06 Jan 2020 09:47:34 -0800 by attock