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 details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

Details for the file service-logging-0.1.1.tar.gz.

File metadata

  • Download URL: service-logging-0.1.1.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.3

File hashes

Hashes for service-logging-0.1.1.tar.gz
Algorithm Hash digest
SHA256 6229711149b6148188d94d95218bf82df53543821d4c16c0103e692613cebf4b
MD5 dc3455b824eeb716685450aa0ce3eab4
BLAKE2b-256 27b869bc65a853cc39147fca807b107ba35cad7e04b36384c3018ff4a7bb9b8d

See more details on using hashes here.

File details

Details for the file service_logging-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: service_logging-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.3

File hashes

Hashes for service_logging-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 83a73adb21e345b86e29f2b627fd01cefd9b0c843659dc9c60eb713c08a5b0dd
MD5 35090ed43de9ad89c4e8ca7dabc59286
BLAKE2b-256 2cbf8addcff9ca4acda3cf115ff27d478291b430b6de6db2ba61f9b6e15e3aa3

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