There is a delay in updating articles on this site, if you want to see articles about Python + Appium, please follow me over to Testhome. Testerhome.com/topics/2780…
First, the function of the log
During project development and testing, logs can help locate and analyze problems accurately
2. Log level
The priority is DEBUG<INFO<WARNING<ERROR<CRITICAL
The default log level is warning or higher. Debug and INFO are not output to the console
Three, a few important concepts
- Logger: An interface that provides logging and is used by code
- Handle: Where do log records (generated by loggers) go, i.e., where do they go out
- Filter: Provides better granularity control to determine which log records to output, providing an elegant way to determine whether a log record is sent to Handle
- Formatter: Specifies the specific format for logging output
4. Logger
1, an overview of the
Logger is a tree hierarchy that must create Logger instances before using the debug, INFO, warning, error, and critical interfaces. If this is not explicitly created, a root Logger is created by default. The default log level (WARN) is applied, the handler Handle (StreamHandle, which prints log information to standard output), and the Formatter (the default format is the one printed in the first simple utility).
Create method: Logger = logging.getLogger(logger_name)
Logger objects are never instantiated directly, but through module-level functionality
Logging.getlogger (name) creates a Logger instance. When logging.getLogger(name) is called, it always returns the application of a Logger object instance if the same value of name is passed in.
2. After creating Logger instance, you can set the log level and add handler
- Logger.setlevel (logging.error) = Set the log level to ERROR, that is, only logs with a log level greater than or equal to ERROR will be output (the default level is warning).
- Logger. addHandle(Handle_name) = Adds a handler to the Logger instance
- Logger. removeHandle(handle_name) = Remove a handler for the Logger instance
5. Handle
1, an overview of the
Handle sends log information to the specified location (file, window)
The types of handles are StreamHandle, FileHandle, and NullHandle
2, StreamHandle
Sh = logging.streamHandle (stream==None)
Send log output to the console
3, FileHandle
Sends logging to a disk file, which inherits the output functionality of StreamHandle
FileHandle(filename,mode=’a’,encoding=None,delay=False)
4, NullHandle
Without doing any formatting or output, it is essentially a “no-action” handler for developers
Is it essentially a “do nothing” Handle used by library developers
A Formatter
1, an overview of the
Set the log output format
Formatter = logging. formatter (FMT =None,datefmt=None), FMT and datefmt are used to set log format and time format
Default format: %(asctime)s-%(LevelName)s-%(message)s
The default time format is %Y-% M -% D %H:%M:%S
2. Use the Formatter object to set the final rule, structure, and content of the log message
Application of logging in real test cases
The wrapper class has the convenience of external file calls to the log, the example mainly completes the log output in the console and the log output to the external.log file
Steps to encapsulate the logging class:
To create a logger
Create a handle…
Create the formatter
To configure the logger
The chestnut:
1. Create the external log file test.log for the class in the project directory
2. Wrap the Logger class:
# Log synthesis case encapsulation
import logging
class Logger() :
def __init__(self, LoggerName, FileName, CmdLevel, FileLevel) :
FileName: external FileName CmdLevel: sets the log output level of the console. FileLevel: sets the log output level of the file
self.logger = logging.getLogger(LoggerName)
Set the log level
self.logger.setLevel(logging.DEBUG)
Set the output format of the log
fmt = logging.Formatter('%(asctime)s-%(name)s-%(levelname)s-%(message)s')
Export logs to the test.log file with handle
fh = logging.FileHandler(FileName)
fh.setLevel(FileLevel)
Output logs to the console with handle
ch = logging.StreamHandler()
ch.setLevel(CmdLevel)
# configuration logger
fh.setFormatter(fmt)
ch.setFormatter(fmt)
Add handle to Logger
self.logger.addHandler(fh)
self.logger.addHandler(ch)
def debug(self,message) :
self.logger.debug(message)
def info(self,message) :
self.logger.info(message)
def warn(self,message) :
self.logger.warning(message)
def error(self,message) :
self.logger.error(message)
def critical(self,message) :
self.logger.critical(message)
# The following is the test code, the actual operation can be commented out
if __name__ == "__main__":
logger = Logger("appium"."test.log",CmdLevel=logging.DEBUG,FileLevel=logging.INFO)
logger.debug("debug message!")
logger.info("info message!")
logger.warn("warning message!")
logger.error("error message!")
logger.critical("critical message!")
Copy the code
3. Call Loger class:
Create an instantiation object of the Logger class outside of the class, passing in the required parameters
# Call the Loger class from another class
Instantiate the Logger class and pass in the parameters
logger = Logger("appium"."test.log", CmdLevel=logging.DEBUG, FileLevel=logging.INFO)
class Test:
logger.debug("debug message!")
logger.info("info message!")
logger.warn("warning message!")
logger.error("error message!")
logger.critical("critical message!")
Copy the code
4. Output results:
Console:2020- 09 -10 10:32:46.230-appium-DEBUG-debug message!
2020- 09 -10 10:32:46.230-appium-INFO-info message!
2020- 09 -10 10:32:46.230-appium-WARNING-warning message!
2020- 09 -10 10:32:46.230-appium-ERROR-error message!
2020- 09 -10 10:32:46.230-appium-CRITICAL-critical message! External files:20- 09 -10 10:32:46.230-appium-INFO-info message!
2020- 09 -10 10:32:46.230-appium-WARNING-warning message!
2020- 09 -10 10:32:46.230-appium-ERROR-error message!
2020- 09 -10 10:32:46.230-appium-CRITICAL-critical message
Copy the code