Skip to main content

Auto-Configuration solution for Python built-in logging.

Project description

uologging is a solution for configuring Python's built-in logging module.

A full example is provided in the docstring for uologging.init_console_logging().

Configuring logging via uologging

The verbosity_flag can be gathered via argparse using uologging.get_default_parser(), or uologging.add_verbosity_flag(parser):

import uologging
import argparse

# Option 1
parser = argparse.ArgumentParser(parents=[uologging.get_default_parser()])
# Option 2
# uologging.add_verbosity_flag(parser)
args = parser.parse_args(['-vv'])

Now, call uologging.init_console_logging() with that verbosity_flag for YOUR package:

uologging.init_console_logging('mypackage', args.verbosity_flag)

Default Log Level

Per Python logging suggestion: WARNING, ERROR, and CRITICAL messages are all logged by default. Meanwhile, INFO and DEBUG messages can be enabled by providing verbosity_flag of 1 or 2 to uologging.init_console_logging().

Logging messages format

The formatting for log messages is specified in the (private) uologging._logging_format variable.

Here are a couple of lines showing what you can expect your logs to looks like:

2021-12-21 10:52:16,430 -- akips_netdot_sync.netdot_util [INFO] Getting all NetDot devices.
2021-12-21 10:52:16,430 -- akips_netdot_sync.netdot_util [WARNING] Unable to parse device: example-sw1.uoregon.edu.

logging Best Practices

Use the Python logging package per the following best practices:

  • logger = logging.getLogger(__name__) to get the logger for each module/script.
  • Use logger.debug(), logger.info(), logger.warning(), etc to add tracing to your modules/packages/scripts.

A trivial example demonstrating best practices:

# hello.py
import logging

logger = logging.getLogger(__name__)

def hello():
    logger.debug('About to say "hello!"')
    print('hello!')
    logger.debug('Said "hello!"')

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

uologging-0.2.1.tar.gz (6.0 kB view details)

Uploaded Source

File details

Details for the file uologging-0.2.1.tar.gz.

File metadata

  • Download URL: uologging-0.2.1.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.6

File hashes

Hashes for uologging-0.2.1.tar.gz
Algorithm Hash digest
SHA256 326c724c733c19f88dc803486b7cb542d510d3320e6d0d27bea7c461b4d8dc06
MD5 6e13e200d417b9c1f9600eed8a8423a1
BLAKE2b-256 40c4fa66e8c55961b5fbb627e96d68ef5a1d51b91fa0f3fb803638cfbc61342b

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