Colorama/HTML Capable Logger
cpylog is a simple, pure Python logging library designed to provide colorful console output using Colorama and rich HTML output capabilities (via Pygments). It offers a straightforward API to configure and manage loggers with various handlers and formatters. The current version is 1.6.0. The library appears to be in maintenance mode, with its last release in 2021, indicating a stable but not actively developed project.
Common errors
-
ModuleNotFoundError: No module named 'cpylog'
cause The `cpylog` library has not been installed in the current Python environment.fixInstall the library using pip: `pip install cpylog` -
ModuleNotFoundError: No module named 'colorama'
cause `colorama` is an optional dependency required for colorful console output and was not installed.fixInstall `cpylog` with the color extra: `pip install cpylog[color]` -
ModuleNotFoundError: No module named 'pygments'
cause `pygments` is an optional dependency required for HTML output and was not installed.fixInstall `cpylog` with the html extra: `pip install cpylog[html]`
Warnings
- gotcha cpylog implements its own logging interface (`cpylog.get_logger`) and configuration (`cpylog.config.basic_config`) which, while inspired by, are not fully compatible with Python's standard `logging` module. Direct interoperability or assumption of standard `logging` module behavior may lead to unexpected results.
- gotcha Features like colorful console output (using `ColorFormatter`) and syntax-highlighted HTML output (using `HTMLFileHandler` or `HTMLStreamHandler`) require optional dependencies (`colorama` and `pygments` respectively) which are not installed by default with `pip install cpylog`.
- gotcha The `cpylog` library has not seen active development since its last release in 2021. While stable, new features or prompt bug fixes are unlikely. Consider this if integrating into a long-term project requiring active maintenance or cutting-edge features.
Install
-
pip install cpylog -
pip install cpylog[color] -
pip install cpylog[html]
Imports
- get_logger
from cpylog import get_logger
- basic_config
from cpylog.config import basic_config
- HTMLFileHandler
from cpylog import HTMLFileHandler
- ColorFormatter
from cpylog.formatters import ColorFormatter
Quickstart
from cpylog.config import basic_config
from cpylog import get_logger, HTMLFileHandler
import os
# Configure basic logging to console (INFO level by default)
basic_config()
logger = get_logger("my_app")
logger.info("This is an informational message to console.")
logger.warning("A warning message.")
# Configure and add an HTML file handler
html_handler = HTMLFileHandler("app_log.html")
logger.addHandler(html_handler)
logger.info("This message will go to console and app_log.html.")
logger.error("An error occurred, see details in HTML file.")
# To demonstrate basic logging with different levels
logger.debug("This is a debug message (won't show by default with basic_config).")
logger.critical("Critical error! System might be down.")
print("Check app_log.html for HTML output.")
# Clean up the generated file (optional, for demonstration)
# try:
# os.remove("app_log.html")
# except OSError:
# pass