Skip to content

Azure: add the ability to set log level for any loggers in the EventHub connector#2027

Open
squioc wants to merge 3 commits intodevelopfrom
chore/AzureEventHubLoggingLevel
Open

Azure: add the ability to set log level for any loggers in the EventHub connector#2027
squioc wants to merge 3 commits intodevelopfrom
chore/AzureEventHubLoggingLevel

Conversation

@squioc
Copy link
Collaborator

@squioc squioc commented Feb 13, 2026

Add the ability to set log level for any loggers in the Azure EventHub connector with some environment variables.

Summary by Sourcery

Allow configuring Azure EventHub connector logging levels via environment variables and bump the Azure integration version.

New Features:

  • Support configuring log levels for selected or all loggers in the Azure EventHub connector using environment variables.

Enhancements:

  • Introduce a shared logging helper to apply log levels from environment variables across specified loggers.

Build:

  • Bump the Azure integration manifest version to 2.9.3.

Documentation:

  • Document the new logging configuration capability in the Azure changelog.

@squioc squioc requested review from a team and Copilot February 13, 2026 09:19
@squioc squioc added the enhancement New feature or request label Feb 13, 2026
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Feb 13, 2026

Reviewer's Guide

Adds configurable logging support to the Azure EventHub connector via environment variables and bumps the Azure integration version to 2.9.3 with corresponding changelog and manifest updates.

Class diagram for Azure EventHub connector logging utilities

classDiagram
    class AzureEventsHubConfiguration {
    }

    class LoggingModule {
        +set_log_level(loggers, log_level) void
        +set_log_level_from_env() void
    }

    class AzureEventHubConnectorModule {
        +set_log_level_from_env_on_import() void
    }

    AzureEventHubConnectorModule ..> LoggingModule : uses
    LoggingModule ..> AzureEventsHubConfiguration : used_with
Loading

Flow diagram for Azure EventHub connector log level initialization from environment

flowchart TD
    A[Connector module import] --> B[Call set_log_level_from_env]
    B --> C{AZURE_LOG_LEVEL set?}
    C -- No --> D[Return without changes]
    C -- Yes --> E[Initialize loggers as None]
    E --> F{AZURE_LOGGERS set?}
    F -- Yes --> G[Parse AZURE_LOGGERS into set of names]
    F -- No --> H[Use all loggers from logging.root.manager.loggerDict keys]
    G --> I[Call set_log_level]
    H --> I[Call set_log_level]
    I --> J[Iterate over each logger name]
    J --> K[Get logger by name and set level to AZURE_LOG_LEVEL]
    K --> L[Connector continues with configured logging]
Loading

File-Level Changes

Change Details Files
Initialize EventHub connector logging configuration from environment variables.
  • Import a new helper function to configure logging from environment variables in the EventHub connector module.
  • Invoke the logging configuration helper at module import time to apply log levels before the connector runs.
Azure/connectors/azure_eventhub.py
Introduce reusable utilities to set logger levels from environment variables for the Azure integration.
  • Add a helper to set the log level for a given set of logger names.
  • Add logic to read AZURE_LOG_LEVEL and AZURE_LOGGERS environment variables, derive the target loggers (or all known loggers when none specified), and apply the configured log level.
Azure/connectors/logging.py
Update Azure integration metadata for the new logging feature.
  • Add a 2.9.3 entry to the changelog describing the new logging capability.
  • Bump the integration version from 2.9.2 to 2.9.3 and compact the categories array formatting in the manifest.
Azure/CHANGELOG.md
Azure/manifest.json

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - I've found 1 issue, and left some high level feedback:

  • The set_log_level_from_env helper assumes AZURE_LOG_LEVEL is a valid integer and will raise if it isn't; consider validating the value (e.g., supporting symbolic levels like DEBUG/INFO) and failing gracefully instead of throwing at import time.
  • Calling set_log_level_from_env() at module import time makes logger configuration a side effect of importing azure_eventhub; consider moving this into an explicit initialization path so that import order and partial environments don't unexpectedly affect logging.
  • When parsing AZURE_LOGGERS, a trailing comma or extra commas will produce empty logger names that end up configuring the root logger; consider filtering out empty names before passing them to set_log_level.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The `set_log_level_from_env` helper assumes `AZURE_LOG_LEVEL` is a valid integer and will raise if it isn't; consider validating the value (e.g., supporting symbolic levels like `DEBUG`/`INFO`) and failing gracefully instead of throwing at import time.
- Calling `set_log_level_from_env()` at module import time makes logger configuration a side effect of importing `azure_eventhub`; consider moving this into an explicit initialization path so that import order and partial environments don't unexpectedly affect logging.
- When parsing `AZURE_LOGGERS`, a trailing comma or extra commas will produce empty logger names that end up configuring the root logger; consider filtering out empty names before passing them to `set_log_level`.

## Individual Comments

### Comment 1
<location> `Azure/connectors/logging.py:38` </location>
<code_context>
+        loggers = set(logging.root.manager.loggerDict.keys())
+
+    # Set the log level for the specified loggers.
+    set_log_level(loggers, int(os.environ.get("AZURE_LOG_LEVEL")))
</code_context>

<issue_to_address>
**issue:** Guard against invalid AZURE_LOG_LEVEL values to avoid crashing on import.

Calling `int(os.environ.get("AZURE_LOG_LEVEL"))` will raise `ValueError` if the env var is set to a non-integer (including empty string), which will break import since this runs at import time. Wrap this in a `try`/`except` for `ValueError` (and possibly `TypeError`) and fall back to a default log level or ignore the override when parsing fails.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

loggers = set(logging.root.manager.loggerDict.keys())

# Set the log level for the specified loggers.
set_log_level(loggers, int(os.environ.get("AZURE_LOG_LEVEL")))
Copy link
Contributor

Choose a reason for hiding this comment

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

issue: Guard against invalid AZURE_LOG_LEVEL values to avoid crashing on import.

Calling int(os.environ.get("AZURE_LOG_LEVEL")) will raise ValueError if the env var is set to a non-integer (including empty string), which will break import since this runs at import time. Wrap this in a try/except for ValueError (and possibly TypeError) and fall back to a default log level or ignore the override when parsing fails.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds an environment-variable-based mechanism to control Python logger levels for the Azure EventHub connector, and bumps the Azure module version accordingly.

Changes:

  • Bump Azure module version to 2.9.3 and add a changelog entry.
  • Add Azure/connectors/logging.py helper to set logger levels from AZURE_LOG_LEVEL / AZURE_LOGGERS.
  • Invoke the env-based log-level initialization from the EventHub connector module.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

File Description
Azure/manifest.json Bumps module version to 2.9.3 (and minor JSON formatting).
Azure/connectors/logging.py Introduces env-driven logger level configuration helpers.
Azure/connectors/azure_eventhub.py Calls the new env-driven log-level initializer.
Azure/CHANGELOG.md Documents release 2.9.3 and the added logging configurability.

Comment on lines +27 to +34
loggers = None

# If AZURE_LOGGERS is set, use the specified loggers. Otherwise, use all loggers.
if os.environ.get("AZURE_LOGGERS") is not None:
loggers = set(os.environ.get("AZURE_LOGGERS").split(","))

# If AZURE_LOGGERS is not set, use all loggers.
if loggers is None:
Copy link

Copilot AI Feb 13, 2026

Choose a reason for hiding this comment

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

loggers = None is later reassigned to a set, which is a type inconsistency and will trip strict type-checking; annotate it as set[str] | None (or avoid the sentinel by initializing to an empty set). Also, when AZURE_LOGGERS is an empty string, split(",") yields "" and ends up modifying the root logger unintentionally—filter out empty names.

Suggested change
loggers = None
# If AZURE_LOGGERS is set, use the specified loggers. Otherwise, use all loggers.
if os.environ.get("AZURE_LOGGERS") is not None:
loggers = set(os.environ.get("AZURE_LOGGERS").split(","))
# If AZURE_LOGGERS is not set, use all loggers.
if loggers is None:
loggers_env = os.environ.get("AZURE_LOGGERS")
# If AZURE_LOGGERS is set and non-empty, use the specified loggers (excluding empty names).
if loggers_env:
loggers: set[str] = {name.strip() for name in loggers_env.split(",") if name.strip()}
else:
# If AZURE_LOGGERS is not set or empty, use all loggers.

Copilot uses AI. Check for mistakes.
Comment on lines +17 to +22
from .logging import set_log_level_from_env
from .metrics import EVENTS_LAG, FORWARD_EVENTS_DURATION, INCOMING_MESSAGES, MESSAGES_AGE, OUTCOMING_EVENTS

# Initialize log level from environment variables
set_log_level_from_env()

Copy link

Copilot AI Feb 13, 2026

Choose a reason for hiding this comment

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

Calling set_log_level_from_env() at module import introduces a global side effect and can make simply importing this module fail depending on environment variables. Prefer invoking this during connector startup (e.g., in AzureEventsHubTrigger.__init__/run) after logging is configured, so behavior is deterministic and easier to test.

Copilot uses AI. Check for mistakes.
Comment on lines +20 to +22
# Initialize log level from environment variables
set_log_level_from_env()

Copy link

Copilot AI Feb 13, 2026

Choose a reason for hiding this comment

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

This new env-driven logging behavior isn’t covered by tests. Since Azure/tests/connector/test_azure_eventhub.py already exists, add unit tests that verify: (1) valid values change the expected logger levels, (2) AZURE_LOGGERS selection works, and (3) invalid AZURE_LOG_LEVEL values don’t crash startup.

Copilot uses AI. Check for mistakes.
loggers = set(logging.root.manager.loggerDict.keys())

# Set the log level for the specified loggers.
set_log_level(loggers, int(os.environ.get("AZURE_LOG_LEVEL")))
Copy link

Copilot AI Feb 13, 2026

Choose a reason for hiding this comment

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

int(os.environ.get("AZURE_LOG_LEVEL")) will raise ValueError for common inputs like "INFO"/"DEBUG" and will crash the connector (currently at import time). Consider accepting standard level names (e.g., via logging.getLevelName/logging._nameToLevel) and handling invalid values gracefully (fallback or ignore) instead of throwing.

Copilot uses AI. Check for mistakes.
Copy link
Contributor

@mchupeau-sk mchupeau-sk left a comment

Choose a reason for hiding this comment

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

Maybe apply copilot suggestion ? LGFM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants