Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable boto logging without modifying the boto files

I am using the Boto library to talk to AWS. I want to disable logging. (Or redirect to /dev/null or other file). I can't find an obvious way to do this. I tried this, but that doesn't seem to help:

import boto
boto.set_file_logger('boto', 'logs/boto.log')

This says it is possible, http://developer.amazonwebservices.com/connect/thread.jspa?messageID=52727&#52727 but as far as I know the documentation doesn't tell how.

like image 475
agiliq Avatar asked Nov 02 '09 13:11

agiliq


3 Answers

You could try

import logging
logging.getLogger('boto').setLevel(logging.CRITICAL)

which will suppress all (other than CRITICAL) errors.

Boto uses logging configuration files (e.g. /etc/boto.cfg, ~/.boto) so see if you can configure it to your needs that way.

The set_file_logger call simply adds a user-defined file to the logging setup, so you can't use that to turn logging off.

like image 144
Vinay Sajip Avatar answered Nov 10 '22 14:11

Vinay Sajip


I move the boto3 answer from the comments (namely charneykaye and gene_wood) to a proper answer:

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)

To get all the loggers follow the response from leobarcellos:

import logging
loggers_dict = logging.Logger.manager.loggerDict
like image 52
lony Avatar answered Nov 10 '22 14:11

lony


This is the only solution, which works for me as of today (2020/01/31):

for name in ['boto', 'urllib3', 's3transfer', 'boto3', 'botocore', 'nose']:
    logging.getLogger(name).setLevel(logging.CRITICAL)
logger = logging.getLogger(__name__)

The solution with

boto3.set_stream_logger('', logging.CRITICAL)

was killing my whole non-boto logs. It manipulates the root logger of the standard logging from python.

Try it out for yourself:

import logging
import boto3
import sys
logger = logging.getLogger(__name__)
boto3.set_stream_logger('', logging.CRITICAL)
logging.basicConfig(level=logging.DEBUG, stream=sys.stdout,
                    format='%(asctime)s - %(levelname)s - %(message)s')


if __name__ == '__main__':
    s3_client = boto3.client('s3')
    response = s3_client.list_buckets()
    logger.info(f'bucket list: {response}')

Regardless of where the init of the logger happens, it won't bring up the output. Remove the line of the boto3.set_stream_logger('', logging.CRITICAL) and the non-boto3 logs will re-appear again! Consequently the only working solution is NOT to use the approach with boto3.set_stream_logger() and apply it as I suggested.

like image 12
mchlfchr Avatar answered Nov 10 '22 16:11

mchlfchr