Skip to main content

Do natural python logging in a natural manner

Project description

goodlog

Built on top the standard library's logging, this package allows for quick and simple logging configuration and provides useful, common utilities like JSON formatting and adding extra info to logs, with minimal configuration.

Install

pip install goodlog

Quick start

import goodlog

# Configure logging once at application startup
goodlog.configure_logging(global_extra_info={"service": "my-app"})

# Create a logger and use it
logger = goodlog.create_logger(__name__)
logger.info("Application started")

Logs are emitted as JSON to stdout:

{"logger_name": "__main__", "timestamp": "2025-01-01 12:00:00,000", "log_level": "INFO", "message": "Application started", "extra_info": {"service": "my-app"}, ...}

Adding ephemeral extra info

Use the ephemeral_info_context context manager to attach extra fields to all logs within a scope:

from goodlog import ephemeral_info_context, create_logger

logger = create_logger(__name__)

with ephemeral_info_context(user_id=42):
    logger.info("Some user action")
logger.info("Some later action")

Output log will be:

{"logger_name": "__main__", "timestamp": "2025-01-01 12:00:00,000", "log_level": "INFO", "message": "Some user action", "extra_info": {"service": "my-app", "user_id": 42}, ...}
{"logger_name": "__main__", "timestamp": "2025-01-01 12:00:00,000", "log_level": "INFO", "message": "Some later action", "extra_info": {"service": "my-app"}, ...}

Inside the context, use add to add more info along the way:

from goodlog import ephemeral_info_context, create_logger, add

logger = create_logger(__name__)

with ephemeral_info_context(user_id=42):
    logger.info("Some user action")
    ... # some logic discovering the user name
    add(user_name="John Doe")
    logger.info("Discovered user name")
logger.info("Some later action")

Output log will be:

{... "message": "Some user action", "extra_info": {"service": "my-app", "user_id": 42}, ...}
{... "message": "Discovered user name", "extra_info": {"service": "my-app", "user_id": 42, "user_name": "John Doe"}, ...}
{... "message": "Some later action", "extra_info": {"service": "my-app"}, ...}

The remove function can be used to remove ephemeral info to add more info along the way:

from goodlog import ephemeral_info_context, create_logger, add, remove

logger = create_logger(__name__)

with ephemeral_info_context(user_id=42):
    logger.info("Some user action")
    ... # some logic discovering the user name
    add(user_name="John Doe")
    logger.info("Discovered user name")
    remove()
    logger.info("No extra info here")

Output log will be:

{... "message": "Some user action", "extra_info": {"service": "my-app", "user_id": 42}, ...}
{... "message": "Discovered user name", "extra_info": {"service": "my-app", "user_id": 42, "user_name": "John Doe"}, ...}
{... "message": "No extra info here", "extra_info": {"service": "my-app"}, ...}

As you can see, calling remove explicitly inside the context has the same effect as if we are already out of the context's scope.

Links

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

goodlog-0.0.1rc6.tar.gz (17.9 kB view details)

Uploaded Source

Built Distribution

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

goodlog-0.0.1rc6-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file goodlog-0.0.1rc6.tar.gz.

File metadata

  • Download URL: goodlog-0.0.1rc6.tar.gz
  • Upload date:
  • Size: 17.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for goodlog-0.0.1rc6.tar.gz
Algorithm Hash digest
SHA256 216bd5bbbeda77dee8e7234cb3d8c91705d19c39ad6488325c7c9df413432c90
MD5 5245b604379628b23dd21ef684b5b21a
BLAKE2b-256 636da6fdb16fe5c53a1ac2746bb51244b35b984704c43f9b7ef12134b7165ba5

See more details on using hashes here.

Provenance

The following attestation bundles were made for goodlog-0.0.1rc6.tar.gz:

Publisher: publish.yml on benronen8/goodlog

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file goodlog-0.0.1rc6-py3-none-any.whl.

File metadata

  • Download URL: goodlog-0.0.1rc6-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for goodlog-0.0.1rc6-py3-none-any.whl
Algorithm Hash digest
SHA256 d342b78f26c72a9ccebb5f6a5a46b82dfb3580ecaf2867fdaab15bfa2b63a6e5
MD5 59d8698dc9489a97ba6e4787b8c60497
BLAKE2b-256 46319969029646e9ba090cddcfaadb1b321ad4a11676068bd980497558776ada

See more details on using hashes here.

Provenance

The following attestation bundles were made for goodlog-0.0.1rc6-py3-none-any.whl:

Publisher: publish.yml on benronen8/goodlog

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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