Skip to content

ENH: Add python loggers#973

Open
YuriCastroDev wants to merge 5 commits into
RocketPy-Team:developfrom
YuriCastroDev:enh/issue-450-adopt-python-loggers
Open

ENH: Add python loggers#973
YuriCastroDev wants to merge 5 commits into
RocketPy-Team:developfrom
YuriCastroDev:enh/issue-450-adopt-python-loggers

Conversation

@YuriCastroDev

Copy link
Copy Markdown

Pull request type

  • Code changes (bugfix, features)
  • Code maintenance (refactoring, formatting, tests)

Checklist

  • Tests for the changes have been added (if needed)
  • Docs have been reviewed and added / updated
  • Lint (black rocketpy/ tests/) has passed locally
  • All tests (pytest tests -m slow --runslow) have passed locally
  • CHANGELOG.md has been updated (if relevant)

Current behavior

RocketPy uses bare print() calls scattered across 43 files to report
simulation progress, warnings, and errors. This approach has no severity
levels, cannot be redirected to a file, and cannot be silenced or filtered
by the end user. Closes #450.

New behavior

All print() calls have been replaced with Python's built-in logging
module following best practices for libraries:

  • rocketpy/__init__.py registers a NullHandler, so the library is
    silent by default with no output unless the user configures a handler.
  • Every module now exposes a logger = logging.getLogger(__name__),
    creating a hierarchy (rocketpy.simulation.flight,
    rocketpy.environment.environment, etc.) that users can filter
    individually.
  • Log levels are assigned according to severity:
    • DEBUG for high-frequency solver ticks and internal detail
    • INFO for normal operation confirmations (simulation completed, files
      saved, .info() output from all print classes)
    • WARNING for unexpected but recoverable situations (missing motor,
      geometry auto-corrected, feature silently skipped)
    • ERROR for optional dependency unavailable or feature broken
  • No handlers are defined inside the library. Configuration is left entirely
    to the user.
  • _SimMonitor.reprint() in MonteCarlo is kept for backwards
    compatibility and now delegates to logger.info().

Users can now direct logs to a file with a single call before running a
simulation:

import logging
logging.basicConfig(
    filename="simulation.log",
    level=logging.INFO,
    format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
)

Note on verbose parameter: The verbose=True flag on Flight retains
its original terminal behavior. It still prints the real-time progress
ticker using \r line overwrite. Additionally, the same information is now
also emitted via logger.debug(), so users who configure a logging handler
will capture it as well. Full backwards compatibility is preserved.

Breaking change

  • No

Additional information

  • 43 files modified, approximately 460 print() calls replaced.

@YuriCastroDev YuriCastroDev requested a review from a team as a code owner June 19, 2026 21:01

@Gui-FernandesBR Gui-FernandesBR left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

does it change current behavior?

If I open the getting start notebook, does it work just like as it should?

@YuriCastroDev

Copy link
Copy Markdown
Author

@Gui-FernandesBR yes, it does change current behavior. The getting started notebook would not work the same because .info() / .all_info() calls are now silent due to the nullhandler. To get the same output, the user would need to configure a handler before running simulation.

@Gui-FernandesBR

Copy link
Copy Markdown
Member

@Gui-FernandesBR yes, it does change current behavior. The getting started notebook would not work the same because .info() / .all_info() calls are now silent due to the nullhandler. To get the same output, the user would need to configure a handler before running simulation.

yes that's my concern... Is there a way where we keep the same behavior for current code, but also benefit from built-in loggers without duplicating code? thta's the real challenge

@YuriCastroDev

Copy link
Copy Markdown
Author

Hm... makes sense. Keeping print() for explicitly user-requested output
like flight.info() and reserving logger for internal runtime events
(simulation completed, file saved, motor overwritten, etc.) follows
the Python logging guide's own distinction.

What do u think?

@Gui-FernandesBR

Copy link
Copy Markdown
Member

Hm... makes sense. Keeping print() for explicitly user-requested output like flight.info() and reserving logger for internal runtime events (simulation completed, file saved, motor overwritten, etc.) follows the Python logging guide's own distinction.

What do u think?

yes but that would imply in us duplicating code verytime we want to print a message, right?

I wonder how other simulator libraries deal with the same situation.

If you could bring us some references so we can base our opinion on different examples... I'd appreciate it a lot.

@YuriCastroDev

Copy link
Copy Markdown
Author

I'll look into other libraries for references, once I have the answer I'll come back, tks for review

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