-
Notifications
You must be signed in to change notification settings - Fork 39
Azure: add the ability to set log level for any loggers in the EventHub connector #2027
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,8 +14,12 @@ | |
| from sekoia_automation.aio.connector import AsyncConnector | ||
| from sekoia_automation.connector import Connector, DefaultConnectorConfiguration | ||
|
|
||
| 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() | ||
|
|
||
|
Comment on lines
+20
to
+22
|
||
|
|
||
| class AzureEventsHubConfiguration(DefaultConnectorConfiguration): | ||
| hub_connection_string: str | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,38 @@ | ||||||||||||||||||||||||||||||||
| import logging | ||||||||||||||||||||||||||||||||
| import os | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| def set_log_level(loggers: set[str], log_level: int) -> None: | ||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||
| Set the log level for the specified loggers. | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| :param loggers: A set of logger names to set the log level for. | ||||||||||||||||||||||||||||||||
| :param log_level: The log level to set for the specified loggers. | ||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||
| for logger in loggers: | ||||||||||||||||||||||||||||||||
| logging.getLogger(logger.strip()).setLevel(log_level) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| def set_log_level_from_env() -> None: | ||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||
| Set the log level for the specified loggers based on the AZURE_LOG_LEVEL and AZURE_LOGGERS environment variables. | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| AZURE_LOG_LEVEL should be set to the desired log level (e.g., 10 for DEBUG, 20 for INFO, etc.). | ||||||||||||||||||||||||||||||||
| AZURE_LOGGERS should be a comma-separated list of logger names to set the log level for. If AZURE_LOGGERS is not set, the log level will be set for all loggers. | ||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||
| # If AZURE_LOG_LEVEL is not set, do not change the log level for any loggers. | ||||||||||||||||||||||||||||||||
| if os.environ.get("AZURE_LOG_LEVEL") is None: | ||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| 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["AZURE_LOGGERS"].split(",")) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| # If AZURE_LOGGERS is not set, use all loggers. | ||||||||||||||||||||||||||||||||
| if loggers is None: | ||||||||||||||||||||||||||||||||
|
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: | |
| 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. |
There was a problem hiding this comment.
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., inAzureEventsHubTrigger.__init__/run) after logging is configured, so behavior is deterministic and easier to test.