Skip to main content

AppSignal Logging for Python

PyPI version Python Support License: MIT

Python logging handlers for AppSignal with support for both simple HTTP and high-performance NDJSON batch endpoints.

Features

  • 🚀 Two handler types: Simple HTTP and batched NDJSON
  • 📦 Automatic batching: Efficiently send multiple logs in a single request
  • ⚡ High performance: Up to 10x faster than sending individual logs
  • 🔄 Async worker thread: Non-blocking log transmission
  • 🛡️ Error handling: Graceful failure with stderr logging
  • 🏷️ Structured logging: Support for custom attributes
  • 📝 Type hints: Full type annotation support
  • ✅ Well tested: Comprehensive test suite

Installation

pip install appsignal-logging

Quick Start

Simple HTTP Handler

Best for low-frequency logging (<10 logs/sec):

import logging
from appsignal_logging import AppSignalHTTPHandler

handler = AppSignalHTTPHandler(
    api_key="your_api_key",
    app_name="my_app",
    hostname="web-server-1"
)

logger = logging.getLogger()
logger.addHandler(handler)
logger.setLevel(logging.INFO)

logger.info("User logged in", extra={"user_id": 123, "ip": "192.168.1.1"})

Best for high-frequency logging (>100 logs/sec):

import logging
from appsignal_logging import AppSignalNDJSONHandler

handler = AppSignalNDJSONHandler(
    api_key="your_api_key",
    app_name="my_app",
    hostname="web-server-1",
    batch_size=100,        # Send every 100 logs
    flush_interval=5.0     # Or every 5 seconds
)

logger = logging.getLogger()
logger.addHandler(handler)
logger.setLevel(logging.INFO)

logger.info("User logged in", extra={"user_id": 123, "ip": "192.168.1.1"})

Handler Comparison

Feature HTTP Handler NDJSON Handler
Requests per 1000 logs 1000 10-20
Latency ~0.5s 0-5s
Memory usage Low Medium
Network load High Low
Performance Low at >100 logs/sec High
Best for Dev/Low traffic Production/High traffic

Configuration Options

AppSignalHTTPHandler

AppSignalHTTPHandler(
    api_key: str,              # Required: Your AppSignal API key
    app_name: str = None,      # Optional: Application name
    hostname: str = None,      # Optional: Hostname (auto-detected if not provided)
    level: int = logging.NOTSET  # Optional: Minimum log level
)

AppSignalNDJSONHandler

AppSignalNDJSONHandler(
    api_key: str,              # Required: Your AppSignal API key
    app_name: str = None,      # Optional: Application name
    hostname: str = None,      # Optional: Hostname (auto-detected if not provided)
    level: int = logging.NOTSET,  # Optional: Minimum log level
    batch_size: int = 100,     # Optional: Logs per batch (default: 100)
    flush_interval: float = 5.0  # Optional: Flush interval in seconds (default: 5.0)
)

Advanced Usage

Custom Attributes

Add custom attributes to your logs using the extra parameter:

logger.info(
    "Payment processed",
    extra={
        "user_id": 123,
        "amount": 99.99,
        "currency": "USD",
        "transaction_id": "txn_123456"
    }
)

Multiple Handlers

Use different handlers for different log levels:

# Critical errors go immediately via HTTP
critical_handler = AppSignalHTTPHandler(
    api_key="your_api_key",
    app_name="my_app"
)
critical_handler.setLevel(logging.ERROR)

# Info logs batched via NDJSON
info_handler = AppSignalNDJSONHandler(
    api_key="your_api_key",
    app_name="my_app",
    batch_size=200
)
info_handler.setLevel(logging.INFO)

logger = logging.getLogger()
logger.addHandler(critical_handler)
logger.addHandler(info_handler)

Django Integration

# settings.py
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'appsignal': {
            'class': 'appsignal_logging.AppSignalNDJSONHandler',
            'api_key': 'your_api_key',
            'app_name': 'myapp',
            'level': 'INFO',
            'batch_size': 100,
            'flush_interval': 5.0,
        },
    },
    'root': {
        'handlers': ['appsignal'],
        'level': 'INFO',
    },
}

Requirements

  • Python 3.8+
  • httpx >= 0.24.0

Development

# Clone the repository
git clone https://github.com/yourusername/appsignal-logging-python.git
cd appsignal-logging-python

# Install development dependencies
pip install -e ".[dev]"

# Run tests
pytest

# Run tests with coverage
pytest --cov=appsignal_logging --cov-report=html

# Format code
black src tests

# Lint code
ruff check src tests

# Type check
mypy src

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • Built for AppSignal
  • Inspired by the need for efficient Python logging in production environments

Metadata

Release files for appsignal-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 appsignal-logging 0.1.1
File Size Uploaded Provenance
appsignal_logging-0.1.1.tar.gz 12.3 kB Without provenance Details

Built distribution (wheel)

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

Total release size: 19.9 kB

Release files / appsignal_logging-0.1.1.tar.gz

Download URL appsignal_logging-0.1.1.tar.gz
Size 12.3 kB
Tags Source
SHA-256 checksum
How to use checksums
44f3d3153dace232b747781c7c7824396444f06ae8d6bdb44a0840b4e1324cf3
BLAKE2b-256 checksum
How to use checksums
1da83d846559f513d59e2002d437c1cfd889313080a9744226c305c3919886e3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.11

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

Download URL appsignal_logging-0.1.1-py3-none-any.whl
Size 7.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
137b1d274558cac02bb9a338115a7ed23f2d3821c90e3d65f3ff3ccc7ed6b88f
BLAKE2b-256 checksum
How to use checksums
06ad491302b7c105e48bb368c664671c0b89e143255776b83acb70b13c326487
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.11

Security for appsignal-logging 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

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