Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] OpenBB Platform hijacks default logger #6680

Closed
ncimino opened this issue Sep 19, 2024 · 4 comments · Fixed by #6681
Closed

[Bug] OpenBB Platform hijacks default logger #6680

ncimino opened this issue Sep 19, 2024 · 4 comments · Fixed by #6681
Labels
bug Fix bug

Comments

@ncimino
Copy link

ncimino commented Sep 19, 2024

Describe the bug
By importing openbb the default logger is manipulated impacting host applications.

To Reproduce
Run this code:

import logging
logging.basicConfig(level=logging.INFO)
logging.info("This message is shown")
import openbb
logging.info("This message is hidden")

Notice: That the host application messages which are printed after importing openbb no longer function the same.

At the very least it would be nice to be able to disable this behavior with a user/system setting so that the host application can control logging. Yes the application can override, but because this happens when openbb is imported it can happen at any time while the application is running. This design effectively forces the user to import openbb at a controlled location to then override these settings which is of course not ideal.

Desktop (please complete the following information):

  • OS: Mac affects all OSes
  • Python version 3.11 affects all versions

Additional context
I believe this is due to the LoggingService changing the default logging configuration:
https://github.com/OpenBB-finance/OpenBB/blob/develop/openbb_platform/core/openbb_core/app/logs/logging_service.py#L122-L128

        logging.basicConfig(
            level=self._logging_settings.verbosity,
            format=FormatterWithExceptions.LOGFORMAT,
            datefmt=FormatterWithExceptions.DATEFORMAT,
            handlers=[],
            force=True,
        )
@deeleeramone
Copy link
Contributor

deeleeramone commented Sep 19, 2024

Hi there,

I don't believe this is a bug. Warnings and errors are handled internally by the application and passed forward. The OpenBB environment can be configured to display console messages and/or tracebacks.

You can enable warnings verbosity in the file, ~/.openbb_platform/user_settings.json

{
    "preferences": {
        "show_warnings": false,
    }
}

Debug mode is enabled with an environment variable. This passes through the complete Exception with traceback.

OPENBB_DEBUG_MODE=true

@montezdesousa, do you have anything to add that can clarify?

@deeleeramone deeleeramone added the bug Fix bug label Sep 19, 2024
@ncimino
Copy link
Author

ncimino commented Sep 20, 2024

Thanks @deeleeramone

I am still a little concerned though, because just importing a package should not alter the behavior of my app which is what’s happening.

@piiq
Copy link
Contributor

piiq commented Sep 20, 2024

Hey @ncimino 👋 Thanks for bringing this to our attention. While we look into this ourselves if you have any suggestions on how to change the LoggingService, they would be very appreciated.

@ncimino
Copy link
Author

ncimino commented Sep 20, 2024

It is not standard practice for a library to alter the root logging configuration upon import, especially in a way that affects the host application's logging behavior. Libraries are expected to be good citizens in the logging ecosystem by:

  • Using Library-Specific Loggers: Libraries should create their own loggers using logging.getLogger(name) and avoid configuring the root logger. This ensures that logging from the library can be controlled separately from the application's logging.
  • Avoiding Global Configuration Changes: Libraries should not call logging.basicConfig() or alter global logging settings, as this can interfere with the host application's logging configuration.
  • Providing Configurable Logging: If a library needs specific logging behavior, it should provide configuration options that the host application can use to adjust the library's logging without affecting the global logging configuration.

In this specific case, the issue arises because OpenBB's LoggingService calls logging.basicConfig() with force=True and handlers=[] upon import, which resets the root logger configuration. This overrides any logging settings that the host application has already configured, leading to the unexpected behavior reported here.

A potential fix might be:

def _setup_handlers(self) -> HandlersManager:
    """Set up Logging Handlers.

    Returns
    -------
    HandlersManager
        Handlers Manager object.
    """
    # Get the library-specific logger
    logger = logging.getLogger('openbb')

    # Configure the library's logger without affecting the root logger
    logger.setLevel(self._logging_settings.verbosity)
    handler = logging.StreamHandler()
    formatter = logging.Formatter(
        fmt=FormatterWithExceptions.LOGFORMAT,
        datefmt=FormatterWithExceptions.DATEFORMAT
    )
    handler.setFormatter(formatter)
    logger.addHandler(handler)

    handlers_manager = HandlersManager(settings=self._logging_settings)

    logger.info("Logging configuration finished")
    logger.info("Logging set to %s", self._logging_settings.handler_list)
    logger.info("Verbosity set to %s", self._logging_settings.verbosity)
    logger.info(
        "LOGFORMAT: %s%s",
        FormatterWithExceptions.LOGPREFIXFORMAT.replace("|", "-"),
        FormatterWithExceptions.LOGFORMAT.replace("|", "-"),
    )

    return handlers_manager

This fix addresses the problem by modifying its logging setup to avoid altering the root logger configuration upon import.

This change will:

  • Remove Global Configuration Calls: The call to logging.basicConfig() with force=True and handlers=[] should be removed from the import-time code. This prevents the library from interfering with the host application's logging configuration.
  • Configure Library-Specific Loggers: OpenBB should configure logging only for its own loggers. This can be done by obtaining a logger with logging.getLogger('openbb') and configuring it as needed.

Some other improvements not included here:

  • Provide Initialization Functions: If OpenBB requires specific logging configurations, it can provide an explicit initialization function that the host application can call. This allows the host application to decide when and how to configure the library's logging, and if other code depends on the current behavior it can restore that by calling these functions.
  • Allow for User Configuration: OpenBB can offer configuration options or settings that allow users to customize the library's logging behavior without affecting the global logging settings. Just giving us a way to disable this for now would be a big improvement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Fix bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants