【问题标题】:Python logging module prints unnecessary messagesPython 日志记录模块打印不必要的消息
【发布时间】:2017-03-03 15:26:44
【问题描述】:

请考虑这个伪代码。

$ cat dummy.py 
import logging
import time

from boto3.session import Session

# Logging Configuration
fmt = '%(asctime)s [%(levelname)s] [%(module)s] - %(message)s'
logging.basicConfig(level='INFO', format=fmt, datefmt='%m/%d/%Y %I:%M:%S')
logger = logging.getLogger()

def main():
    session = Session(region_name='us-west-2')
    client = session.client('ec2')
    response = client.describe_instances(InstanceIds=['i-11111111111111111'])

    logger.info('The instnace size is: %s', response[
                'Reservations'][0]['Instances'][0]['InstanceType'])

if __name__ == '__main__':
    main()

输出:

$ python3 dummy.py 
03/03/2017 08:47:00 [INFO] [credentials] - Found credentials in shared credentials file: ~/.aws/credentials
03/03/2017 08:47:01 [INFO] [connectionpool] - Starting new HTTPS connection (1): ec2.us-west-2.amazonaws.com
03/03/2017 08:47:02 [INFO] [dummy] - The instnace size is: t2.micro

问题: 如何避免打印下面的行?

03/03/2017 08:47:00 [INFO] [credentials] - Found credentials in shared credentials file: ~/.aws/credentials
03/03/2017 08:47:01 [INFO] [connectionpool] - Starting new HTTPS connection (1): ec2.us-west-2.amazonaws.com

如果我将logging.basicConfig(level='INFO',... 更改为logging.basicConfig(level='WARNING',...,则不会打印这些消息,但随后我的所有消息都会以WARNING 严重性记录。

我只希望logging 模块打印我使用logger.info .... 明确编写的消息,仅此而已。因此,我需要任何关于如何避免打印不必要的消息的指示。

【问题讨论】:

  • 你不能只为自己应用过滤器吗?
  • 虽然可能不是完全相同的副本,但您应该能够在 this question 中找到您要查找的内容。
  • @glibdud,你提到的问题对我来说不是解决方案/解决方法,我已经在我的帖子中提到过。感谢您的宝贵时间。
  • 链接的问题向您展示了如何访问其他模块的记录器(例如credentialsconnectionpool)并将它们设置为不同的日志级别。我在您的问题中没有看到您解决这个问题的任何地方。
  • @glibdud,我已经解决了。这个。我会尽快发布答案。

标签: python logging


【解决方案1】:

解决方案:

import logging
import time

from boto3.session import Session

# Logging Configuration
fmt = '%(asctime)s [%(levelname)s] [%(module)s] - %(message)s'
logging.basicConfig(format=fmt, datefmt='%m/%d/%Y %I:%M:%S')
logger = logging.getLogger('LUCIFER')
logger.setLevel(logging.INFO)


def main():
    COUNTER = 3
    session = Session(region_name='us-west-2')
    client = session.client('ec2')
    response = client.describe_instances(InstanceIds=['i-0a912622af142b510'])

    logger.info('The instnace size is: %s', response[
                'Reservations'][0]['Instances'][0]['InstanceType'])

if __name__ == '__main__':
    main()

输出:

$ python3 dummy.py 
03/03/2017 10:30:15 [INFO] [dummy] - The instnace size is: t2.micro

解释: 早些时候,我在根记录器上设置了INFO 级别。因此,所有其他没有设置级别的记录器,获取此级别propagated 并开始记录。在解决方案中,我专门在记录器LUCIFER 上启用此级别。

参考: 来自:https://docs.python.org/3/howto/logging.html

子记录器将消息传播到与其祖先记录器关联的处理程序。因此,没有必要为应用程序使用的所有记录器定义和配置处理程序。为顶级记录器配置处理程序并根据需要创建子记录器就足够了。 (但是,您可以通过将记录器的传播属性设置为 False 来关闭传播。)

除了与记录器直接关联的任何处理程序之外,还调用与记录器的所有祖先关联的所有处理程序来调度消息(除非记录器的传播标志设置为 false 值,此时传递到祖先处理程序停止)。

【讨论】:

    【解决方案2】:

    boto3 定义了一个函数 set_stream_logger,它覆盖了特定 boto3 命名空间的日志记录级别。

    原型:

    boto3.set_stream_logger(name='boto3', level=logging.DEBUG, format_string=None)

    例子:

    import boto3
    boto3.set_stream_logger(name='botocore.credentials', level=logging.WARNING)
    boto3.set_stream_logger(name='urllib3.connectionpool', level=logging.WARNING)
    

    botocore/credentials.py 使用这一行加载记录器:

    logger = logging.getLogger(__name__)
    

    在此文件中,__name__ 解析为 botocore.credentials,因此这将是调用 set_stream_logger 时的记录器名称。

    注意Starting new HTTPS connection 消息来自urllib3,它不是boto3 的一部分。 boto3.set_stream_logger 函数仍然有效,因为它作用于 logging 模块。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-26
      • 1970-01-01
      • 2016-03-10
      • 1970-01-01
      • 1970-01-01
      • 2013-08-15
      • 1970-01-01
      相关资源
      最近更新 更多