模块日志记录的属性funcName返回默认的Levelname

时间:2018-12-18 02:36:02

标签: python-3.x

使用merge_sort测试logging.debug

In [64]: %paste                                                                                                                                       
import logging
logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(funcName)s - %(message)s")
def merge_sort(A):
    if len(A) <= 1:
        return A
    mid = len(A) // 2
    left = merge_sort(A[:mid])
    logging.debug(f"left is {str(left)}")
    right = merge_sort(A[mid:])
    logging.debug(f"right is {str(right)}")
    result = []
    j, k = 0, 0
    while j < len(left) and k < len(right):
        if left[j] <= right[k]:
            result.append(left[j])
            j += 1
        else:
            result.append(right[k])
            k += 1
    while j < len(left):
        result.append(left[j])
        j += 1
    while k < len(right):
        result.append(right[k])
        k += 1
    logging.debug(f"result is {str(result)}.")

    return result
## -- End pasted text --

运行

In [70]: random.shuffle(A)                                                                                                                            

In [71]: A                                                                                                                                            
Out[71]: [23, 9, 2, 3, 5, 9]

In [72]: merge_sort(A)                                                                                                                                
2018-12-18 10:31:54,588 - DEBUG - left is [23]
2018-12-18 10:31:54,588 - DEBUG - left is [9]
2018-12-18 10:31:54,589 - DEBUG - right is [2]
2018-12-18 10:31:54,589 - DEBUG - result is [2, 9].
2018-12-18 10:31:54,589 - DEBUG - right is [2, 9]
2018-12-18 10:31:54,589 - DEBUG - result is [2, 9, 23].
2018-12-18 10:31:54,589 - DEBUG - left is [2, 9, 23]
2018-12-18 10:31:54,589 - DEBUG - left is [3]
2018-12-18 10:31:54,589 - DEBUG - left is [5]
2018-12-18 10:31:54,589 - DEBUG - right is [9]
2018-12-18 10:31:54,589 - DEBUG - result is [5, 9].
2018-12-18 10:31:54,589 - DEBUG - right is [5, 9]
2018-12-18 10:31:54,589 - DEBUG - result is [3, 5, 9].
2018-12-18 10:31:54,589 - DEBUG - right is [3, 5, 9]
2018-12-18 10:31:54,589 - DEBUG - result is [2, 3, 5, 9, 9, 23].
Out[72]: [2, 3, 5, 9, 9, 23]

请注意,funcName是DEBUG而不是merge_sort

这怎么可能?

0 个答案:

没有答案