-
Notifications
You must be signed in to change notification settings - Fork 38
Set log level from config or cli args #528
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
Changes from 2 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 | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -54,6 +54,7 @@ def _do_main(cli_args: list[str]): | |||||||||
| parser = setup_parser(cli_args) | ||||||||||
| argcomplete.autocomplete(parser) | ||||||||||
|
|
||||||||||
| _handle_log_level_argument(cli_args) | ||||||||||
| _handle_verbose_argument(cli_args) | ||||||||||
| args = parser.parse_args(cli_args) | ||||||||||
|
|
||||||||||
|
|
@@ -65,8 +66,9 @@ def _do_main(cli_args: list[str]): | |||||||||
| handlers=[RichHandler(show_time=False, rich_tracebacks=True)], | ||||||||||
| ) | ||||||||||
| else: | ||||||||||
| level: str = args.log_level | ||||||||||
| logging.basicConfig( | ||||||||||
| level="INFO", | ||||||||||
| level=level.upper(), | ||||||||||
| format="%(name)s: %(message)s", | ||||||||||
| handlers=[RichHandler(show_time=False, rich_tracebacks=True)], | ||||||||||
| ) | ||||||||||
|
|
@@ -112,6 +114,13 @@ def setup_parser(args: list[str]): | |||||||||
| version=f"MultiversX Python CLI (mxpy) {version}", | ||||||||||
| ) | ||||||||||
| parser.add_argument("--verbose", action="store_true", default=False) | ||||||||||
| parser.add_argument( | ||||||||||
| "--log-level", | ||||||||||
| type=str, | ||||||||||
| default=config.get_log_level_from_config(), | ||||||||||
| choices=["debug", "info", "warning", "error"], | ||||||||||
| help="default: %(default)s", | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| subparsers = parser.add_subparsers() | ||||||||||
| commands: list[Any] = [] | ||||||||||
|
|
@@ -166,6 +175,17 @@ def _handle_verbose_argument(args: list[str]): | |||||||||
| args.insert(0, verbose_arg) | ||||||||||
|
|
||||||||||
|
|
||||||||||
| def _handle_log_level_argument(args: list[str]): | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could have handled both
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done, moved the logic in |
||||||||||
| log_level_arg = "--log-level" | ||||||||||
|
|
||||||||||
| if log_level_arg in args: | ||||||||||
| index = args.index(log_level_arg) | ||||||||||
|
||||||||||
| index = args.index(log_level_arg) | |
| index = args.index(log_level_arg) | |
| if index + 1 >= len(args) or args[index + 1].startswith("--"): | |
| raise ValueError(f"Argument '{log_level_arg}' must be followed by a log level value.") |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -136,9 +136,18 @@ def get_defaults() -> dict[str, Any]: | |
| "dependencies.testwallets.urlTemplate.osx": "https://github.com/multiversx/mx-sdk-testwallets/archive/{TAG}.tar.gz", | ||
| "dependencies.testwallets.urlTemplate.windows": "https://github.com/multiversx/mx-sdk-testwallets/archive/{TAG}.tar.gz", | ||
| "github_api_token": "", | ||
| "log_level": "info", | ||
| } | ||
|
|
||
|
|
||
| def get_log_level_from_config(): | ||
| log_level = get_value("log_level") | ||
| if log_level not in ["debug", "info", "warning", "error"]: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. List could have been in
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
| raise errors.LogLevelError(log_level) | ||
|
|
||
| return log_level | ||
|
|
||
|
|
||
| def get_deprecated_entries_in_config_file(): | ||
| default_config_keys = set(get_defaults().keys()) | ||
| current_config_keys = set(get_active().keys()) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -221,3 +221,8 @@ def __init__(self, message: str): | |
| class NetworkProviderError(KnownError): | ||
| def __init__(self, url: str, error: str): | ||
| super().__init__(f"Url = [{url}], error = {error}") | ||
|
|
||
|
|
||
| class LogLevelError(KnownError): | ||
| def __init__(self, log_level: str): | ||
| super().__init__(f"Log level not accepted: {log_level}. Choose between ['debug', 'info', 'warning', 'error'].") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. List could have been in
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
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.
👍