簡體   English   中英

如何僅在Python中設置模塊的日志記錄級別?

[英]How to set logging level for the module only in Python?

我正在使用logging.info輸出有關我的腳本正在執行的操作的信息,並且我正在使用logging.basicConfig(level=logging.INFO)來啟用此功能。 這個( logging.basicConfig(level=logging.INFO) )也影響我調用的其他模塊(例如SQLAlchemy),導致比我想要的更詳細的輸出。 我可以將我的實際腳本的日志記錄級別設置為INFO,但不能將其使用的第三方模塊設置為INFO(我更喜歡將它們作為警告)?

執行此操作的常規方法是在模塊的開頭為當前模塊定義記錄器(通常基於文件名),並在整個過程中參考。

import logging

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

def function_that_logs():
    logger.info('will log')   # note, using logger not logging
    logger.debug('will not log')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM