Skip to main content

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

You can instll the package with pip or conda.

$ pip install rainbowlog
$ conda install rainbowlog -c abrahammurciano

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 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
)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rainbowlog-2.0.2.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

rainbowlog-2.0.2-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file rainbowlog-2.0.2.tar.gz.

File metadata

  • Download URL: rainbowlog-2.0.2.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.8 CPython/3.7.13 Linux/5.15.0-1014-azure

File hashes

Hashes for rainbowlog-2.0.2.tar.gz
Algorithm Hash digest
SHA256 35b3560f54a0fd0dde982b0bc7c5ce75d61ec3de51b72f2b9e921cd2295b2d76
MD5 ca4a0ac9a9cc70c1831f5505f249dac7
BLAKE2b-256 00cb71602731f15013c4ffe1122d473a81800c419a0341bc6e13e011fe47e4ef

See more details on using hashes here.

File details

Details for the file rainbowlog-2.0.2-py3-none-any.whl.

File metadata

  • Download URL: rainbowlog-2.0.2-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.8 CPython/3.7.13 Linux/5.15.0-1014-azure

File hashes

Hashes for rainbowlog-2.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b75c0a49e59f8a7866d8dfed8632ac406bbdc0baf520ec102e644154d135d459
MD5 0e666c45e768f9de6180a8ddbd5ddff0
BLAKE2b-256 c187ae70e6ae6b28de05b1a7e5729c17c6ae8d5e1a43c341f2cd499f86eaca14

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page