Skip to main content

A Python logging handler for sending logs to ntfy

Project description

ntfy-logging

A Python logging handler for sending logs to ntfy, a simple notification service.

Installation

Poetry

poetry add ntfy-logging

Pip

pip install ntfy-logging

uv

uv add ntfy-logging

Basic Usage

import logging
from ntfy_logging import NtfyHandler

# Create logger
logger = logging.getLogger("my_app")
logger.setLevel(logging.DEBUG)

# Create ntfy handler (only send WARNING or higher to ntfy)
ntfy_handler = NtfyHandler(
    topic_name = "your-topic",  # Replace with your ntfy topic
    server_url = "https://ntfy.sh",  # Optional: your ntfy server
    access_token = "your-access-token",  # Optional: for private servers
    include_traceback = True,  # Include exception tracebacks in notifications
)
ntfy_handler.setLevel(logging.WARNING)  # Only WARNING or higher go to ntfy
logger.addHandler(ntfy_handler)

# Log messages
logger.debug("This is a debug message (won't go to ntfy)")
logger.info("This is an info message (won't go to ntfy)")
logger.warning("This is a warning message (will go to ntfy)")
logger.error("This is an error message (will go to ntfy)")
logger.critical("This is a critical message (will go to ntfy)")

# Log exceptions with traceback
try:
    1 / 0
except Exception:
    logger.exception("An error occurred")

Advanced Usage

The NtfyHandler class supports many customization options:

import logging
from ntfy_logging import NtfyHandler

# Custom priority mapping
priority_mapping = {
    logging.CRITICAL: 5,  # Max
    logging.ERROR: 4,     # High
    logging.WARNING: 3,   # Default
    logging.INFO: 2,      # Low
    logging.DEBUG: 1,     # Min
}

# Custom tags mapping
tag_mapping = {
    5: "rotating_light",
    4: "exclamation",
    3: "warning",
    2: "information_source",
    1: "incoming_envelope",
}

# Authentication (if your ntfy server requires it)
# For bearer token:
# credentials = ntfy_api.Credentials(bearer="your-auth-token")
# For basic auth:
# credentials = ntfy_api.Credentials(basic=("username", "password"))

logger = logging.getLogger("my_app")

# Create handler with custom configuration
ntfy_handler = NtfyHandler(
    topic_name = "your-topic",
    server_url = "https://ntfy.sh",  # Change if using a self-hosted server
    access_token = "your-access-token",  # Or use credentials=...
    log_level_priority_map = priority_mapping,
    priority_tags_map = tag_mapping,
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'),
    click = "https://your-app-dashboard.com",  # URL opened when notification is clicked
    include_traceback = True,  # Include exception tracebacks in notifications
)
ntfy_handler.setLevel(logging.WARNING)
logger.addHandler(ntfy_handler)

Features

  • Automatic mapping of logging levels to ntfy priorities
  • Custom emoji tags for different log levels
  • Support for exception logging with tracebacks
  • Authentication support for private ntfy servers
  • Customizable message formatting
  • Support for click actions
  • Context manager support for clean resource handling

CI

This project uses GitHub Actions to run tests with pytest on every push and pull request.

Credits

This project vendors (bundles) the ntfy-api Python package, licensed under the Apache License 2.0. The ntfy_api source code is included directly in this package; you do not need to install it separately.

Note: ntfy-api is bundled because PyPI does not allow dependencies from git repositories and the PyPI version of ntfy-api is outdated.

See the NOTICE file and https://www.apache.org/licenses/LICENSE-2.0 for details.

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

ntfy_logging-1.0.11.tar.gz (57.8 kB view details)

Uploaded Source

Built Distribution

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

ntfy_logging-1.0.11-py3-none-any.whl (63.6 kB view details)

Uploaded Python 3

File details

Details for the file ntfy_logging-1.0.11.tar.gz.

File metadata

  • Download URL: ntfy_logging-1.0.11.tar.gz
  • Upload date:
  • Size: 57.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.10 Linux/6.11.0-1015-azure

File hashes

Hashes for ntfy_logging-1.0.11.tar.gz
Algorithm Hash digest
SHA256 1af6558cca3745620c0804188690e4ba5995d6bdf2280cd4f5085c55c1189590
MD5 227db5025c92f72840489d76e1957482
BLAKE2b-256 16b306b3f1fda11b0b36812572067a4bd3328ef2b678c595c99ed26f93d89fac

See more details on using hashes here.

File details

Details for the file ntfy_logging-1.0.11-py3-none-any.whl.

File metadata

  • Download URL: ntfy_logging-1.0.11-py3-none-any.whl
  • Upload date:
  • Size: 63.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.10 Linux/6.11.0-1015-azure

File hashes

Hashes for ntfy_logging-1.0.11-py3-none-any.whl
Algorithm Hash digest
SHA256 9fd1f4104bd6b19205a8330c3106b56982b6c1a309d219c55d092350b57636f3
MD5 a17660769ce2fdd8563fac55616a2e4e
BLAKE2b-256 a53f27d9b7ac94f37f0ff3bbaf8813984e006fe8c4600173cdd30015ce0567da

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