禁用博托记录,而无需修改博托文件
-
12-09-2019 - |
题
我现在用的是Boto程式库交谈AWS。我想禁用日志记录。 (或重定向到/ dev / null或其他文件)。我无法找到一个明显的方式做到这一点。我想这一点,但似乎并没有帮助。
import boto
boto.set_file_logger('boto', 'logs/boto.log')
这表示,它是可能的,
解决方案
您可以尝试
import logging
logging.getLogger('boto').setLevel(logging.CRITICAL)
这将抑制(超过临界其他)错误的所有
博托使用日志记录配置文件(例如/etc/boto.cfg
,~/.boto
),所以看看你是否可以将其配置为您的需求的方式。
在set_file_logger
呼叫只是增加了一个用户定义的文件到日志设置,所以不能用它来关闭日志记录。
其他提示
我的boto3答案从注释(即charneykaye和gene_wood)到适当的答案移动:
import logging
logger = logging.getLogger()
logger.addHandler(logging.StreamHandler()) # Writes to console
logger.setLevel(logging.DEBUG)
logging.getLogger('boto3').setLevel(logging.CRITICAL)
logging.getLogger('botocore').setLevel(logging.CRITICAL)
logging.getLogger('s3transfer').setLevel(logging.CRITICAL)
logging.getLogger('urllib3').setLevel(logging.CRITICAL)
import boto3
s3 = boto3.resource('s3')
for bucket in s3.buckets.all():
print(bucket.name)
要得到所有的记录器按照从leobarcellos 中响应:
import logging
loggers_dict = logging.Logger.manager.loggerDict
更好的是,用于博托禁用propagate
:
import boto
boto.set_file_logger('boto', 'logs/boto.log')
logging.getLogger('boto').propagate = False
不隶属于 StackOverflow