程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Detailed explanation of Python log processing

編輯:Python

In automated testing , According to the requirements of the project , Custom log fields , You can also use python in logging Module definition log .

The main function of using logs can also be divided into three parts : Program debugging 、 Understand the operation of software programs 、 Software program running fault analysis and problem location , The same is true with the introduction of logging in our automated testing .
One 、 Custom log module

You can customize the fields in the log according to specific conditions , Such as time 、 file 、 Test case name , Test case name , Expected results , Information such as actual results . Call this method to write the corresponding data where the log needs to be written in the test case .

1. establish python Custom log module in the environment

Custom log module , Although the function is not as good as the professional module , But you can easily customize the output , We can also try to use... In our automated testing work .



2. Where you need to output logs , Import the created log module , And call methods
3. View the contents of the log file

Two 、logging Log module

logging The module is Python Built in standard modules , It is mainly used to output the operation log , You can set the level of output log 、 Log save path 、 Log file rollback, etc , comparison print() Print directly on the console , It has the following advantages :

You can set different log levels

You can only output important information , Without having to display a lot of debugging information

logging It's up to the developer to decide where to output the information , And how to output .

1. encapsulation logging Log module

Packaging Foundation logging Module function , Call it in the automation script , Can be defined as a class or method , Easy to call and use



2. Where the test case needs to output logs , Call this method to log .

3、 Solve the problem of Chinese garbled code in the log

stay Python Installation directory /lib/logging/ Next , modify __init__.py The following code of the module , take encoding The default value of is changed to utf-8 that will do .

4. To configure logging In several ways

1) Use Python Explicit code creation loggers, handlers and formatters And call their configuration functions respectively ;

2) Create a log profile , And then use fileConfig() Function to read the contents of the file ;

3) Create a dict, And then pass it on to dictConfig() function ;

5. Add context information to the log output

In addition to the parameters passed to the logging function , Sometimes we want to include some additional context information in the log output . such as , In a web application , You may want to log client specific information , Such as : Remote client's IP Address and username . Here we will introduce the following implementation methods :

· By passing a to the logging function extra Parameter introduces context information

· Use LoggerAdapters Introduce contextual information

· Use Filters Introduce contextual information

Free programming materials : Learning exchange group :399932895
Fast learning python Basics

Python Network programming

Python The crawler development

Python Omnipotent Vocational Course

django Project case elaboration

Python Database programming

Python Develop payment system

Cloud computing Big data and artificial intelligence


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved