給blog添加log日誌
阿新 • • 發佈:2018-12-03
頁面 document 配置 文章 技術 dict disable ask evel
bin/start.py
# import sys import sys import os # sys.path.append(r‘F:\python\python練習\day-07\blog‘) # from core import src # src.run() # print(os.path.dirname(__file__)) # print(os.path.dirname(os.path.dirname(__file__))) #動態獲取你的項目目錄(你所在的目錄下的上級目錄) # print(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) #動態獲取你的上一級目錄的上一級BASE_PATH = os.path.dirname(os.path.dirname(__file__)) sys.path.append(BASE_PATH) from core import src if __name__ == ‘__main__‘: src.run()
conf/settings.py
DB_PATH = r‘F:\python\python練習\day-08\blog\db\register‘ LOG_PATH = r‘F:\python\python練習\day-08\blog\log\access.log‘ """ logging配置""" import os import logging.config # 定義三種日誌輸出格式 開始 standard_format = ‘[%(asctime)s][%(threadName)s:%(thread)d][task_id:%(name)s][%(filename)s:%(lineno)d]‘ ‘[%(levelname)s][%(message)s]‘ #其中name為getlogger指定的名字 simple_format = ‘[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s‘ id_simple_format = ‘[%(levelname)s][%(asctime)s] %(message)s‘ # 定義日誌輸出格式 結束 logfile_dir = os.path.dirname(os.path.abspath(__file__)) # log文件的目錄 logfile_name = ‘高配版.log‘ # log文件名 # 如果不存在定義的日誌目錄就創建一個 # if not os.path.isdir(logfile_dir): # os.mkdir(logfile_dir) # log文件的全路徑 logfile_path = os.path.join(logfile_dir, logfile_name) # log配置字典 LOGGING_DIC = { ‘version‘: 1, ‘disable_existing_loggers‘: False, ‘formatters‘: { ‘standard‘: { ‘format‘: standard_format }, ‘simple‘: { ‘format‘: simple_format }, }, ‘filters‘: {}, ‘handlers‘: { #打印到終端的日誌 ‘console‘: { ‘level‘: ‘DEBUG‘, ‘class‘: ‘logging.StreamHandler‘, # 打印到屏幕 ‘formatter‘: ‘simple‘ }, #打印到文件的日誌,收集info及以上的日誌 ‘default‘: { ‘level‘: ‘DEBUG‘, ‘class‘: ‘logging.handlers.RotatingFileHandler‘, # 保存到文件 ‘formatter‘: ‘standard‘, ‘filename‘: LOG_PATH, # 日誌文件 ‘maxBytes‘: 1000, # 日誌大小 5M ‘backupCount‘: 5, ‘encoding‘: ‘utf-8‘, # 日誌文件的編碼,再也不用擔心中文log亂碼了 }, }, ‘loggers‘: { #logging.getLogger(__name__)拿到的logger配置 ‘‘: { ‘handlers‘: [‘default‘, ‘console‘], # 這裏把上面定義的兩個handler都加上,即log數據既寫入文件又打印到屏幕 ‘level‘: ‘DEBUG‘, ‘propagate‘: True, # 向上(更高level的logger)傳遞 }, }, }
core/src.py
# from lib import logger from lib import commom from conf import settings log1 = commom.get_logger(‘購物車相關:‘) log2 = commom.get_logger(‘日記頁面相關:‘) def register(): with open(settings.DB_PATH, encoding=‘utf-8‘, mode=‘a+‘) as f1: f1.write(‘太白金星|123\n‘) def login(): pass # @logger def comment(): print(‘歡迎訪問評論頁面‘) # @logger # @commom.get_logger def article(): print(‘歡迎訪問文章頁面‘) # @logger def diary(): log2.info(‘日記頁面出現問題‘) print(‘日記:歡迎訪問日記頁面‘) def shopping(): log1.info(‘購物車:小李花了300買了一個娃娃...‘) # log1.error(‘支付失敗。。‘) print(‘歡迎進入購物車‘) dic = { 1: register, 2: login, 3: comment, 4: article, 5: diary, 6: shopping, } def run(): while 1: print(‘‘‘ 1: register, 2: login, 3: comment, 4: article, 5: diary 6: shopping ‘‘‘) choice = input(‘請輸入‘) if choice.isdigit(): choice = int(choice) dic[choice]() else: print(‘請重新輸入‘)
lib/commom.py
from conf import settings import os import logging.config def logger(f): def inner(*args, **kwargs): with open(settings.LOG_PATH, encoding=‘utf-8‘, mode=‘a+‘) as f1: f1.write(‘您訪問了%s‘ % (f.__name__)) ret = f(*args, **kwargs) return ret return inner def get_logger(taskname): logging.config.dictConfig(settings.LOGGING_DIC) # 導入上面定義的logging配置 logger = logging.getLogger(taskname) # 生成一個log實例 # logger.info(‘高配版開始運行了!‘) # 記錄該文件的運行狀態 return logger # if __name__ == ‘__main__‘: # load_my_logging_cfg()
執行結果:
執行順序:
給blog添加log日誌