Skip to main content

Shared logging + Slack-alerting for BetShareMarket Python services

Project description

betshare-infra-logging

Shared logging + Slack-alerting for BetShareMarket Python services. Extracted from the near-identical per-service conf_logger modules (consumer, joiner-events, betfair-exchange-scraper).

Import name: betshare_infra_logging. Distribution name: betshare-infra-logging.

Why

The per-service copies drifted in accidental ways that were bugs:

  • Slack POST hang. joiner-events and betfair-exchange-scraper called requests.post(webhook, ...) with no timeout, so a hung Slack call could freeze their single-threaded loops. This package bakes in a finite timeout (SLACK_TIMEOUT = 10) with no public parameter to override it to None.
  • Level env var schism (LOGGER_LEVEL vs LOG_LEVEL).
  • Slack channel drift (#error vs #errors).
  • Idempotency, noise suppression, and correlation IDs were each reinvented or missing.

Install

Production (private registry, version-pinned per service):

# in the service's pyproject.toml
betshare-infra-logging = "^0.1.0"

Local development (PATH dependency, used by the pilot migration):

betshare-infra-logging = { path = "../../packages/py-infra-logging", develop = true }

Or with pip: pip install -e packages/py-infra-logging.

Usage

One-liner at service startup. A service's thin conf_logger.py shim collapses to:

from betshare_infra_logging import configure_logging

logger = configure_logging("joiner")  # Slack timeout now bounded => hang bug fixed

configure_logging configures the ROOT logger, so existing logging.getLogger(__name__) call sites keep working unchanged via propagation.

Correlation-id / context binding:

log = logger.child(correlation_id=cid, bookmaker="bet365")
log.info("processing message")
# -> "... processing message [correlation_id=... bookmaker=bet365]"
# context shows in console output AND in the Slack payload.

get_logger(name) returns a Logger wrapping logging.getLogger(name).

Notes

  • No import-time side effects. Importing the package does NOT configure logging (unlike the old modules which called configure_logger() at import). Call configure_logging(...) explicitly. The package does NOT call load_dotenv() — that stays in the service entrypoint.
  • Level resolution priority (Python majority order): level arg ?? LOGGER_LEVEL env ?? LOG_LEVEL env ?? "INFO". Cutover does not change any service's effective level and no .env is edited.
  • Slack channel defaults to #errors (unified from the Python services' #error). The channel field is cosmetic for modern Slack incoming webhooks — the destination is fixed by the webhook URL itself, so this change is safe and does not reroute alerts.
  • Idempotent. Calling configure_logging twice does not stack duplicate console/Slack handlers; the package tracks and clears the handlers it owns.
  • Slack failures are swallowed. A failing Slack POST never propagates into the caller's loop.

Public interface

DEFAULT_QUIET_LOGGERS = (
    "azure", "azure.servicebus", "azure.core",
    "motor", "pymongo", "crawl4ai", "playwright",
)
SLACK_TIMEOUT = 10  # seconds, baked in

def configure_logging(service, *, level=None, slack_webhook=None,
                      quiet_loggers=None) -> Logger: ...
def get_logger(name=None) -> Logger: ...

class Logger:
    def debug(self, message, **context) -> None
    def info(self, message, **context) -> None
    def warning(self, message, **context) -> None
    def error(self, message, exc=None, **context) -> None
    def child(self, **bindings) -> "Logger"

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

betshare_infra_logging-0.1.0.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

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

betshare_infra_logging-0.1.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file betshare_infra_logging-0.1.0.tar.gz.

File metadata

  • Download URL: betshare_infra_logging-0.1.0.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.14.3 Darwin/25.5.0

File hashes

Hashes for betshare_infra_logging-0.1.0.tar.gz
Algorithm Hash digest
SHA256 90592e52f8f9342c7ce24ee8eeac519cd0024d4cf11f58d6da0a619e7ad7291a
MD5 12b7d55d09ebf7f5b7c319a4e2082d8a
BLAKE2b-256 9618fd341e5cbb8f07d7f32bbcb20e830d87dd24c29b37991e967f06da1609e9

See more details on using hashes here.

File details

Details for the file betshare_infra_logging-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for betshare_infra_logging-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d884e52321823f77a8985eed78beaaae423d3a0348039028e8f6b5c1cfce3d16
MD5 83c77bd8db65460ba6c4121299d98e98
BLAKE2b-256 7e2c0c169b3af8ef858c270d92458aae8c11647074e61e5298592ad99b73bafc

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