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.10.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.10-py3-none-any.whl (63.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ntfy_logging-1.0.10.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.10.tar.gz
Algorithm Hash digest
SHA256 8a03b9f5279a10e0393d7a8d3547c2306844b76d994d86222a89064f81d99440
MD5 6691a1bb9b67273c3192fdeecde17d69
BLAKE2b-256 fe5efcf87e6ae67491f29d4a3b04af28ce56e65eb0714d1609bacb8f469fd1e8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ntfy_logging-1.0.10-py3-none-any.whl
  • Upload date:
  • Size: 63.5 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.10-py3-none-any.whl
Algorithm Hash digest
SHA256 2435fb9bc3a74b5eb381f39203086bea58258eb2c46efb7f019e01772157c0f7
MD5 3a41a56839596d5ab3b019a988af8272
BLAKE2b-256 cfdb326d6d46e7256f0feedf2bc3997c97b436dc5e7033adda8f692ff8282927

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