Skip to content

Duplicate log lines when creating multiple client instances #372

@v-yarotsky

Description

@v-yarotsky

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 way1), 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

Footnotes

  1. https://github.com/okta/okta-sdk-python/issues/363

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions