Format your python logs with colours based on the log levels.
Project description
Rainbow Log
Format your python logs with colours based on the log levels.
Installation
pip install rainbowlog
Docs
You can find the documentation here
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 construct the rainbowlog.Formatter
object like this:
import logging
from rainbowlog import Formatter, Format, Color, Style
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
color_formatter = Formatter(
formatter,
color_configs={
logging.DEBUG: Format(Color.BLUE, style=Style.FAINT),
logging.INFO: Format(Color.GREEN),
logging.WARNING: Format(Color.YELLOW, style=Style.ITALIC),
logging.ERROR: Format(Color.RED, Color.WHITE, Style.BOLD),
logging.CRITICAL: Format(Color.RED, Color.YELLOW, (Style.BOLD, Style.UNDERLINE)),
}
exception_config=Format(Color.RED, Color.WHITE, Style.BOLD),
stack_config=Format(Color.RED, Color.WHITE, Style.BOLD),
)
NOTE: You can pass instead of a Format object, a dict of keyword arguments which ansicolors library's
color
function accepts. See the ansicolors documentation. This will usually not be necessary.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file rainbowlog-1.0.0.tar.gz
.
File metadata
- Download URL: rainbowlog-1.0.0.tar.gz
- Upload date:
- Size: 4.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.8 CPython/3.7.13 Linux/5.13.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7cef3726f27ae4ae8a34dbcd07305bad8a8ffb1e774186238cdaadc6b1865c51 |
|
MD5 | 2ff54f75957e584604bd14b74d8eb012 |
|
BLAKE2b-256 | 4d0b9d5c79f1f8189b1bf5c274905cf0fa03d989cf3765fb500e37c28b4be530 |
File details
Details for the file rainbowlog-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: rainbowlog-1.0.0-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.8 CPython/3.7.13 Linux/5.13.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c088de4612d99feee34e9efcbbff1e023ffebfdde6b0cac03ef800d5e4b57437 |
|
MD5 | 967c2a53ea9b34aebcedf78e6b4019ea |
|
BLAKE2b-256 | 87fccd45a89cf0dcf37458f835b5a777af3c3bb7a18b4ab466aed54ceb960a5c |