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.
- ntfy-api: https://github.com/tanrbobanr/ntfy-api
- Copyright © ntfy-api authors
See the NOTICE file and https://www.apache.org/licenses/LICENSE-2.0 for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ntfy_logging-1.0.9.tar.gz.
File metadata
- Download URL: ntfy_logging-1.0.9.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0709a4ab8eac897d7b7c54474597c3e0ec9163efadca8bb12d673e79e2348226
|
|
| MD5 |
5fdaac33beec3be278b8b6a72d2fad91
|
|
| BLAKE2b-256 |
1819d16d3c532f7bb416569bc3bafa6227a37ad9ff07bcd6c10ae3cc66bc9fbf
|
File details
Details for the file ntfy_logging-1.0.9-py3-none-any.whl.
File metadata
- Download URL: ntfy_logging-1.0.9-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
044cb38ef3c8cecebd40b36a0ec7f3b2bd6cdae5131d6a1b9e711f1660b7ae26
|
|
| MD5 |
5bdea1a640722124340926ec8ba6a7ed
|
|
| BLAKE2b-256 |
7a0913bb14596b60b928f2d7e9d7fc9bfc43a43ed33f16f857433d0d0249b035
|