Python Pit Guide 02--logging module log repeat printing problem

Source: Internet
Author: User

Directory

[TOC]

First, the problem is thrown

Python's logging module is a powerful tool for Python to print logs during use. We can use the logger, Handler, formatter of the logging module to encapsulate our logs, specifying the path, format, and location of the log output. When declaring logger, you can pass a string as a label for this logger. Always thought that this logger is a singleton object design pattern design, as long as this signature is the same, then the return of the logger is the same. When printing the log, want to implement the log hierarchy, define classes as follows function to encapsulate the log printing function (because the source code has been modified, in pseudo-code form)

def print_log_info(msg):    # Logger是我定义的一个类,用来封装日志打印的    = Logger(‘INFO‘).getLogger()    logger.info(msg)

DDT is used in test cases to parameterize test cases. The key code is as follows:

    @data(*test_datas[' Test_add '])@unpack    defTest_add ( Self,*Args**Kwargs): Data_path, Err_num, test_id, p1, p2, value=Args result=Test (P1, p2). Add ()Try: Self. Assertequal (result, value,"in{Err_num}Line use Case number is{test_id}In the test add error, please re-enter ".format(Err_num=Err_num, test_id=test_id)) Utils.print_log_info ("in{Err_num}Line use Case number is{test_id}test of the addition test succeeded ".format(Err_num=err_num,test_id=TEST_ID))except Assertionerror  asErr:err_str=Traceback.format_exc ()RaiseErr

After the test case is executed, a test case is found to print more than one, and the number of repetitions is increased from 11 to n(bloggers are lazy, this is the source code problems).

Second, problem solving

At first, I always thought it was a bug of DDT. But then directly in the external definition of a logger, call this logger print log, found that the log does not repeat the printing situation, and when I accidentally, two times declared the same logger case, found that the log was printed two times. Began to think that this is not the problem of the logging module itself, because the first chapter of the code, each print the log, the need to Print_log_info () function, this function every time the call, the declaration of a logger. Every test case is finished, you need to print the log, and then declare the logger, then the number of times to declare logger is 1 cumulative increase to N, isnot the same as our above repeated log printing situation? So the consideration of the problem is caused by the repeated declaration of logger. To verify the conjecture, make the following modifications to the code (the source code is too long and the sticky part of the key code).

Info_logger=Logger (' INFO '). Get_logger () Error_logger=Logger (' ERROR '). Get_logger ()defPrint_log_info (msg):" "Print normal information for test case execution and console logs:p Aram msg: Information that needs to be printed: return:" "    # Get information about the function callerCaller_module, Msg_lineno=Trace_caller (2) edit_msg= ' [%s] : [%s]     %s' %(Caller_module, Msg_lineno, msg)Print(edit_msg) Info_logger.info (edit_msg)defPrint_log_error (msg):" "Print error log and console output when a use case goes wrong:p Aram msg: Information that needs to be printed: return:" "Edit_msg=Msg Error_logger.error (EDIT_MSG)

In the test case, the same way the first chapter calls the log Print code, get the log as follows , the duplicate log bug has been resolved:

[2018-04-30Monday A:xx: $] [INFO] [Mainthread:28980] [src\Test\Case\TESTEXAMPLE.PY]: [ the] In3The test for the line use Case number TEST_ADD01 was successful in the addition test [2018-04-30Monday A:xx: $] [ERROR] [Mainthread:28980] File: Data\Error in test case error in Calculator.xls routine:4, use Case number: Test_add02traceback (most recent call last): File"C:\Users\Think\PycharmProjects\InterfaceFrame\src\ tEst\case\ testexample.py ", line the,inchTest_add test_id=test_id File"C:\Python33\lib\unittest\case.py", line641,inchAssertequal Assertion_func (first, second, MSG=msg) File"C:\Python33\lib\unittest\case.py", line634,inch_baseassertequalRaise  Self. Failureexception (msg)Assertionerror:3 != 4: In4Add error in test with line use Case number TEST_ADD02, please reenter [2018-04-30Monday A:xx: $] [INFO] [Mainthread:28980] [src\Test\Case\TESTEXAMPLE.PY]: [ the] In5The test for the line use Case number TEST_ADD03 was successful in the addition test [2018-04-30Monday A:xx: $] [INFO] [Mainthread:28980] [src\Test\Case\TESTEXAMPLE.PY]: [ the] In6The test for the line use Case number TEST_ADD04 was successful in the addition test [2018-04-30Monday A:xx: $] [INFO] [Mainthread:28980] [src\Test\Case\TESTEXAMPLE.PY]: [ the] In theThe Division test succeeded in a test where the line use case number is TEST_DIV01 [2018-04-30Monday A:xx: $] [INFO] [Mainthread:28980] [src\Test\Case\TESTEXAMPLE.PY]: [ the] In -The Division test succeeded in a test where the line use case number is test_div02 [2018-04-30Monday A:xx: $] [INFO] [Mainthread:28980] [src\Test\Case\TESTEXAMPLE.PY]: [ the] In -The Division test succeeded in a test where the line use case number is test_div03 [2018-04-30Monday A:xx: $] [INFO] [Mainthread:28980] [src\Test\Case\TESTEXAMPLE.PY]: [ the] In -The Division test succeeded in a test where the line use case number is test_div04 [2018-04-30Monday A:xx: $] [INFO] [Mainthread:28980] [src\Test\Case\TESTEXAMPLE.PY]: [135] In OneThe multiplication test succeeded in a test where the line use case number is test_multi01 [2018-04-30Monday A:xx: $] [INFO] [Mainthread:28980] [src\Test\Case\TESTEXAMPLE.PY]: [135] In AThe multiplication test succeeded in a test where the line use case number is test_multi02 [2018-04-30Monday A:xx: $] [INFO] [Mainthread:28980] [src\Test\Case\TESTEXAMPLE.PY]: [135] In -Multiplication test succeeded in test with line use Case number test_multi03

Python Pit Guide 02--logging module log repeat printing problem

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.