python logging 工具

來源:互聯網
上載者:User

標籤:sts   記錄   class   __file__   enc   handler   mod   logger   python   

使用 python logging記錄debug 等日誌資訊

debug 以上資訊寫入檔案

info 以上資訊輸出在控制台

import osimport loggingVDE_LOGGING_NAME = "vde_logging"LOG_FILE_PATH = os.path.join(os.path.dirname(__file__), "vde_regression.log")def logger_initialization():    # 1. logging    logger = logging.getLogger(VDE_LOGGING_NAME)    logger.setLevel(logging.DEBUG)    # 2.handler    # file handler    fh = logging.FileHandler(LOG_FILE_PATH, mode="w")    fh.setLevel(logging.DEBUG)    # standard control console    ch = logging.StreamHandler()    ch.setLevel(logging.INFO)    # 3.format    formatter = logging.Formatter("[%(asctime)s %(name)s].%(levelname)s: %(message)s")    fh.setFormatter(formatter)    ch.setFormatter(formatter)    logger.addHandler(fh)    logger.addHandler(ch)def debug_logging_recoder(debug_msg):    vde_logging = logging.getLogger(VDE_LOGGING_NAME)    vde_logging.debug(debug_msg)def info_logging_recoder(info_msg):    vde_logging = logging.getLogger(VDE_LOGGING_NAME)    vde_logging.info(info_msg)if __name__ == ‘__main__‘:    logger_initialization()    debug_logging_recoder("debug")    info_logging_recoder("info")

  References:

http://www.zlovezl.cn/articles/replacing-print-simple-introduction-to-logging/

https://blog.igevin.info/posts/python-log/

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.