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

眉间皱痕 提交于 2020-03-04 07:15:34

问题


I am using logging.info to output information about what my script is doing and I am using logging.basicConfig(level=logging.INFO) to enable this. And this (logging.basicConfig(level=logging.INFO)) also affects other modules I invoke (e.g. SQLAlchemy) causing a way much more verbose output than I want. Can I set the logging level to INFO for my actual script but not for the 3rd-party modules it uses (I'd prefer it to be WARNING for them)?


回答1:


The normal way to do this is to define a logger for the current module - usually based on the file name - at the start of the module, and refer to this throughout.

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')


来源:https://stackoverflow.com/questions/48787538/how-to-set-logging-level-for-the-module-only-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!