python大型项目中的日志模块,如何仅在Python中设置模块的日志记录级别?

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值