-
Notifications
You must be signed in to change notification settings - Fork 97
feat: add automatic logging config to support debug logging #754
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
7b1605c
feat: add base logger to enable debug logging
ohmayr 48b9ed9
address PR feedback
ohmayr ef66ac5
address PR feedback
ohmayr a42c5ac
add tests
ohmayr ff7792b
address PR feedback
ohmayr 65708ff
address PR feedback
ohmayr 0f1a4bd
address PR feedback
ohmayr b5088af
address PR feedback
ohmayr 09283ec
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 89c15b5
address PR feedback
ohmayr 3ffe3e3
Merge branch 'debug-logging' of github.com:googleapis/python-api-core…
ohmayr f782b63
address PR feedback
ohmayr 601b653
address PR feedback
ohmayr 69bbd05
add coverage for already configured scope
ohmayr b6b354f
Add partial documentation; clarify test base logger
vchudnov-g 8514389
Merge branch 'main' into debug-logging
parthea a157d8c
address PR feedback
ohmayr a15f59d
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import logging | ||
| import json | ||
| import re | ||
| import os | ||
|
|
||
| LOGGING_INITIALIZED = False | ||
|
|
||
| # TODO(<add-link>): Update Request / Response messages. | ||
| REQUEST_MESSAGE = "Sending request ..." | ||
| RESPONSE_MESSAGE = "Receiving response ..." | ||
ohmayr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| # TODO(<add-link>): Update this list to support additional logging fields | ||
| _recognized_logging_fields = ["httpRequest", "rpcName", "serviceName"] # Additional fields to be Logged. | ||
|
|
||
| def logger_configured(logger): | ||
ohmayr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return logger.hasHandlers() or logger.level != logging.NOTSET | ||
ohmayr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| def initialize_logging(): | ||
ohmayr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| global LOGGING_INITIALIZED | ||
| if LOGGING_INITIALIZED: | ||
| return | ||
| scopes = os.getenv("GOOGLE_SDK_PYTHON_LOGGING_SCOPE") | ||
| setup_logging(scopes) | ||
| LOGGING_INITIALIZED = True | ||
|
|
||
| def parse_logging_scopes(scopes): | ||
ohmayr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if not scopes: | ||
| return [] | ||
| # TODO(<add-link>): check if the namespace is a valid namespace. | ||
ohmayr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # TODO(<add-link>): parse a list of namespaces. Current flow expects a single string for now. | ||
| namespaces = [scopes] | ||
| return namespaces | ||
|
|
||
| def default_settings(logger): | ||
ohmayr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if not logger_configured(logger): | ||
| console_handler = logging.StreamHandler() | ||
| logger.setLevel("DEBUG") | ||
| logger.propagate = False | ||
| formatter = StructuredLogFormatter() | ||
| console_handler.setFormatter(formatter) | ||
| logger.addHandler(console_handler) | ||
|
|
||
| def setup_logging(scopes): | ||
| # disable log propagation at base logger level to the root logger only if a base logger is not already configured via code changes. | ||
| base_logger = logging.getLogger("google") | ||
| if not logger_configured(base_logger): | ||
| base_logger.propagate = False | ||
|
|
||
| # only returns valid logger scopes (namespaces) | ||
| # this list has at most one element. | ||
| loggers = parse_logging_scopes(scopes) | ||
|
|
||
| for namespace in loggers: | ||
| # This will either create a module level logger or get the reference of the base logger instantiated above. | ||
| logger = logging.getLogger(namespace) | ||
|
|
||
| # Set default settings. | ||
| default_settings(logger) | ||
|
|
||
| class StructuredLogFormatter(logging.Formatter): | ||
| def format(self, record): | ||
ohmayr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| log_obj = { | ||
ohmayr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 'timestamp': self.formatTime(record), | ||
| 'severity': record.levelname, | ||
| 'name': record.name, | ||
| 'message': record.getMessage(), | ||
| } | ||
|
|
||
| for field_name in _recognized_logging_fields: | ||
| value = getattr(record, field_name, None) | ||
| if value is not None: | ||
| log_obj[field_name] = value | ||
| return json.dumps(log_obj) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import logging | ||
| import pytest | ||
|
|
||
| from google.api_core.client_logging import BaseLogger | ||
ohmayr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
ohmayr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| def test_base_logger(caplog): | ||
|
|
||
| logger = BaseLogger().get_logger() | ||
|
|
||
| with caplog.at_level(logging.INFO, logger="google"): | ||
| logger.info("This is a test message.") | ||
|
|
||
| assert "This is a test message." in caplog.text | ||
| assert caplog.records[0].name == "google" | ||
| assert caplog.records[0].levelname == "INFO" | ||
| assert caplog.records[0].message == "This is a test message." | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.