Skip to main content

A colorful logger for python3.

Project description

:warning: This README is translated by Google. If there is a grammatical error, please open an issue to correct it!

Python Colorful Logger

Downloads Downloads Downloads

Colorful logger for python3.

How to use

Install

pip install colorful-logger

Usage

1 default logger

You can directly use the default logger, the colored logs will be printed on the terminal, and the default logger level is warning.

from colorful_logger import logger

with logger:
    logger.debug("default logger")
    logger.info("default logger")
    logger.warning("default logger")
    logger.error("default logger")

As you can see, logger needs to be executed in the with statement, because this package uses QueueListener to call log output. You need to call the start method before using logger to output the log, and you need to call the stop after the end of use. I encapsulated these two methods in the with statement. In non-special scenarios, there is no need to call the start and stop methods separately.

image-20230221100744751

2 custom logger

You can also change the log level, save the log to a file, change the logger name, and the log may not be output to the terminal.

from colorful_logger import get_logger, DEBUG


def demo_logger(to_file=False):
    file = "test_%d.log"

    l1 = get_logger(
        "demo",
        DEBUG,
        add_file_path=False,
        disable_line_number_filter=False,
        file_path=file % 1 if to_file else None,
    )
    with l1:
        l1.debug("without file path")
        l1.info("without file path")
        l1.warning("without file path")
        l1.error("without file path")

    l2 = get_logger(
        "demo",
        DEBUG,
        add_file_path=True,
        disable_line_number_filter=False,
        file_path=file % 2 if to_file else None,
    )
    with l2:
        l2.debug("with file path")
        l2.info("with file path")
        l2.warning("with file path")
        l2.error("with file path")

    l3 = get_logger(
        None,
        DEBUG,
        add_file_path=True,
        disable_line_number_filter=True,
        file_path=file % 3 if to_file else None,
    )
    with l3:
        l3.debug("without name, and with path")
        l3.info("without name, and with path")
        l3.warning("without name, and with path")
        l3.error("without name, and with path")

    l4 = get_logger(
        None,
        DEBUG,
        add_file_path=False,
        disable_line_number_filter=True,
        file_path=file % 4 if to_file else None,
    )
    with l4:
        l4.debug("without name and path")
        l4.info("without name and path")
        l4.warning("without name and path")
        l4.error("without name and path")

There may be unexpected situations when outputting logs outside of the with statement, which may not achieve the expected results.

image-20230221100003891

The content of the log file ./test.log (example, inconsistent with the information in the above figure):

10:09:33.146 DEB demo:26 - without file path
10:09:33.146 INF demo - without file path
10:09:33.146 WAR demo - without file path
10:09:33.146 ERR demo:29 - without file path

The log output to the file is not a color log by default.

If you need to save the color log in a file, set the file_colorful parameter to True. In this example, the color log is saved.

The color log file has only one function, which is to view the real-time log in the terminal:

  • Unix
tail -f test.log
# 或
cat test.log
  • Windows
Get-Content -Path test.log

3 child logger

After defining a logger, I want to use all the parameters of this logger except name to output the log. At this time, you need to use the child_logger method to generate a child logger. The child logger needs to be in the with of the parent logger Execute in the statement:

from colorful_logger import get_logger, DEBUG

# parent logger
logger = get_logger(name="sample_logger", level=DEBUG, file_path="./test.log")

with logger:
    logger.error("parent error")
    l1 = logger.child("l1")
    l1.error("l1 error")
    l1.fatal("l1 fatal")

The child logger is the same except that the name is different from the parent logger, and it will not output the log of the third-party library.

The execution of the child logger in the with statement of the parent logger does not mean that it must be called directly in the with statement. It can be executed in a function in the with statement, such as:

# log.py
from colorful_logger import get_logger, DEBUG

logger = get_logger(name="sample_logger", level=DEBUG, file_path="./test.log")
# main.py
from log import logger
from other_file import test

with logger:
    test()
# other_file.py

test_logger = logger.child("test_logger")

def test():
    test_logger.error("test error")

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

colorful-logger-0.2.0a3.tar.gz (14.8 kB view details)

Uploaded Source

Built Distribution

colorful_logger-0.2.0a3-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

Details for the file colorful-logger-0.2.0a3.tar.gz.

File metadata

  • Download URL: colorful-logger-0.2.0a3.tar.gz
  • Upload date:
  • Size: 14.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for colorful-logger-0.2.0a3.tar.gz
Algorithm Hash digest
SHA256 c97120f1b12e81273d7aefcdd412629273304d51f7d7f629dc175082b25739f4
MD5 29d0c4ab41afc78f273ba10e176b4dfb
BLAKE2b-256 dfbcd1debf9a885df4fddfb2a903ba7fa8885207d14aadc82f272c90e5624303

See more details on using hashes here.

File details

Details for the file colorful_logger-0.2.0a3-py3-none-any.whl.

File metadata

File hashes

Hashes for colorful_logger-0.2.0a3-py3-none-any.whl
Algorithm Hash digest
SHA256 62b27cb2e715447c3568328b416beda7429734ab699a4c97c47c30c43c7e38a0
MD5 6a7c87278c18e674e2e136c9a2a243c6
BLAKE2b-256 08c37f8457c96b229ce1c5d7a80413d4d7c7f9c1a78307603ce2af72d95e8cc6

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