Skip to main content

service-logging

Python logging configurations done The Right Way

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.

Metadata

Release files for service-logging 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for service-logging 0.1.1
File Size Uploaded
service-logging-0.1.1.tar.gz 9.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for service-logging 0.1.1
File Interpreter ABI Platform
service_logging-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 17.5 kB

Release files / service-logging-0.1.1.tar.gz

Download URL service-logging-0.1.1.tar.gz
Size 9.6 kB
Tags Source
SHA-256 checksum
How to use checksums
6229711149b6148188d94d95218bf82df53543821d4c16c0103e692613cebf4b
BLAKE2b-256 checksum
How to use checksums
27b869bc65a853cc39147fca807b107ba35cad7e04b36384c3018ff4a7bb9b8d
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / service_logging-0.1.1-py3-none-any.whl

Download URL service_logging-0.1.1-py3-none-any.whl
Size 7.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
83a73adb21e345b86e29f2b627fd01cefd9b0c843659dc9c60eb713c08a5b0dd
BLAKE2b-256 checksum
How to use checksums
2cbf8addcff9ca4acda3cf115ff27d478291b430b6de6db2ba61f9b6e15e3aa3
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page