Appium collection of logs for the Python Automated Test series (12)

Source: Internet
Author: User
Tags appium

? 13.1 definition of the 13.1.1 log

Hear the log this thing may have some people inexplicably, the first contact will feel why we want to collect logs, even if we want to collect logs so we need to collect what logs, what is the role of the log and so on.

In fact, whether the log is in the test, development is very important, if you run Automation when the program error, but you do not log how you go to locate the program errors or your app error? Or is it another environmental factor?

In Java has log4j this log module, in Python We use logging this module, this module does not need you to how to download the installation and so on, Python comes with, you just need to use it just fine.

13.2 Log Collection actual combat 13.2.1 log three elements

If we knew the importance of the log during the test, would that be OK? The answer is definitely no, we need to understand a few things in the process of collecting logs:

First, the level of the log

In the company employees have the level of points, this log is the same, he also has a level of points. Why do you say that? It's not always possible to collect all the logs from the test, and I'll take care of 3, 7, and 21. This is definitely not going to work.

First of all, our log needs to be based on info, debug, error and other levels to differentiate. Of course, this level can be set on its own. In general, our normal output of our direct use of the type of info, debugging with the debug type, if the error is expected then we need to use the error type of the log.

Second, the format of the log

Why is there a log format? Because we debug the program or look at the log when we to better read, or to increase the readability of the log, and we locate the problem is convenient, fast we need to do some optimization of the format of the log, like I in this log output time, And when I see this log, I know which class is wrong?

Third, the log location

People read the log when they write the program, most people still see it through the console, if you accidentally shut down the console? And how do you look at the console after you've integrated the framework? These are not realistic, so in the log we need to have a location to save our logs, so we will have a default log directory when we build the framework.

13.2.2 Log Collection Combat

One, log file configuration

First we collect logs we want to logging module configuration, in fact, this and log4j is the same, you can check the website or other places the basic file of this configuration, I also show you A: log.conf

[Loggers]keys=root,infologger,errorlogger [  Logger_root]level=debughandlers=infohandler,errorhandler  [ Logger_infologger]handlers=infohandlerqualname=infologgerpropagate=0  [logger_errorlogger]handlers= errorhandlerqualname=errorloggerpropagate=0  ###############################################  [handlers] Keys=infohandler,errorhandler  [Handler_infohandler]class=streamhandlerlevel=infoformatter=form02args= ( Sys.stdout,)  [handler_errorhandler]class=filehandlerlevel=errorformatter=form01args= ('. /logs/mylog.log ', ' a ')  ###############################################  [formatters]keys=form01,form02  [formatter_form01]format=% (asctime) s% (filename) s% (levelname) s  % (message) sdatefmt=%y-%m-%d%h:%m:%s  [formatter_form02]format=% (asctime) s% (filename) s% (levelname) s  % (message) sdatefmt=%y-%m-%d%h:%m:%s

This configuration file Everyone configuration is not the same, but the general function is similar, this only need to according to their own preferences to define the line, the following I put the configuration definition of the method to paste it out for everyone to see:

1. Initialize logger = Logging.getlogger ("Endlesscode"), the GetLogger () method is best followed by the name of the module to be logged, and the subsequent log format of% (name) s corresponds to the module name 2. Set the level Logger.setlevel (logging. Debug), Logging NotSet < DEBUG < INFO < WARNING < ERROR < critical these levels, logs record 3 above the set level. Handler, commonly used Streamhandler and filehandler,windows you can simply understand as a console and a file log, a print on a CMD window, a record on a file 4. Formatter, which defines the order, structure and content of the final log information, I like to use such formats as ' [% (asctime) s] [% (levelname) s]% (message) s ', '%y-%m-%d%h:%m:%s ',% (name) s Logger name% (levelname) s text form of log level% (message) s user output message% (asctime) s string form the current time. The default format is "2003-07-08 16:49:45,896". The comma is followed by a millisecond% (Levelno) s number form of log level% (pathname) s The full pathname of the module that calls the log output function, and may not have% (filename) s to call the log output function of the module's file name% (module) s  Call log output function module name% (funcName) s call log output function name% (Lineno) d Call Log output function The statement of the code line in% (created) F current time, a floating-point number representing time with UNIX standard% ( relativecreated) d The number of milliseconds (thread) d thread ID since logger was created when the log information is output. There may not be a% (threadname) s thread name. There may not be a% (process) d process ID. Probably not.

  

In fact, everyone just need to follow my configuration above, here need to mention a bit is, in the configuration file you need to configure the log output file, if you do not have this file, the system will error, "args= ('. /logs/mylog.log ', ' a ') "This sentence everyone changes according to their own situation."

After we have configured all the configuration files, we just need to introduce them where we need them, and look at the following code:

#coding =utf-8import Loggingimport logging.config# This is the path to the configuration file Conf_log = ". /config/log.conf "Logging.config.fileConfig (conf_log) logger = Logging.getlogger () #下面就是使用日志打印日志信息logger. info (" Info type of log ") Logger.error (" error log ")

Through the above configuration and output the final results such as:

With this image we can see the log time we have output, the name of the class, and the rank. Isn't it convenient? Do it.

Note: I here the wrong log in the logs file under the MyLog.log file is also saved, because we look at the error log when most only look at the wrong log, so only the wrong log is saved, if other types of need to save, then you will follow similar format in the configuration of info and also configure the directory in.

 

Appium collection of logs for the Python Automated Test series (12)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.