问题描述
我正在使用logging.info
输出有关脚本正在执行的操作的信息,并且我正在使用logging.basicConfig(level=logging.INFO)
启用此功能.并且此(logging.basicConfig(level=logging.INFO)
)还影响我调用的其他模块(例如SQLAlchemy),从而导致输出比我想要的还要冗长得多的方式.我可以为我的实际脚本将日志记录级别设置为INFO,但不能为它使用的第三方模块设置日志级别(我希望对它们使用警告)吗?
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)?
推荐答案
执行此操作的通常方法是在模块的开头为当前模块定义一个记录器(通常基于文件名),并参考这整个.
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')
这篇关于如何仅在Python中设置模块的日志记录级别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!