Preface:

Logging is a very important module in the daily work of software testing. There are four main functions of logging for testing:

  1. The debugger
  2. Understand whether the system program is running normally
  3. System west operation fault analysis and problem location
  4. Used for user behavior analysis and data statistics

Therefore, when writing automation test scripts and building automation test framework, it is best to add the collection function of logs, which can be used to locate problems.

The body of the

Relevant concepts

Before we can customize the log, we need to know the following:

  1. A log collector can be thought of as a container for collecting log information;
  2. Log Level: Common log levels are: DEBUG, INFO, WARNING, ERROR, and CRITICAL
  3. Output channel (Handle) : Console output: streamHandle saves the log information in a file: FileHandle
  4. Log Format: generally contains the following information: log time – log name – log level name – file name – line number – log information, etc

    The sample

FMT = "%(asctime)s %(name)s %(levelName)s %(filename)s-%(lineno)d:%(message)s logging.Formatter(fmt)

In the Python-Logging module, the default is the root logging collector and the default output level is: WARNING

Customize the log operation flow

  1. Logging module: import Logging
  2. Create a Log Collector: logger.getLogger (” Name of Log Collector “)
  3. Setlevel (logger.info) # Sets the level of the collector to INFO
  4. Create an output channel for the log collector (from Part 1: The log output channel consists of console output and file output). The following is an example of console output, which is similar to file output. 4.1 Create a log output channel: Handle1 = logging.streamHandle () 4.2 You can set the level of the log output channel separately: 4.3 If the log Level of the log output channel is not set, the default value is Level 4.4 set by the log collector. If the log Level of the log output channel is set separately, the default value is Level 4.4 set by the log collector. The log level must be higher than the log collector level, otherwise the setting is invalid.
  5. Set the content format of the log output
FMT = "%(asctime)s %(name)s %(levelName)s %(filename)s-%(lineno)d:%(message)s logging.Formatter(fmt)
  1. Bind the set log format to the created output channel, associating the log format with the output channel

handler1.setFormatter(formatter)

  1. Add the set output channel to the log collector

    logger.addHandler(handler1)

Note: The procedure to output log information to a file is similar, with a slight difference in Step 4

handler2 = logging.FileHandler(filename="xxx.log",encoding="utf-8")

Log Code Reference

Import logging.getLogger(name="login_test") import logging.getLogger = logging.getLogger(name="login_test") Logging.setlevel (logging.warn) # 3 Set the output of the log to the handler1 = logging.streamHandler () # 3.2 File log output handler2 = logging.fileHandler (filename="my_log.log",encoding="utf-8") # set log level (logging.error) # optional FMT = "%(ASCTIME) S %(NAME) S %(LevelName) S %(FILENAME) S -%(LINENO) D :%(MESSAGE) S "Formatter = Logging.Formatter(FMT) # 5, 3 and 4 handler1. Logging.Formatter(FMT) # 5, 4 and 5 Logger.addHandler (handler1) logger.addHandler(handler2) # test logger.warning(" Logger failed warning ") logger.error(" Logger debug error ")

Encapsulation of custom logs

Since the flow of custom logging is relatively fixed, we can encapsulate the custom logging as a class. When we need to use it, we simply import the module.

  1. Logging.Logger is a class that inherits functions such as debug() and info() from the parent class.
  2. As we know from the operation flow in the second part, different users may set different log names, log levels and log file information when introducing the module. Therefore, these parameters can allow users to initialize the log object when instantiating it.
  3. When we need to use a custom logging class, we can introduce this module.

Reference code

Class MyLogger(logging.logger): def __init__(self,name,level,file=None); Super () __init__ (name, level) # set the log output channel handler1 = logging. StreamHandler () # set the log output format FMT = "% s % (name) s (asctime) %(levelname)s %(filename)s-%(lineno)d:%(message)s" formatter = logging.Formatter(fmt) handler1.setFormatter(formatter) # Self. AddHandler (handler1) if file: handler2 = logging.FileHandler(filename=file,encoding="utf-8") handler2.setFormatter(formatter) self.addHandler(handler2) pass pass pass

The latter

The above is a simple custom log and its packaging, if you have some help hope to be able to click a small like, in the future will continue to update more automated test learning techniques and methods

The above is my collection of some videos and test questions, for software testing friends should be the most comprehensive and complete interview preparation warehouse

In order to better organize each module, I also referred to a lot of quality blog posts and projects on the Internet, and tried not to miss every knowledge point. Many friends reviewed with these contents, and got the offer from BATJ and other big factories. This warehouse has also helped a lot of software testing learners, I hope it can help you

Pay attention to WeChat public number [testing rookie Xiao Jia Luo] can receive software testing super core resources! You can also click here to enter the group chat

In a word, learning is like rowing upstream; not to advance is to drop back. You need to work hard to get what you want.