cisco-en-programmability / dnacentersdk

Cisco DNA Center Python SDK
https://dnacentersdk.readthedocs.io/en/latest/
MIT License
70 stars 33 forks source link

dnacentersdk appears to add extra logging.StreamHandler() #17

Closed jordanjms17 closed 3 years ago

jordanjms17 commented 3 years ago

I have boiled this down to a basic issue introduced when importing dnacentersdk, so I'm using the basic logging example from https://docs.python.org/3.7/howto/logging.html#configuring-logging to demonstrate the issue. When DNACenterAPI is imported, logging messages are sent to stdout twice (once following the configured formatter and once unformatted)

import logging
from dnacentersdk import DNACenterAPI

# create logger
logger = logging.getLogger('simple_example')
logger.setLevel(logging.DEBUG)

# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)

# create formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

# add formatter to ch
ch.setFormatter(formatter)

# add ch to logger
logger.addHandler(ch)

# 'application' code
logger.debug('debug message')

Output with from dnacentersdk import DNACenterAPI

2020-10-09 15:16:49,099 - simple_example - DEBUG - debug message
DEBUG:simple_example:debug message

Output without from dnacentersdk import DNACenterAPI

2020-10-09 15:22:38,215 - simple_example - DEBUG - debug message

Any thoughts on how to stop the extra output would be appreciated.

Thanks!

robertcsapo commented 3 years ago

It's because dnacentersdk is configured to create a StreamHandler as well.

https://github.com/cisco-en-programmability/dnacentersdk/blob/b752b32091deb010a6c61306bf3b059ce83d7e70/dnacentersdk/__init__.py#L55

If we remove this line (L55 in __init__.py), there would only be one logging stream (as you expect in your sample code).

pip install dnacentersdk
git clone https://github.com/cisco-en-programmability/dnacentersdk.git
cd dnacentersdk
sed -i 's/logging.basicConfig()/#logging.basicConfig()/' dnacentersdk/__init__.py 

Then run your sample script.

python issue_17.py 
2020-10-14 20:05:35,525 - simple_example - DEBUG - debug message

I'll let the maintainers comment on how they want to proceed ☺️

jbogarin commented 3 years ago

Let me review it this weekend and get back to you. We are working on upgrading the SDK to 2.1.2 and we should be able to fix this when we release that version.

jbogarin commented 3 years ago

We are working on updating the SDK to include support for DNAC 2.1.2. We'll improve the logging for that release, which should be ready in two to three weeks.

jordanjms17 commented 3 years ago

This one appears to be fixed in v2.0.2 of the SDK. Closing the issue.

Thanks for the help @jbogarin and @robertcsapo

jbogarin commented 3 years ago

@jordanjms17 thanks, sorry I didn't let you know but we are working on including some extra stuff around the DNA Center SDK for the end of November and haven't had the time to check the issues and close those that are fixed.

Thanks for letting us know that it was solved.

We did update the docs https://dnacentersdk.readthedocs.io/en/latest/api/quickstart.html#configuring-logging-for-dnacentersdk so maybe that also helps.