Skip to content
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

Migrate print calls to logging #1083

Merged
merged 5 commits into from
Nov 12, 2024
Merged

Conversation

wwwillchen
Copy link
Collaborator

@wwwillchen wwwillchen commented Nov 2, 2024

This conforms to more standard Python practices and enables apps that use Mesop to customize the logs generated by Mesop (e.g. ignore info logs).

This moves the print statements from env.py into server.py because logging calls need to happen after the main function is called (see: abseil/abseil-py#194 (comment))

@wwwillchen wwwillchen marked this pull request as draft November 2, 2024 06:27
@wwwillchen wwwillchen marked this pull request as ready for review November 9, 2024 05:31
Copy link
Collaborator

@richard-to richard-to left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. I definitely agree using logging is better than the print statements.

There are a few cases where we still uses print statements. I'm curious when it's important to keep the print statement versus using the logger. For example we have mesop/warn.py. Should that just become a logger.warning. Or are those cases special enough to warrant a specific colored print message. But then we also have a case of the CSP error message, which I think makes sense to really format and highlight that to the user.

mesop/env/env.py Outdated

EXPERIMENTAL_EDITOR_TOOLBAR_ENABLED = (
os.environ.get("MESOP_EXPERIMENTAL_EDITOR_TOOLBAR", "false").lower() == "true"
)

if EXPERIMENTAL_EDITOR_TOOLBAR_ENABLED:
print("Experiment enabled: EXPERIMENTAL_EDITOR_TOOLBAR_ENABLED")
logging.info("Experiment enabled: EXPERIMENTAL_EDITOR_TOOLBAR_ENABLED")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this one is in server.py already. And I think you mentioned this wouldn't show up.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops - you're right - forgot to remove this - done.

@wwwillchen
Copy link
Collaborator Author

Nice. I definitely agree using logging is better than the print statements.

There are a few cases where we still uses print statements. I'm curious when it's important to keep the print statement versus using the logger. For example we have mesop/warn.py. Should that just become a logger.warning. Or are those cases special enough to warrant a specific colored print message. But then we also have a case of the CSP error message, which I think makes sense to really format and highlight that to the user.

good call on warn.py - I migrated that to logger.warning, but kept the color formatting so it still stands out in the terminal. For CSP, yeah, I think keeping it as a print is better due to the formatting.

tbh, this is an area I've never been super sure about because Mesop as a CLI, print is better vs. Mesop as a library, logging is better.

@wwwillchen wwwillchen merged commit 38edea8 into google:main Nov 12, 2024
6 checks passed
@wwwillchen wwwillchen deleted the print-to-logging branch November 12, 2024 19:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants