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

[Proposal] Ensure a console logging handler is set when using auto-instrumentation #4436

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

pmcollins
Copy link
Member

@pmcollins pmcollins commented Feb 17, 2025

Description

If a user sets up auto-instrumentation and sets OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED to true, any calls to logging.basicConfig() will be no-ops. This means that if console logging was previously enabled via basicConfig and logs were printed to the console, after turning on logging auto-instrumentation, logs will be exported to OTel and will no longer be printed to the console.

This proposal is: if under auto-instrumentation, the OTel SDK will explicitly set up a console/stream logger by reading a new environment variable, OTEL_PYTHON_LOG_FORMAT. If the user requests logging auto-instrumentation, the SDK will read the environment variable: if it's set and not empty, the SDK will use that as a custom format; if explicitly empty, the SDK will skip setting up a console logger altogether. This could give users a smoother experience when setting up logging auto instrumentation as their logs will appear on the console by default under auto-instrumentation, but they'll still have control over the format, including the ability to turn console logging off.

A possible drawback is if a user does not have console logging turned on and they turn on logging auto-instrumentation, they will start getting logs printed to the console when they didn't have any before. They would have to turn it off explicitly by setting OTEL_PYTHON_LOG_FORMAT="".

Addresses #4427

I have tested this manually but haven't added unit tests to this PR yet -- looking to get feedback on the general approach first.

level=logging.NOTSET, logger_provider=provider
logger = logging.getLogger()
logger.addHandler(
LoggingHandler(level=logging.NOTSET, logger_provider=provider)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might want to make the level configurable as well.

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

Successfully merging this pull request may close these issues.

1 participant