New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

logging-colorizer

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

logging-colorizer

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

  • 0.0.14
  • PyPI
  • Socket score

Maintainers
1

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

LevelDefault function nameAbbriviationShortened name
Infoinfoinfi
warningwarningwarnw
debugdebugdbgd
errorerrorerre
criticalcriticalcritc

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

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc