A Python library for generating HTML logs with support for colors, file rotation, tagging, and JavaScript filters.
- ✅ Colored logs in HTML format
- ✅ Automatic file rotation
- ✅ Clean interface with integrated JavaScript filters
- ✅ Thread-safe and high performance
- ✅ Exception support with full traceback
- ✅ Flexible configuration for file size and quantity
- ✅ Tagging system for message categorization
- ✅ Advanced filtering by tags and text content
- ✅ Default color mapping for specific tags
pip install py-html-logger
from loghtml import log, error, report_exception
# Simple messages
log("Normal informative message")
log("Blue message", color="blue")
# Error messages
error("This is an error message")
# Log exceptions
try:
# Your code here
raise ValueError("Example error")
except Exception as e:
report_exception(e)
The logger supports tagging messages for better organization and filtering:
from loghtml import log, info, debug, warning
# Tagged messages
log("User login", tag="auth")
info("Data processed", tag="processing")
debug("Variable value", tag="debug")
warning("Resource low", tag="system")
You can define default colors for specific tags:
from loghtml import set_default_tag_color
default_tag_colors = {
"database": "LightGrey",
"connection": "LightBlue",
"heartbeat": "Yellow"
}
set_default_tag_color(default_tag_colors)
from loghtml import config
# Customize logger settings
config(
max_files=15, # Maximum number of log files
max_size=5000000, # Maximum size per file (5MB)
main_filename="log.html", # Main file name
log_dir="logs" # Directory for logs
)
Logs are stored in the specified directory (default: logs/
) with the following structure:
logs/
└── log.html (current file)
└── 2023-10-05_12-30-45_log.html (rotated file)
└── 2023-10-05_10-15-32_log.html (rotated file)
Generated HTML files include advanced filtering capabilities to facilitate analysis:
- Text filtering with AND/OR logic
- Tag-based filtering
- Time period filtering
- Real-time highlighting of matched terms
- Preserved original log view
from loghtml import log, info, debug, warning, error, report_exception, config, set_default_tag_color
# Configure logger
config(
max_files=10,
max_size=2000000, # 2MB
log_dir="my_logs"
)
# Set default tag colors
default_tag_colors = {
"system": "green",
"processing": "cyan",
"checkpoint": "magenta"
}
set_default_tag_color(default_tag_colors)
# Log with different tags and levels
log("Application started", tag="system")
info("Loading configuration", tag="config")
debug("Initializing modules", tag="debug")
for i in range(100):
if i % 10 == 0:
log(f"Checkpoint {i}", tag="checkpoint")
info(f"Processing item {i}", tag="processing")
try:
# Code that might raise an error
result = 10 / 0
except Exception as e:
error("Division by zero detected")
report_exception(e)
log("Application finished", tag="system")
Logs a message with optional color and tag(s).
Logs an informational message.
Logs a debug message.
Logs a warning message.
Logs an error message (in red).
Logs an exception with its full traceback.
Configures logger options:
max_files
: Maximum number of files to maintainmax_size
: Maximum size in bytes per filemain_filename
: Main log file namelog_dir
: Directory where logs will be stored
Sets default colors for specific tags:
color_dict
: Dictionary mapping tag names to color values
Processes all pending messages before termination.
To contribute to the project:
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
If you encounter issues or have questions:
- Check the documentation
- Open an issue
- Contact: rphspires@gmail.com
This project is licensed under the MIT License - see the LICENSE file for details.
- Developed by Raphael Pires
- Inspired by the need for better log visualization and analysis tools