python + Appium automation: log logging module

Log Level

debug, info, warn, error, critical five levels

logging modules (four parts)

Logger (recording means for log collection)

Handler (processor, sends the log to the appropriate path)

Filter (filter, provides better control of particle size, which determines the output log)

Formatter (from format, a specified format log)

 

Logger (recorder)

Create a logging instance before using debug, info, warn, error, critical five levels

Methods: basicConfig () to do basic configuration for the logging system

# -*- coding: utf-8 -*-#

import logging
# 根据日志级别输出
# logging.basicConfig(level=logging.INFO)
logging.basicConfig(level=logging.CRITICAL)

logging.debug("debug info")
logging.info("hello world")
logging.warning("warnning info")
logging.error("error info")
logging.critical("critical info")

Handler (processor)

1.StreamHandler

Transmitting to sys.stdout logging output, the value of the object sys.stderr any similar document stream, the above example is output to the console

2.FileHandler

The logging output is sent to a disk file, it inherits the output of StreamHandler

logging.basicConfig(filename="runlog.log",lever=logging.DEBUG)

After running the current script path generates a runlog.log file for logging

3.NullHandler

Without any formatting or output, it is essentially a developer using the "no operation" handler.

Filter (filter)

Filters can be done using more sophisticated level than the filter

 

Formatter

Formatter object is set using information of the last log rule, structure and content, a default time is% Y-% m-% d% H% M% S

% (Levelno) s print log level value

% (Levelname) s log level printed name

% (Pathname) s Print program execution path

% (Filename) s print the currently executing program name

% (FuncName) s print log current function

% (Lineno) d print log the current line number

% (Asctime) s time printing log

% (Thread) d print thread id

% (ThreadName) s print thread name

% (Process) s printing process ID

% (Message) s print log information

 

method:

logging.basicConfig(level=logging.DEBUG,filename="runlog.log",format="%(asctime)s%(filename)s[line:%(lineno)d] %(levelname)s %(message)s")

 

Open Taobao instance:

# -*- coding: utf-8 -*-#

from appium import webdriver
import yaml
import logging
from selenium.common.exceptions import NoSuchElementException

file = open('../xxx.yaml',encoding="utf-8")
data = yaml.load(file,yaml.FullLoader)

logging.basicConfig(level=logging.DEBUG,filename="runlog.log",format="%(asctime)s%(filename)s [line:%(lineno)d] %(levelname)s %(message)s")

desired_caps={
               "platformName": data["platformName"],
               "platformVersion": data["platformVersion"],
               "deviceName": data["deviceName"],
               "appPackage": data["appPackage"],
               "appActivity": data["appActivity"],
               "unicodeKeyboard":data["unicodeKeyboard"],
               "resetKeyboard":data["resetKeyboard"],
               "noReset": data["noReset"]
                }logging.info(
"logging app...")
driver = webdriver.Remote('http://'+str(data['ip'])+':'+str(data['port'])+'/wd/hub',desired_caps) 
def check_agreebtn ():

# protocol agreed detection pop
    logging.info("check_agreebtn")
    try:
        agreebtn=driver.find_element_by_id('com.taobao.taobao:id/provision_positive_button')
    except NoSuchElementException:
        logging.info("no agreebtn")
    else:
        agreebtn.click()

def check_localtionbtn():
    logging.info("check_localtionbtn")
    try:
        localtionbtn=driver.find_element_by_id('com.taobao.taobao:id/uik_mdButtonDefaultPositive')
    except NoSuchElementException:
        logging.info("no localtionbtn")
    else:
        localtionbtn.click()

check_agreebtn()
check_localtionbtn()

Guess you like

Origin www.cnblogs.com/bugbreak/p/12085045.html