Python3 logging模組&ConfigParser模組,python3logging模組

來源:互聯網
上載者:User

Python3 logging模組&ConfigParser模組,python3logging模組

  1 '''  2 部落格園 Infi_chu  3 '''  4   5 '''  6 logging模組  7 該模組是關於日誌相關操作的模組  8 '''  9  10 import logging 11  12 # logging.debug('debug') 13 # logging.info('info') 14 # logging.warning('warning')      # 預設層級,上列印不出來,下可以列印,但許可權可改 15 # logging.error('error') 16 # logging.critical('critical') 17  18 ''' 19 部落格園 Infi_chu 20 ''' 21  22 ''' 23 記錄層級等級 24 critical>error>warning>info>debug>notset 25 ''' 26  27 # 配置日誌 28 # logging.basicConfig(level=logging.DEBUG,    # 層級 29 #                     format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',       # 格式 30 #                     datefmt='%a, %d %b %Y %H:%M:%S',    # asctime的格式 31 #                     filename='F:\Python project\log.txt',   # 檔案名稱 32 #                     filemode='w')   # 檔案模式 33  34 ''' 35 可用參數 36 filename:用指定的檔案名稱建立FiledHandler(後邊會具體講解handler的概念),這樣日誌會被儲存在指定的檔案中。 37 filemode:檔案開啟檔案,在指定了filename時使用這個參數,預設值為“a”還可指定為“w”。 38 format:指定handler使用的日誌顯示格式。  39 datefmt:指定日期時間格式。  40 level:設定rootlogger(後邊會講解具體概念)的記錄層級  41 stream:用指定的stream建立StreamHandler。可以指定輸出到sys.stderr,sys.stdout或者檔案(f=open('test.log','w')),預設為sys.stderr。若同時列出了filename和stream兩個參數,則stream參數會被忽略。 42 ''' 43  44 ''' 45 部落格園 Infi_chu 46 ''' 47  48  49 ''' 50 format參數中可能用到的格式化串: 51 %(name)s Logger的名字 52 %(levelno)s 數字形式的記錄層級 53 %(levelname)s 文本形式的記錄層級 54 %(pathname)s 調用日誌輸出函數的模組的完整路徑名,可能沒有 55 %(filename)s 調用日誌輸出函數的模組的檔案名稱 56 %(module)s 調用日誌輸出函數的模組名 57 %(funcName)s 調用日誌輸出函數的函數名 58 %(lineno)d 調用日誌輸出函數的語句所在的程式碼 59 %(created)f 目前時間,用UNIX標準的表示時間的浮 點數表示 60 %(relativeCreated)d 輸出日誌資訊時的,自Logger建立以 來的毫秒數 61 %(asctime)s 字串形式的目前時間。預設格式是 “2017-10-18 19:05:36,765”。逗號後面的是毫秒 62 %(thread)d 線程ID。可能沒有 63 %(threadName)s 線程名。可能沒有 64 %(process)d 進程ID。可能沒有 65 %(message)s使用者輸出的訊息 66 ''' 67  68 ''' 69 部落格園 Infi_chu 70 ''' 71  72 # logging.debug('debug') 73 # logging.info('info') 74 # logging.warning('warning') 75 # logging.error('error') 76 # logging.critical('critical') 77  78 ''' 79 部落格園 Infi_chu 80 ''' 81 # 另外一個模組層級別的函數 82 import  logging 83 # logger = logging.getLogger() 84 # d1 = logging.FileHandler('asd.log')     # 建立一個檔案輸出對象,handler,用於寫入記錄檔,FileHandler是檔案輸出資料流對象 85 # d2 = logging.StreamHandler()             # 建立一個螢幕輸出對象,標準輸出資料流,用於輸出到控制台(螢幕) 86 # format1 = logging.Formatter('%(asctime)s %(name)s %(levelname)s %(message)s')    # 格式 87 # d1.setFormatter(format1)    # 用上面一行代碼規定的格式輸出 88 # d2.setFormatter(format1)    # 用上面一行代碼規定的格式輸出 89 # logger.addHandler(d1)       # 將d1加入到logger中,檔案輸出顯示 90 # logger.addHandler(d2)       # 將d2加入到logger中,控制台輸出顯示 91 # 92 # logger.debug('debug') 93 # logger.info('info') 94 # logger.warning('warning') 95 # logger.error('error') 96 # logger.critical('critical') 97  98 ''' 99 部落格園 Infi_chu100 '''101 102 '''103 ConfigParser模組(Python3)104 該模組用於產生和修改常見的配置文檔105 '''106 import configparser107 108 # config_file1 = configparser.ConfigParser()        # 調用配置操作控制代碼109 #110 # # 建立111 # # 方法一112 # config_file1['DEFAULT'] = {'ServerAliveInterval':45,113 #                            'Compression':'yes',114 #                            'CompressionLevel':9}115 # # 方法二116 # config_file1['DEFAULT1'] = {}117 # config_file1['DEFAULT1']['USER'] = 'bob'118 # # 方法三119 # add_group = config_file1['DEFAULT1']120 # add_group['GROUP'] = 'manager'121 #122 # with open('config_file.ini','w') as configfile:     # 寫入,configfile不要動123 #     config_file1.write(configfile)124 125 '''126 部落格園 Infi_chu127 '''128 129 # # 讀檔案130 # rd = configparser.ConfigParser()  # 調用配置操作控制代碼131 # rd.read('config_file.ini')132 # print(rd.sections())      # 讀出了大標題,也就是上述配置過程中字典的鍵,除了預設133 # print(rd.defaults())      # 讀出大標題,唯讀出了預設134 # print(rd['DEFAULT1']['group'])  # 取大標題下的值135 #136 # for i in rd:137 #     print(i)        # 查看大標題138 #139 # for i in rd['DEFAULT']:140 #     print(i)        # 查看大標題下的鍵,只顯示DEFAULT141 #142 # for i in rd['DEFAULT1']:143 #     print(i)        # 查看大標題下的鍵,包括DEFAULT144 '''145 部落格園 Infi_chu146 '''147 # # 刪除配置資訊148 # rmv = configparser.ConfigParser()149 # rmv.remove_section('DEFAULT1')150 # rmv.write(open('config_file1.ini','w'))151 # rmv.remove_option('DEFAULT','compressionlevel')     # 刪除鍵中鍵152 # rmv.write(open('config_file.ini','w'))153 #154 # # 查看配置中是否有此條資訊155 # find1 = configparser.ConfigParser()156 # print(find1.has_section('DEFAULT'))157 # find1.write(open('config_file.ini','r'))158 #159 # # 修改設定檔160 # conf = configparser.ConfigParser()161 # print(conf.set('DEFAULT1','group','123'))162 # conf.write(open('config_file.ini','w'))163 '''164 部落格園 Infi_chu165 '''

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.