【发布时间】: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,你提到的问题对我来说不是解决方案/解决方法,我已经在我的帖子中提到过。感谢您的宝贵时间。
-
链接的问题向您展示了如何访问其他模块的记录器(例如
credentials、connectionpool)并将它们设置为不同的日志级别。我在您的问题中没有看到您解决这个问题的任何地方。 -
@glibdud,我已经解决了。这个。我会尽快发布答案。