okta / okta-sdk-python

Apache License 2.0
234 stars 143 forks source link

Duplicate log lines when creating multiple client instances #372

Open v-yarotsky opened 1 year ago

v-yarotsky commented 1 year ago

Okta client version: 2.9.3

Summary

Creating an instance of the Client class has a side effect - it creates a new logging StreamHandler and attaches it to the package-level logger. Creating multiple client instances adds duplicate handlers, causing duplicate log lines. This is unexpected, inconvenient and even somewhat dangerous, as it can result in overwhelmed log collection systems and memory leaks. Since the client object is not meant to be a singleton (indeed, it's not currently possible to use it that way^1), the constructor should not have side effects.

Test script

import logging
from okta.client import Client as OktaClient

config = {
    "orgUrl": "https://example.com",
    "token": "foo",
    "logging": {
        "enabled": True,
    },
}

def make_client_and_run():
    OktaClient(config)
    logging.getLogger("okta-sdk-python").info("Hello from Okta logger")

def main():
    logging.info("Creating client #1")
    make_client_and_run()

    logging.info("Creating client #2")
    make_client_and_run()

    logging.info("Creating client #3")
    make_client_and_run()

main()

Expected result

2023-08-31 12:39:10,527 - okta-sdk-python - test - INFO - Hello from Okta logger
INFO:okta-sdk-python:Hello from Okta logger
2023-08-31 12:39:10,527 - okta-sdk-python - test - INFO - Hello from Okta logger
INFO:okta-sdk-python:Hello from Okta logger
2023-08-31 12:39:10,527 - okta-sdk-python - test - INFO - Hello from Okta logger
INFO:okta-sdk-python:Hello from Okta logger

Actual result

2023-08-31 12:39:10,527 - okta-sdk-python - test - INFO - Hello from Okta logger
INFO:okta-sdk-python:Hello from Okta logger
2023-08-31 12:39:10,527 - okta-sdk-python - test - INFO - Hello from Okta logger
2023-08-31 12:39:10,527 - okta-sdk-python - test - INFO - Hello from Okta logger
INFO:okta-sdk-python:Hello from Okta logger
2023-08-31 12:39:10,527 - okta-sdk-python - test - INFO - Hello from Okta logger
2023-08-31 12:39:10,527 - okta-sdk-python - test - INFO - Hello from Okta logger
2023-08-31 12:39:10,527 - okta-sdk-python - test - INFO - Hello from Okta logger
INFO:okta-sdk-python:Hello from Okta logger