0

I ran these lines in a Jupyter notebook cell:

session = boto3.session.Session(profile_name="profile1",region_name="us-east-1")
sts_client = session.client('sts')
account = sts_client.get_caller_identity()['Account']
logger.info("Session is active: {}".format(account))

When I run these lines, I get this output:

Found credentials in shared credentials file: ~/.aws/credentials
Found credentials in shared credentials file: ~/.aws/credentials
Found credentials in shared credentials file: ~/.aws/credentials
Found credentials in shared credentials file: ~/.aws/credentials
Found credentials in shared credentials file: ~/.aws/credentials
Session is active: 1234
Session is active: 1234
Session is active: 1234
Session is active: 1234
Session is active: 1234

My question is, why is my output repeating lines? Why is "Session is active" message getting printed 5 times?

Thank you very much! June

shimo
  • 2,156
  • 4
  • 17
  • 21
juneyangdata
  • 31
  • 1
  • 4
  • How is your logger created? At a guess, you have accidentally created it 5 times so 5 log handlers have been added and each prints the same thing. Other ideas [here](https://stackoverflow.com/questions/7173033/duplicate-log-output-when-using-python-logging-module). – jarmod Nov 04 '21 at 01:52

1 Answers1

0

This is from that handlers add info every time jupyter is run.

You can clear these by:

logger.handlers = []

or

logger.handlers.clear()

If the root handler meet the same situation,

logger.root.handlers.clear()

will work.

shimo
  • 2,156
  • 4
  • 17
  • 21
  • 1
    Thanks shimo! I was using "logger.addHandler(logging.StreamHandler(sys.stdout))" to print logger output to stdout. After I cleared the handlers, I was getting no output at all. Instead, I used "logging.basicConfig(stream=sys.stdout)" to print to output and now is printing only once like expected! Thanks a lot :) – juneyangdata Nov 04 '21 at 21:30