Skip to main content

Python logging formatters, filters, and convenience methods

Project description

DDV Logging

Python logging formatters, filters, and convenience methods.

GitLab: https://gitlab.com/davidevi/ddv-logging

PyPi: https://pypi.org/project/ddv-logging/

Features:

  • Can enable printing of logs to stdout
  • Can indent logs based on position in stack trace
  • Can colour the log level
  • Can filter out modules from the log output based on verbosity levels

Usage Example

The code:

import logging

from ddv.logging import log_to_stdout

logger = logging.getLogger(__name__)


class A(object):
    def __init__(self):
        self.logger = logging.getLogger(__name__ + ".A")
        self.logger.debug("Creating instance of A")
        self.b = B()
        self.A1()

    def A1(self):
        self.logger.info("A1 has been called")


class B(object):
    def __init__(self):
        self.logger = logging.getLogger(__name__ + ".B")
        self.logger.debug("Creating instance of B")
        self.c = C()
        self.B1()

    def B1(self):
        self.logger.info("B1 has been called")


class C(object):
    def __init__(self):
        self.logger = logging.getLogger(__name__ + ".C")
        self.logger.debug("Creating instance of C")
        self.C1()

    def C1(self):
        self.logger.info("C1 has been called")


def main():

    verbosity_filters = {  # Verbosity 0 will display none
        1: ["__main__.A"], # Verbosity 1 will display A
        2: ["__main__.B"], # Verbosity 2 will display A and B
        3: ["__main__.C"]  # Verbosity 3 will display A, B, and C
    }

    log_to_stdout(
        logging_level=logging.DEBUG,
        enable_colours=True,
        enable_indentation=True,
        verbosity_filters=verbosity_filters,
        verbosity_level=2,
    )

    logger.info("Main has been called")
    A()
    logger.warning("Execution complete")


if __name__ == "__main__":
    main()

The output:

Output

Verbosity levels could come from the command line:

program --arg1 --arg2 -vvv

Where the number of v dictates the verbosity level.

You could simply use the length of -vvv to determine verbosity level.

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

ddv-logging-0.3.0.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

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

ddv_logging-0.3.0-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file ddv-logging-0.3.0.tar.gz.

File metadata

  • Download URL: ddv-logging-0.3.0.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.5 CPython/3.9.0 Darwin/19.6.0

File hashes

Hashes for ddv-logging-0.3.0.tar.gz
Algorithm Hash digest
SHA256 f6d6d7f656390ef2a2093bc76bbb3b87e01746e00b3aefae76bb8662c3aa242f
MD5 168e124dd898e9694c01599f3abb7ddb
BLAKE2b-256 4f04b8c57bf9709f339724ca8f09c911883e3a33b3c2d0ce5ef70507b63cd6f3

See more details on using hashes here.

File details

Details for the file ddv_logging-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: ddv_logging-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 4.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.5 CPython/3.9.0 Darwin/19.6.0

File hashes

Hashes for ddv_logging-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9a7d6467d2fa2b5404e0d1cb5b746afe2173023528f6cb6557698a269126d4e9
MD5 c598e1bed4fa684318db677ec6153b33
BLAKE2b-256 53fd09f9bd24715b0df5926efa87bee315af7510ed62649566160600fc6aad0e

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