ENH: Add python loggers#973
Conversation
Gui-FernandesBR
left a comment
There was a problem hiding this comment.
does it change current behavior?
If I open the getting start notebook, does it work just like as it should?
|
@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 |
|
Hm... makes sense. Keeping print() for explicitly user-requested output 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. |
|
I'll look into other libraries for references, once I have the answer I'll come back, tks for review |
Pull request type
Checklist
black rocketpy/ tests/) has passed locallypytest tests -m slow --runslow) have passed locallyCHANGELOG.mdhas been updated (if relevant)Current behavior
RocketPy uses bare
print()calls scattered across 43 files to reportsimulation 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-inloggingmodule following best practices for libraries:
rocketpy/__init__.pyregisters aNullHandler, so the library issilent by default with no output unless the user configures a handler.
logger = logging.getLogger(__name__),creating a hierarchy (
rocketpy.simulation.flight,rocketpy.environment.environment, etc.) that users can filterindividually.
DEBUGfor high-frequency solver ticks and internal detailINFOfor normal operation confirmations (simulation completed, filessaved,
.info()output from all print classes)WARNINGfor unexpected but recoverable situations (missing motor,geometry auto-corrected, feature silently skipped)
ERRORfor optional dependency unavailable or feature brokento the user.
_SimMonitor.reprint()inMonteCarlois kept for backwardscompatibility and now delegates to
logger.info().Users can now direct logs to a file with a single call before running a
simulation:
Note on
verboseparameter: Theverbose=Trueflag onFlightretainsits original terminal behavior. It still prints the real-time progress
ticker using
\rline overwrite. Additionally, the same information is nowalso emitted via
logger.debug(), so users who configure a logging handlerwill capture it as well. Full backwards compatibility is preserved.
Breaking change
Additional information
print()calls replaced.