Python Learning Notes (log system implementation)

Source: Internet
Author: User

Bloggers have added a log system to their interface Automation framework today

Based on the Python logging library, including the log main function, generate log file:

1 #-*-coding:utf-8-*-2 #Log System3 #Time: 2017-08-314 #Name: xx5 6 ImportLogging7 ImportOS8  fromDatetimeImportdatetime9 Ten  One classMainlog: A     def __init__(self): -         Pass -  the @staticmethod -     defGetLog (): - Logging.basicconfig ( -             #Filename=mainlog.logtxt (), # Log output to file +Level=logging. DEBUG, -format="% (asctime) s-% (levelname) s-% (message) s", +datefmt="%y-%m-%d%h:%m:%s", A         ) at         returnLogging -  - @staticmethod -     deflogtxt (): -Today_name = Datetime.today (). Strftime ("%y%m%d") -Log_name = Datetime.today (). Strftime ("%y%m%d%h%m%s") +". txt" in         #folder path for New Day -Path ="f:\\logs\\"+ Today_name +"\\" to         ifos.path.exists (path): +             Pass -         Else: the os.makedirs (path) *         #log file path name $File_path = path +Log_namePanax Notoginseng         returnFile_path
1 #-*-coding:utf_8-*-2 #Basic Interface Request Method3 #Time: 2017-08-314 ImportRequests5  fromRequests.exceptionsImportreadtimeout, Connectionerror, Requestexception6  fromLog.main_logImportMainlog7 8 9 classMainresponse:Ten     def __init__(self): One         Pass A  - @staticmethod -     defMainpost (Url=none, Params=none, json=None): theU""" - URL: The URL of the interface request - params: request header for Interface request - JSON: JSON for interface requests +         """ -Logger =Mainlog.getlog () +         Try: AResponse = Requests.post (Url=url, Params=params, Json=json, timeout=10) atCode =Response.status_code -Text =Response.text -Logger.debug (U"Interface Request Address:") - logger.debug (URL) -Logger.debug (U"interface Request Header:") - Logger.debug (params) inLogger.debug (U"Interface Request Parameters:") - Logger.debug (JSON) toLogger.debug (U"Interface Request return code:") + logger.debug (code) -Logger.debug (U"interface return information:") the logger.debug (text) *             returntext $         exceptreadtimeout:Panax NotoginsengLogger.error (U"Request timed out") -         exceptConnectionerror: theLogger.error (U"Request Connection Error") +         exceptrequestexception: ALogger.error (U"return error")

Then add the log format to the POST request in your package

The debug level is normal content.

Error level is the wrong scenario

Finally, execute the automation use case in the UnitTest framework and generate the test report:

Python Learning Notes (log system implementation)

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.