Skip to main content

A colorful logger for python3.

Project description

python-logger

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

logger.debug("This is a debug message.")
logger.info("This is a info message.")
logger.warning("This is a warning message.")
logger.error("This is a error message.")
logger.fatal("This is a fatal message.")

default logger

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
from colorful_logger.logger import DEBUG

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

logger.debug("This is a debug message.")
logger.info("This is a info message.")
logger.warning("This is a warning message.")
logger.error("This is a error message.")
logger.fatal("This is a fatal message.")

custom logger

The contents of the log file:

[DEBUG] 2021-05-21 15:08:42 test.py:8 - This is a debug message.
[INFO] 2021-05-21 15:08:42 test.py:9 - This is a info message.
[WARNING] 2021-05-21 15:08:42 test.py:10 - This is a warning message.
[ERROR] 2021-05-21 15:08:42 test.py:11 - This is a error message.
[CRITICAL] 2021-05-21 15:08:42 test.py:12 - This is a fatal message.

As you can see, the log saved to the file uses the default formatter, so it is not in color.

3 basic config

Use the basic_logger function to configure the global logger.

from colorful_logger import basic_logger
from colorful_logger.logger import INFO

basic_logger(level=INFO, show=False, file_path="./test.log")

Then all loggers generated in other files will use this configuration.

# a.py
import logging

logger = logging.getLogger("a")

# b.py
import logging

logger = logging.getLogger("b")

When the level is set to DEBUG or NOSET, there may be logs generated by other third-party libraries when using the global configuration.

4 child logger

If you don't want to see the logs generated by third-party libraries, you need to use the child_logger method.

from colorful_logger import get_logger
from colorful_logger.logger import DEBUG

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

# child logger in other files
from colorful_logger import child_logger

# child1.py
child1 = child_logger("child1", logger)
# child2.py
child2 = child_logger("child2", logger)
# child3.py
child3 = child_logger(__name__, logger)

If all loggers in your project have only one parent logger, you can reimplement a chil_logger method.

from colorful_logger import get_logger, child_logger
from colorful_logger.logger import DEBUG

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

def my_child_logger(name: str):
    return child_logger(name, logger)

# child1.py
child1 = my_child_logger("child1")
# child2.py
child2 = my_child_logger("child2")
# child3.py
child3 = my_child_logger(__name__)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

colorful_logger-0.0.4-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file colorful_logger-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: colorful_logger-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.2

File hashes

Hashes for colorful_logger-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 ee945a24ea361d07fe50958a2553a803138db7701dd88b290909d41955bcaa62
MD5 b6d62889d9b2502a3e26d73e01c9d7be
BLAKE2b-256 a46b1c21918af076a635eb77581b71838d7ab4cb7f4e3abb91474232b00506f9

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