Skip to main content

Python logging configurations done The Right Way for programs that may run in the foreground or background

Project description

https://github.com/rpatterson/service-logging/workflows/Run%20linter,%20tests%20and,%20and%20release/badge.svg

TL;DR:

logging.basicConfig() is very often not enough, leading to DRY violations. Use service-logging to log to syslog or the Windows event log when running in the background, and log to stderr when running in the foreground, both with well formatted messages.

Using logging correctly is often both usage and OS specific. For a usage specific example, when developing or troubleshooting a daemon and/or service it is often useful to use one logging configuration when running in the foreground in a shell and another when running in the background. For an OS specific example, when running in the background a daemon and/or service should use the appropriate logging service for that system so that logging metadata is correct (severity, facility, etc.), but the right way to do that is OS specific. Furthermore, deciding between these configurations requires logic that cannot be done through the simple configuration mechanisms provided by Python’s logging.config.

Given these realities, logging is often not done “The Right Way”, and/or is difficult to adjust for debugging/testing vs background. service-logging aims to address this by providing the logic to choose between correct logging configurations for different usages and OSes. It also provides several ways to use these configurations, making the depth of commitment opt-in.

Installation

Install using any tool for installing standard Python 3 distributions such as pip:

$ sudo pip3 install service-logging

Usage

Use servicelogging.basicConfig() to add an OS and usage specific root logging hander:

>>> import servicelogging
>>> servicelogging.basicConfig()

The command-line script also supports wrapping another Python script with logging configured:

$ python -m servicelogging foreground_demo.py
$ service-logging foreground_demo.py

See the command-ling help for details on options and arguments:

$ service-logging --help
usage: service-logging [-h] [--level LEVEL] script

python logging configurations done The Right Way, top-level package.

positional arguments:
  script         The Python script to run after configuring logging

optional arguments:
  -h, --help     show this help message and exit
  --level LEVEL  The level of messages to log at or above (default: 20)

Or if you just want the appropriate handler and formatter to use as you’d like in your code, you can use servicelogging.choose_handler():

>>> import servicelogging
>>> handler = servicelogging.choose_handler()

TODO

Features for future releases

Support alternate logging configuration options:

Make sure that the approach is compatible with the various Python logging.config options.

Anything else you find youself doing over and over:

Submit a PR and make an argument for why a change would be useful in the vast majority of cases.

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

service-logging-0.1.1.tar.gz (9.6 kB view hashes)

Uploaded Source

Built Distribution

service_logging-0.1.1-py3-none-any.whl (7.9 kB view hashes)

Uploaded Python 3

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