-
Notifications
You must be signed in to change notification settings - Fork 12
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
Use Python logging system for emitting log messages when running via the Python API #159
Open
hoechenberger
wants to merge
15
commits into
main
Choose a base branch
from
hoechenberger/issue141
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
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
59e3eca
Use Python logging system for emitting log messages when called from …
hoechenberger 3bfd5ee
Refactor
hoechenberger e758498
Fix
hoechenberger 0a1f9d1
Forgot one
hoechenberger 73bd58e
Better
hoechenberger 4244427
Merge branch 'main' into hoechenberger/issue141
hoechenberger c700a04
Add `cli_only` kwarg
hoechenberger 3b16bbc
Make pytest print logging output
hoechenberger a7e9436
Simplify logging
hoechenberger d9b9e57
Add retry emoji
hoechenberger ae9d108
Add CLI download test
hoechenberger eb66ec2
Merge branch 'main' into hoechenberger/issue141
hoechenberger 252a24a
Merge branch 'main' into hoechenberger/issue141
hoechenberger a1af5c6
Merge branch 'main' into hoechenberger/issue141
larsoner fe44369
Merge branch 'main' into hoechenberger/issue141
hoechenberger 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 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 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 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 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 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 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,64 @@ | ||
import logging | ||
import sys | ||
|
||
from tqdm.auto import tqdm | ||
|
||
logging.basicConfig(format="%(message)s", level=logging.INFO) | ||
logger = logging.getLogger("openneuro-py") | ||
|
||
|
||
if hasattr(sys.stdout, "encoding") and sys.stdout.encoding.lower() == "utf-8": | ||
stdout_unicode = True | ||
elif hasattr(sys.stdout, "reconfigure"): | ||
sys.stdout.reconfigure(encoding="utf-8") | ||
stdout_unicode = True | ||
else: | ||
stdout_unicode = False | ||
|
||
|
||
def log( | ||
message: str, | ||
emoji: str | None = None, | ||
end: str | None = None, | ||
cli_only: bool = False, | ||
) -> None: | ||
"""Emit a log message. | ||
|
||
Parameters | ||
---------- | ||
message | ||
The message to emit. | ||
emoji | ||
Unicode eomji to prepend. | ||
end | ||
String to append. By default, `"…"`. | ||
cli_only | ||
Whether to emit the message only when running from the CLI. If `False`, the | ||
message will shop up when running from the CLI and the Python API. | ||
|
||
""" | ||
from openneuro import _RUNNING_FROM_CLI # avoid circular import | ||
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. Rather than this it seems a tiny bit cleaner to have a global var in this file. Then here you don't need any import and above... |
||
|
||
if cli_only and not _RUNNING_FROM_CLI: | ||
return | ||
|
||
if emoji is None: | ||
emoji = " " | ||
if end is None: | ||
end = "…" | ||
|
||
message_unicode = _unicode(message, emoji=emoji, end=end) | ||
del message | ||
|
||
if _RUNNING_FROM_CLI: | ||
logger.log(level=logging.INFO, msg=message_unicode) | ||
else: | ||
tqdm.write(message_unicode) | ||
|
||
|
||
def _unicode(msg: str, *, emoji: str = " ", end: str = "…") -> str: | ||
if stdout_unicode: | ||
msg = f"{emoji} {msg} {end}" | ||
elif end == "…": | ||
msg = f"{msg} ..." | ||
return msg |
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.
You can have this:
(along with an
import openneuro._logging
at the top)