Skip to main content

Extends default python logging package to include 256-bit terminal colors

Project description

pycolorlogs

Intoduction

The name pycolorlogs is a wordplay between the words python, colors and logs

pythoncolorslogs

pycolorlogs Is a simple module to improve standard python logging capabilities by providing colored logging outputs, some context information and improved formatting. Future updates are in development to enable automatically redirection of logging to files similar to tee command in linux and some other functionalities. Feel free to provide suggestions/comments here on github by opening an issue


Implementation

This package enables 256-bit colored output in python logging package by instantiating an alternate logging.StreamHandler object which formats logs with colors and level descriptors, like on the screenshot below:

Image depicting an example of colored logging output

Under the hood, pycolorlogs just wraps around python's built in logging package and expose some useful methods and constants.


Quick Use

Using pycolorlogs is very simple, First you should install the package with the command:

python3 -m pip --user install pycolorlogs

Note: if you are using venv, pipenv or a similar virtual environment tool you may ignore the --user flag

to begin logging away just import the init_logs() method, the lowest severity which you plan to display and finally the logging methods such as debug() and warn()

from pycolorlogs import init_logs, INFO, info, debug

To use pycolorlogs simply initialize the package with the chosen severity level, like this:

init_logs(DEBUG)

and start logging away:

# Import the module
from pycolorlogs import init_logs, DEBUG, debug, error, info

# Initialize the handler
init_logs(DEBUG)

# Log away !
info('Starting program...')
a: int = 5
b: int = 3
result: int = a+b
debug(f'The result is {result}')
try:
    denominator = 0
    division = 100/denominator
except:
    error('Can\'t divide by 0 !!')
finally:
    debug(f'The result is: {division}')
info('Done !')

this outputs:



15/09-07:54:55 example.py | INFO: Starting program...

15/09-07:54:55 example.py | DEBUG: The result is 8

15/09-07:54:55 example.py l:17 | ERROR: Can't divide by 0 !!


In Depth Explanation

The first imported element, init_logs is the function responsible for initializing the Handler and replacing it with logging package's default instance. You should provide a logging level integer as a parameter to this method. This will tell the lowest level of logging that will be printed by your program. the options are, in verbosity order from lower to higher level:

  • CRITICAL or FATAL
  • ERROR
  • WARNING or WARN
  • INFO
  • DEBUG

These are actually defined as constant integers on the logging module, in pycolorlogs these values are just exported to the user in order to avoid the need to import logging module as well as pycolorlogs

After importing the init_logs() method, the least severe chosen logging level is imported, INFO on this example. This means that every log message whose level is lower than INFO will not be printed, while more severe messages, on the other hand, will be shown normally. If INFO is given as an argument to init_logs() it means that only DEBUG level messages will be hidden, while WARNING, ERROR and CRITICAL level information will keep being shown.

Lastly the appropriate logger functions are imported. These are used to log a message of the homonimous severity. info() method outputs INFO level messages, while debug()will print DEBUG information and so on.

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

pycolorlogs-1.1.1.tar.gz (43.7 kB view details)

Uploaded Source

Built Distribution

pycolorlogs-1.1.1-py3-none-any.whl (30.1 kB view details)

Uploaded Python 3

File details

Details for the file pycolorlogs-1.1.1.tar.gz.

File metadata

  • Download URL: pycolorlogs-1.1.1.tar.gz
  • Upload date:
  • Size: 43.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for pycolorlogs-1.1.1.tar.gz
Algorithm Hash digest
SHA256 e369ea2b99ae7e8a5015389354a494e6b8f52a22876a473ea81f7d1d05c29d96
MD5 414447568f4f1ce7e9f4addbe79a406c
BLAKE2b-256 af5dcc1a733869cae0b30a373abe81eb1891ef278341fa7cae65a49b69a6c9c3

See more details on using hashes here.

File details

Details for the file pycolorlogs-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: pycolorlogs-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 30.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for pycolorlogs-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ad0893b27b65e73820a2535ae19c48b7668173def19fafd050c0f64b7355334a
MD5 2ac543b1467168d54367a30660dcf460
BLAKE2b-256 b37dec8d48b9e30b5e1d28fd1bee3e220cc750d07d420d579f9ec4ad36dbc4fd

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