視頻 | Python測試開發之調試print代碼實例

視頻內容
# -*- coding: utf-8 -*-

__author__ = "苦葉子"

"""

公衆號: 開源優測

Email: [email protected]

"""

import sys


# 定義print輸出級別, 用於控制print輸出
# 優先級 DEBUG > INFO > WARNING > ERROR
class Print:
    DEBUG = 0
    INFO = 1
    WARNING = 2
    ERROR = 3
    PRINT_TIP = {DEBUG: "Debug", INFO: "Info", WARNING: "Warn", ERROR: "Error"}


# 全局print控制標誌
PRINT_LEVEL = Print.ERROR


# 自定義print輸出
# msg - 輸出內容
# level - 輸出級別控制
def print_console(msg, level=Print.INFO):
    filename = sys._getframe().f_code.co_filename
    func = sys._getframe().f_code.co_name
    line = sys._getframe().f_lineno
    if level >= PRINT_LEVEL:
        print("In File: %s, Function: %s @line: %s  %s: %s" % (filename, func, line, Print.PRINT_TIP[level], msg))


# 測試print_console
def test_print_console():
    print_console("這是debug輸出...", Print.DEBUG)

    print_console("這是info輸出....", Print.INFO)

    print_console("這是warning輸出...", Print.WARNING)

    print_console("這是error輸出", Print.ERROR)

    print("---" * 10)


if __name__ == "__main__":
    print("print輸出示例")

    # 設置輸出爲DEBUG級別
    PRINT_LEVEL = Print.DEBUG
    test_print_console()

    # 設置輸出爲INFO級別
    PRINT_LEVEL = Print.INFO
    test_print_console()

    # 設置輸出爲Warning級別
    PRINT_LEVEL = Print.WARNING
    test_print_console()

    # 設置輸出爲ERROR級別
    PRINT_LEVEL = Print.ERROR
    test_print_console()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章