Skip to main content

A pretty and colorful logger to emulate nest.js's logger and overwrite the python print function.

Project description

Pretty Logger

Description

This is python package has been developed to emulate the nest.js integrated logger. It allows for colorful terminal logs as well as activating or deactivating debug/warn or error print statements.
It is able to save all logs to text files for better quiet program running, or could be used while running in verbose.
Lastly, it allows for the overwrite of print statements within a function by the use of function decorators.

Install

pip install kayer-pretty-logger

In-code usage

To use the pretty logger, you have two main options which I have split into procedural or object-oriented programming examples.

Example 1 - Procedural Programming:

To use it with procedural programming, you may either create an instance of the Logger similarly to the OOP example seen later, or you may use the decorators found in pretty_logger.decorators to overwrite the print statements. In this example we will show you how to use the decorators to change the behaviour of your functions.

The code before:

def my_function(a: int, b: int) -> int:
    print('We have entered the function')
    return a + b

print(my_function(1, 2))

The updated code:

from pretty_logger.decorators import pretty_debug

@pretty_debug
def my_function(a: int, b: int) -> int:
    print('We have entered the function')
    return a + b

print(my_function(1, 2))

Example 2 - OOP:

To use it in an OOP context, you first create an instance of the Logger class within the class you wish to have this logger run for and give it the class's name. Once you have an object for the logger, replace all instances of your print statements to the new object's log functions. Here you will be able to decide if your previous print statements should be of type: log, debug, or error.

The code before:

class TestClass:
    def __init__(self):
        print('Object created')

    def something(self, number: str) -> int:
        print('We are here')
        
        try:
            return int(number)
        except ValueError as e:
            print(e)
            
            return 0

The updated code:

from pretty_logger import Logger

class TestClass:
    logger: Logger
    
    def __init__(self):
        self.logger = Logger(TestClass.__name__)
        self.logger.log('Object created')

    def something(self, number: str) -> int:
        self.logger.debug('We are here')
        
        try:
            return int(number)
        except ValueError as e:
            self.logger.error(e)
            
            return 0

Running a program which has a Pretty Logger

After the pretty logger has been implemented, you can use the following flags when launching your python script to customize you log experience:

  • --pretty-debug: Allows for displaying debug/warn logs
  • --pretty-error: Allows for displaying error logs
  • --pretty-all: Activates all logs for script
  • --pretty-none: Disables all logs for script
  • --pretty-save: Activates the system which will save all logs to their corresponding files
  • --pretty-save-silent: Activates the system which will save all logs to their corresponding files in silent mode
  • --logger-folder-path path/to/folder: [Optional] Custom path to a directory to save the logs in

Examples:

This will run the script main.py with all the possible loggers:

python main.py --pretty-all

This will run the script main.py with in silent mode and save the logs to the ./custom_logs/ directory:

python main.py --pretty-save-silent --logger-folder-path ./custom_logs/

Buy me a coffee

If you wish to offer me a coffee, please buy yourself one instead and cheer in my name xD

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

kayer_pretty_logger-1.2.1.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

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

kayer_pretty_logger-1.2.1-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file kayer_pretty_logger-1.2.1.tar.gz.

File metadata

  • Download URL: kayer_pretty_logger-1.2.1.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.1

File hashes

Hashes for kayer_pretty_logger-1.2.1.tar.gz
Algorithm Hash digest
SHA256 5c5bd163bb0a1934d69d04911a3b8bce38009411142d88772bdfe431f1c4d1bb
MD5 deee230f436948a21ea7cbe355ec863f
BLAKE2b-256 9e6d66779b4add3c8bdf1e6c390d4af4872b1f0f8b9d2c842752c9c1ec5d45f6

See more details on using hashes here.

File details

Details for the file kayer_pretty_logger-1.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for kayer_pretty_logger-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c4b0435ef8260dd350cbb7db114839287086a7d7bdaa32344e19592228a6ba36
MD5 c89a53df387a577093d8c999f407109d
BLAKE2b-256 c87b13690f4274f53345f9e9188e776b0708ea97e97b659b31a4248b52dec3b6

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