Rainbow Log
Format your python logs with colours based on the log levels.
Installation
You can instll the package with pip.
$ pip install rainbowlog
$ conda install rainbowlog -c abrahammurciano
Links
Usage
Here's a basic example of a script that logs colorfully to the console, but regularly to a file.
import logging
import rainbowlog
logger = logging.getLogger(__name__)
# This one will write to the console
stream_handler = logging.StreamHandler()
# This one will write to a file
file_handler = logging.FileHandler("output.log")
# Here we decide how we want the logs to look like
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
# We want the stream handler to be colorful
stream_handler.setFormatter(rainbowlog.Formatter(formatter))
# We don't want the file handler to be colorful
file_handler.setFormatter(formatter)
# Finally we add the handlers to the logger
logger.addHandler(stream_handler)
logger.addHandler(file_handler)
if __name__ == "__main__":
logger.debug("This is a debug message")
logger.info("This is an info message")
logger.warning("This is a warning message")
logger.error("This is an error message")
logger.critical("This is a critical message")
If you want to change the format of the logs for each log level, you can use any callable that takes a string and returns the same string with ANSI codes surrounding it. There are many libraries you can use to provide such callables.
import logging
from rainbowlog import Formatter
# Here are some libraries you can use to get a style callable without dealing with ANSI codes
from constyle import Style, Attributes as Attrs
import termcolor
from functools import partial
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
color_formatter = Formatter(
formatter,
log_styles={
logging.DEBUG: Style(Attrs.BLUE, Attrs.FAINT), # An example using constyle
logging.INFO: lambda s: f"\033[32m{s}\033[0m", # An example using lambdas
logging.WARNING: termcolor.red, # An example using termcolor's predifined functions
logging.ERROR: partial(termcolor.colored, color="red", on_color="on_white", attrs=["bold"]), # An example using functools.partial
logging.CRITICAL: Attrs.RED + Attrs.ON_YELLOW + Attrs.BOLD + Attrs.UNDERLINE, # An example using constyle's added attributes
}
exception_style=lambda s: f"{Attrs.RED + Attrs.ON_WHITE + Attrs.BOLD}{s}{Attrs.RESET}" # An example using lambdas and constyle,
stack_style=Attrs.RED, # An example using a single constyle attribute
)
Metadata
Release files for rainbowlog 2.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| rainbowlog-2.1.0.tar.gz | 24.7 kB | Without provenance | Details for rainbowlog-2.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| rainbowlog-2.1.0-py3-none-any.whl | Python 3 | none | any | Details for rainbowlog-2.1.0-py3-none-any.whl |
Total release size: 29.2 kB
Release files / rainbowlog-2.1.0.tar.gz
| Download URL | rainbowlog-2.1.0.tar.gz |
|---|---|
| Size | 24.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
cb18b5da986b648cea72fd783b4ac5ca9a45627d4a3c8b7fa6fddb5d55bc328d
|
|
BLAKE2b-256 checksum How to use checksums |
87d8a145dd742167b87c3d84b68e2ab2424c42907ed9a28872d452ab0e74457f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.6.3
|
Release files / rainbowlog-2.1.0-py3-none-any.whl
| Download URL | rainbowlog-2.1.0-py3-none-any.whl |
|---|---|
| Size | 4.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
857ac6c745134a3a25de0deaf004f7f5cfab50be1e9cf31bc8b7ef2ab6b5fb55
|
|
BLAKE2b-256 checksum How to use checksums |
b50f27d7d35dba021391329e596e5b8f46ebcdf0f6d17612fac11245a78dcbd2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.6.3
|
Security for rainbowlog 2.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.