PrintEvents is a Python utility class built on top of the rich library to provide standardized, color-coded logging and display capabilities across an application. It allows developers to use predefined "tags" (e.g., db, error, success) to ensure consistent visual output in terminal interfaces.
- Styled Logging: Easily log messages with specific semantic tags that map to a custom color theme.
- Customizable Theme: A centralized theme defines how each tag is rendered.
- Error Reporting: Detailed exception information (file, function, line number) can be formatted and displayed with specialized error styling.
- CLI Utilities: Includes helper methods for capturing styled text for use in command-line interfaces.
Ensure you have the rich library installed:
pip install rich
or
uv add richfrom nuPac.PrintEvents.main import PrintEvents
log = PrintEvents()
# Using predefined tags
log.display("db", "Connecting to database...")
log.display("success", "Data retrieved successfully!")
log.display("error", "An unexpected error occurred.")PrintEvents can format exception details for clear debugging:
try:
1 / 0
except Exception as e:
log = PrintEvents()
log.error_info(e, custom_msg="Something went wrong with calculation")Use the cli method to capture styled text for use in interactive prompts:
log = PrintEvents()
prompt = log.cli("Enter your name: ", "cyan")
print(f"Captured prompt: {prompt}")| Method | Description |
|---|---|
display(tag, message) |
Logs a message with the style associated with the given tag. |
error_info(e, custom_msg="") |
Formats an exception and displays it using the failure tag. |
cli(tag, color) |
Captures a styled string of text for use in CLI outputs. |
display_all() |
A utility method that iterates through all tags in the current theme and prints "test". |
rich