-
Notifications
You must be signed in to change notification settings - Fork 9
Issue 68 logging #73
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
Open
kan-fu
wants to merge
4
commits into
main
Choose a base branch
from
issue-68-logging
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Issue 68 logging #73
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6db8622
Add functionality for more descriptive console messages and returning…
IanTBlack 36401d7
refactor: replace direct console prints with structured logging acros…
kan-fu 4f878ef
refactor: standardize test error handling by injecting err_400 and er…
kan-fu 19f99da
refactor: apply style format and lint, use numpy docstring style
kan-fu 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,86 @@ | ||
| import logging | ||
| import re | ||
| import requests | ||
| import time | ||
|
|
||
| REQ_MSG = "Requested: {}" # get request url | ||
| RESPONSE_TIME_MSG = "Response received in {} seconds." # requests.elapsed value. | ||
| RESPONSE_MSG = "HTTP Response: {} ({})" # Brief description, status code | ||
| MULTIPAGE_MSG = ("The requested data quantity is greater than the " | ||
| "supplied row limit and will be downloaded over multiple requests.") | ||
|
|
||
|
|
||
| def setup_logger(logger_name: str = 'onc-client', | ||
| level: int | str = 'DEBUG') -> logging.Logger: | ||
| """ | ||
| Set up a logger object for displaying verbose messages to console. | ||
|
|
||
| :param logger_name: The unique logger name to use. Can be shared between modules | ||
| :param level: The logging level to use. Default is 2, which corresponds to DEBUG. | ||
| :return: The configured logging.Logger object. | ||
| """ | ||
|
|
||
| logger = logging.getLogger(logger_name) | ||
| logger.propagate = False | ||
| if not logger.handlers: | ||
| logger.setLevel(logging.DEBUG) | ||
| console = logging.StreamHandler() | ||
| console.setLevel(level) | ||
|
|
||
| # Set the logging format. | ||
| dtfmt = '%Y-%m-%dT%H:%M:%S' | ||
| strfmt = f'%(asctime)s.%(msecs)03dZ | %(name)-12s | %(levelname)-8s | %(message)s' | ||
| #strfmt = f'%(asctime)s.%(msecs)03dZ | %(levelname)-8s | %(message)s' # Use this if you don't want to include logger name. | ||
| fmt = logging.Formatter(strfmt, datefmt=dtfmt) | ||
| fmt.converter = time.gmtime | ||
|
|
||
| console.setFormatter(fmt) | ||
| logger.addHandler(console) | ||
| return logger | ||
|
|
||
|
|
||
| def scrub_token(input: str) -> str: | ||
| """ | ||
| Replace a token in a query URL or other string with the string 'REDACTED' | ||
| so that users don't accidentally commit their tokens to public repositories | ||
| if ONC Info/Warnings are too verbose. | ||
|
|
||
| :param query_url: An Oceans 3.0 API URL or string with a token query parameter. | ||
| :return: A scrubbed url. | ||
| """ | ||
| token_regex = r'(&token=[a-f0-9-]{36})' | ||
| token_qp = re.findall(token_regex, input)[0] | ||
| redacted_url = input.replace(token_qp, '&token=REDACTED') | ||
| return redacted_url | ||
|
|
||
|
|
||
| def build_error_message(response: requests.Response, redact_token: bool) -> str: | ||
| """ | ||
| Build an error message from a requests.Response object. | ||
|
|
||
| :param response: A requests.Response object. | ||
| :param redact_token: If true, redact tokens before returning an error message. | ||
| :return: An error message. | ||
| """ | ||
| payload = response.json() | ||
| if 'message' in payload.keys(): | ||
| message = payload['message'] | ||
| else: | ||
| message = None | ||
|
|
||
| if 'errors' in payload.keys(): | ||
| errors = payload['errors'] | ||
| error_messages = [] | ||
| for error in errors: | ||
| emsg = (f"(API Error Code {error['errorCode']}) " | ||
| f"{error['errorMessage']} for query parameter(s) " | ||
| f"'{error['parameter']}'.") | ||
| error_messages.append(emsg) | ||
| error_message = '\n'.join(error_messages) | ||
| else: | ||
| error_message = None | ||
| msg = '\n'.join([m for m in (message, error_message) if m is not None]) | ||
| if redact_token is True and 'token=' in msg: | ||
| msg = scrub_token(msg) | ||
| return msg | ||
|
|
||
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
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
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
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
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
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
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.
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.
Doc string should be updated to reflect logging numeric values.
https://docs.python.org/3/library/logging.html#logging-levels
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.
Nevermind. I see that this is outdated.