python之列印日誌logging

來源:互聯網
上載者:User

標籤:filename   ogg   bsp   檔案   and   not   python   was   inf   

 

 1 import logging 2  3  4 # 簡單列印日誌舉例 5 logging.basicConfig(level=logging.DEBUG)  # 設定記錄層級,WARN 6 logging.warning(‘Watch out!‘)  # will print a message to the console 7 logging.info(‘I told you so‘)  # will not print anything 8  9 10 # 列印日誌到檔案,注意要新起一個檔案,否則不能儲存檔案11 def log_to_file(logs_dir="D:\\test_data\\logs\\log_DEBUG.txt"):12     logging.basicConfig(filename=logs_dir, level=logging.DEBUG)13     logging.debug(‘This message should go to the log file‘)14     logging.info(‘So should this‘)15     logging.warning(‘And this, too‘)16 17 18 log_to_file()19 20 # 多參數日誌21 logging.warning(‘%s before you %s‘, ‘Look‘, ‘leap!‘)22 23 # 日誌中列印時間24 logging.basicConfig(format=‘%(asctime)s %(message)s‘)25 logging.warning(‘is when this event was logged.‘)26 27 # 指定時間格式28 logging.basicConfig(format=‘%(asctime)s %(message)s‘, datefmt=‘%Y-%m-%d %I:%M:%S %p‘)29 logging.warning(‘is when this event was logged.‘)
注意:logging的參數設定只在第一次運行之前有效,只能設定一次,後續設定無效。

指定時間格式運行結果:
1 # 指定時間格式2 2017-08-12 10:47:07 PM is when this event was logged.

 

python之列印日誌logging

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.