找bug的過程

關於昨天程序出差我找bug的過程記錄html

昨天才程序python

http://www.javashuo.com/article/p-gwsmcrsr-eo.htmlios

├── xxxx
│   ├── src.py
│   └── fil_mode.py
│   └── data_time.py
│   └── loading.py
│   └── data_time.py
│   └── logger.py

src.py與打印日誌相關片斷app

if __name__ == '__main__':
    logger_start('程序主界面')
    while True:
        print("\033[0;31;44m\t\t楊大爺超市歡迎您\t\t\t\t\033[5m")
        print("\033[0;30;42m\t\t輸入0進入註冊界面\t\t\t\033[5m")
        print("\033[0;30;42m\t\t輸入1進入登入界面\t\t\t\033[5m")
        print("\033[0;30;42m\t\t輸入2進入充值界面\t\t\t\033[5m")
        print("\033[0;30;42m\t\t輸入3進入餘額查詢界面\t\t\033[5m")
        print("\033[0;30;42m\t\t輸入4進入購物界面\t\t\t\033[5m")
        print("\033[0;30;42m\t\t輸入5進入解凍界面\t\t\t\033[5m")
        print("\033[0;30;42m\t\t輸入q退出程序 \t\t\t\t\033[5m")
        print(38 * '-')
        user_shopping_chiose = input('請輸入你要選擇的功能')
        if user_shopping_chiose not in msg_dict:
            print('請好好輸入')
            continue
        elif user_shopping_chiose=='q':
            print('再見')
            logger_end('程序主界面')
            break
        else:
            logger_start(f'{app_name_dict[user_shopping_chiose]}')
            msg_dict[user_shopping_chiose]()
            logger_end(f'{app_name_dict[user_shopping_chiose]}')

我將其中打印日誌出現問題的地方運行,其餘地方註釋掉函數

if __name__ == '__main__':
    #logger_start('程序主界面')
    while True:
        print("\033[0;31;44m\t\t楊大爺超市歡迎您\t\t\t\t\033[5m")
        print("\033[0;30;42m\t\t輸入0進入註冊界面\t\t\t\033[5m")
        print("\033[0;30;42m\t\t輸入1進入登入界面\t\t\t\033[5m")
        print("\033[0;30;42m\t\t輸入2進入充值界面\t\t\t\033[5m")
        print("\033[0;30;42m\t\t輸入3進入餘額查詢界面\t\t\033[5m")
        print("\033[0;30;42m\t\t輸入4進入購物界面\t\t\t\033[5m")
        print("\033[0;30;42m\t\t輸入5進入解凍界面\t\t\t\033[5m")
        print("\033[0;30;42m\t\t輸入q退出程序 \t\t\t\t\033[5m")
        print(38 * '-')
        user_shopping_chiose = input('請輸入你要選擇的功能')
        if user_shopping_chiose not in msg_dict:
            print('請好好輸入')
            continue
        elif user_shopping_chiose=='q':
            print('再見')
            logger_end('程序主界面')
            break
        else:
            logger_start(f'{app_name_dict[user_shopping_chiose]}')
            msg_dict[user_shopping_chiose]()
            #logger_end(f'{app_name_dict[user_shopping_chiose]}')

生成的日誌日誌

top_up程序啓動時間:2019-06-12 17:37:53  #運行一次top_up功能

top_up程序啓動時間:2019-06-12 17:37:55  #運行兩次top_up功能
top_up程序啓動時間:2019-06-12 17:37:55

top_up程序啓動時間:2019-06-12 17:37:57  #運行三次top_up功能
top_up程序啓動時間:2019-06-12 17:37:57
top_up程序啓動時間:2019-06-12 17:37:57

top_up程序啓動時間:2019-06-12 17:37:58  #運行四次top_up功能
top_up程序啓動時間:2019-06-12 17:37:58
top_up程序啓動時間:2019-06-12 17:37:58
top_up程序啓動時間:2019-06-12 17:37:58

咱們發現他運行後內側應該還存在日誌相關函數,咱們這時候看看原來的函數code

logger.pyorm

def logger_start(app):
    logger_login = logging.getLogger('start')
    fli_show = logging.FileHandler('app.log')
    logger_format = logging.Formatter('%(message)s程序啓動時間:%(asctime)s',
                     datefmt='%Y-%m-%d %X' )
    fli_show.setFormatter(logger_format)
    logger_login.addHandler(fli_show)
    logger_login.setLevel(10)
    logger_login.info(app)
    
#這時候我又三種想法,
#第一種把函數return出來,每次賦予不一樣的變量名,這裏能夠用時間生成函數來進行保證每次函數名不同
#第二種找可能會出現相同地方'logger_login'這個名字
#第三種找可能會出現相同地方logging.getLogger('start')中('start')這個名字

我選擇最容易修改的第三種進行修改htm

import logging
from data_time import data_time
def logger_login(user):
    logger_login = logging.getLogger(data_time()) #這是我自定義的一個函數返回值是當前時間每次都會不一樣
    fli_show = logging.FileHandler('user_login.log')
    logger_format = logging.Formatter('登入時間:%(asctime)s  %(levelname)s-用戶:%(message)s',
                     datefmt='%Y-%m-%d %X' )
    fli_show.setFormatter(logger_format)
    logger_login.addHandler(fli_show)
    logger_login.setLevel(10)
    logger_login.info(user)


def logger_exit(user):
    logger_login = logging.getLogger(data_time())
    fli_show = logging.FileHandler('user_login.log')
    logger_format = logging.Formatter('退出時間:%(asctime)s  %(levelname)s-用戶:%(message)s',
                     datefmt='%Y-%m-%d %X' )
    fli_show.setFormatter(logger_format)
    logger_login.addHandler(fli_show)
    logger_login.setLevel(10)
    logger_login.info(user)

def logger_start(app):
    logger_login = logging.getLogger(data_time())
    fli_show = logging.FileHandler('app.log')
    logger_format = logging.Formatter('%(message)s程序啓動時間:%(asctime)s',
                     datefmt='%Y-%m-%d %X' )
    fli_show.setFormatter(logger_format)
    logger_login.addHandler(fli_show)
    logger_login.setLevel(10)
    logger_login.info(app)
    

def logger_end(app):
    logger_login = logging.getLogger(data_time())
    fli_show = logging.FileHandler('app.log')
    logger_format = logging.Formatter('%(message)s程序關閉時間:%(asctime)s',
                     datefmt='%Y-%m-%d %X' )
    fli_show.setFormatter(logger_format)
    logger_login.addHandler(fli_show)
    logger_login.setLevel(10)
    logger_login.info(app)

def logger_shopping(user,car):
    logger_login = logging.getLogger(data_time())
    fli_show = logging.FileHandler('user_shopping.log')
    logger_format = logging.Formatter('%(asctime)s  %(levelname)s-%(message)s',
                     datefmt='%Y-%m-%d %X' )
    fli_show.setFormatter(logger_format)
    logger_login.addHandler(fli_show)
    logger_login.setLevel(10)
    logger_login.info(user,car)

最後呢解決了blog

相關文章
相關標籤/搜索