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

Importing google.cloud.aiplatform has side-effects and misconfigures logging #3667

Open
tgolsson opened this issue Apr 24, 2024 · 0 comments
Labels
api: vertex-ai Issues related to the googleapis/python-aiplatform API.

Comments

@tgolsson
Copy link

tgolsson commented Apr 24, 2024

Environment details

  • OS type and version: WSL/Ubuntu 20.04
  • Python version: python3.11
  • pip version: N/A
  • google-cloud-aiplatform version: 1.47.0

Steps to reproduce

  1. Import aiplatform into any code before initializing logging
  2. Note that there are either incorrectly configured logs or duplicate logs

Code example

# This creates a logging.StreamHandler and adds it, forcing output. Note that I'm normally *not* importing this, but rather just `aiplatform.init` for example. The logger is created as soon as the file is imported.
from google.cloud.aiplatform.base import _LOGGER

import logging
import sys

logger = logging.getLogger(__file__)
logging.basicConfig(level=logging.INFO, handlers=[logging.StreamHandler(sys.stdout)])

# ok
logger.info("foobar")
# duplicate
_LOGGER.info("I'm duplicated")

Gives the output:

INFO:/home/ts/Repositories/spoof-vai/example.py:foobar
I'm duplicated
INFO:google.cloud.aiplatform.base:I'm duplicated

This means that any actual diagnostic output from the functions on VertexLogger appears twice in my output. A library should never create a logging handler, and should trust the end user to properly configure logging if desired. And if it HAD to create a logging handler, it shouldn't do so by side-effecting on an import.

For anyone finding this in search, the workaround I've found to prevent this handler creation is to be faster, by ensuring all my entrypoints look something like this:

import logging
import sys

root = logging.getLogger()
handler = logging.StreamHandler(sys.stdout)
root.addHandler(handler)

import aiplatform

... 

def main():
     logging.basicConfig(..., force=True) # or whatever logging you want to use...
@product-auto-label product-auto-label bot added the api: vertex-ai Issues related to the googleapis/python-aiplatform API. label Apr 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: vertex-ai Issues related to the googleapis/python-aiplatform API.
Projects
None yet
Development

No branches or pull requests

1 participant