程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

python logging

編輯:Python

python logging Is to add... To the code log, The module used is logging

python log Yes 5 Class :

''' DEBUG INFO WARNING ERROR CRITICAL '''

If logging.basicConfig Method does not set level Parameters , Then the default output level by Warning level ,Warning Below grade log That is to say INFO and DEBUG No output .

If level Set to logging.DEBUG All kinds of log All output .

Set up filename, take log The output is written to the specified file , Do not set console Output log.

logging.getLogger('test_logger') If the parameter is set to __name__ Then each module has its own log. In different modules , If this parameter is the same , Then belong to the same log.

import logging
logging.basicConfig(format="%(asctime)s %(levelname)-8s: [%(filename)s:%(lineno)d] %(message)s",
level=logging.DEBUG,
filename="logs.txt")
logger = logging.getLogger('test_logger')
logger.info('log msg.')
logger.warning('warning msg')
logger.debug("debug msg")
logger.critical("critical msg")
logger.error("error msg")

logs.txt Content :

2022-06-24 18:26:02,631 INFO : [app.py:7] log msg.
2022-06-24 18:26:02,631 WARNING : [app.py:8] warning msg
2022-06-24 18:26:02,631 DEBUG : [app.py:9] debug msg
2022-06-24 18:26:02,631 CRITICAL: [app.py:10] critical msg
2022-06-24 18:26:02,631 ERROR : [app.py:11] error msg

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved