Skip to main content

This library is based on the requests library but it colorizes the logs.

Project description

Installation

you can install the following library simply by just running:

pip install logging-colorizer

Usage

In addition to use this library first we need to import it using:

from logging_colorizer import Log

after that we instantiate our logger and we also can costumize its format

log = Log() #this is the logger with its default settings
log = Log(format="%(light_black)s TIME: %(asctime)s %(cyan)s|%(light_black)s IP: %(clientip)s %(cyan)s|%(light_black)s CODE: %(Code)s %(cyan)s|%(purple)s [%(lvl)s] %(cyan)s|%(log_color)s %(message)s %(reset)s") # this is the logger with costumized format like so

The levels to trigger a log

Level Default function name Abbriviation Shortened name
Info info inf i
warning warning warn w
debug debug dbg d
error error err e
critical critical crit c

So all of three lines bellow do the exact same thing

log.info('Here is the info')
log.inf('Here is the info')
log.i('Here is the info')

And so on...

The colors

For using different colors in your logger you should provide the color on the format argument of the module like this:

log = Log(format="%(purple)s %(message)s %(reset)s")

in the above example in every call of the logger, it writes the message in purple color and then resets the color for future logs.(just incase if there were no colors provided)

instead of purple, you can use these colors:

  • light_black, black, bold_black
  • light_red, red, bold_red
  • light_green, green, bold_green
  • light_yellow, yellow, bold_yellow
  • light_blue, blue, bold_blue
  • light_purple, purple, bold_purple
  • light_cyan, cyan, bold_cyan
  • light_white, white, bold_white

Example

A boilerplate code can be like this:

# Imports
from logging_colorizer import Log
import requests
# A function that gets the current IP address
def check_ip():
    session = requests.session()
    ip = session.get("http://icanhazip.com").text.replace("\n","")
    session.close()
    return ip
ip = check_ip() # Run the function to get the ip
log = Log(format="%(light_black)s TIME: %(asctime)s %(cyan)s|%(light_black)s IP: %(clientip)s %(cyan)s|%(light_black)s CODE: %(Code)s %(cyan)s|%(purple)s [%(lvl)s] %(cyan)s|%(log_color)s %(message)s %(reset)s") # Initializing our logging module with our custom format

log.info('Here is an info log', extra_plus_ali={'clientip': ip, "Code": "Boilerplate.py"})
log.warning('Here is a warning log', extra_plus_ali={'clientip': ip, "Code": "Boilerplate.py"})
log.debug('Here is a debug log', extra_plus_ali={'clientip': ip, "Code": "Boilerplate.py"})
log.error('Here is an error log', extra_plus_ali={'clientip': ip, "Code": "Boilerplate.py"})
log.critical('Here is a critical log', extra_plus_ali={'clientip': ip, "Code": "Boilerplate.py"})
  • Which is supposed to write the current time, ip, the code name in black color and the log level in purple and the message color is based on the log level.
  1. if the log level is info, the message color will be green
  2. if the log level is warning, the message color will be yellow
  3. if the log level is debug, the message color will be white
  4. if the log level is error, the message color will be red
  5. if the log level is critical, the message color will be bold red

Which returns an output like bellow: ConsoleOutput

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

logging_colorizer-0.0.14.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

logging_colorizer-0.0.14-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

Details for the file logging_colorizer-0.0.14.tar.gz.

File metadata

  • Download URL: logging_colorizer-0.0.14.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.7

File hashes

Hashes for logging_colorizer-0.0.14.tar.gz
Algorithm Hash digest
SHA256 34e8b0adc3c9c4516530bf047ef415453f42ebac279917e697a418c5c1745b23
MD5 f03be10fd2cb7517bfb0df007f282b17
BLAKE2b-256 bbd3777ce515252b80effc160430d7585527547c0531f867df0c3e344a0c94b1

See more details on using hashes here.

File details

Details for the file logging_colorizer-0.0.14-py3-none-any.whl.

File metadata

File hashes

Hashes for logging_colorizer-0.0.14-py3-none-any.whl
Algorithm Hash digest
SHA256 887d5c7e3b1d3f8e4a1c198c50b7c7a6ec0ffc37d734071b272e64220a418c41
MD5 8bb71906e8b2ac4206b0abfda9762bca
BLAKE2b-256 182b38bfff7a624fe3edb1a5674a02a155e077a3f2643bbdc825a02e2dbb48b9

See more details on using hashes here.

Supported by

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