Skip to main content

An extremely simple logging handler for relaying log events to ntfy

Project description

ntfy-handler

This repo contains an extremely simple logging handler that relays log events to ntfy.

Installation

pip install ntfy-handler

Usage

import logging
from ntfy_handler import NtfyHandler

logger = logging.getLogger()
logging.basicConfig()

nh = NtfyHandler(topic_name="my_topic")
logger.addHandler(nh)

logger.info("Info!")

Advanced Options

In addition to topic_name, the following parameters can be passed to a NtfyHandler during construction:

Parameter Name Type Description Default Value
server_url str The base URL of the ntfy server to utilize. "https://ntfy.sh"
log_level_priority_map Optional[Dict[int, int]] A dict of {log_level: ntfy_priority} items, where log_level is defined as a log_level // 10 * 10, and ntfy_priority is the priority to use when sending the ntfy message. See below
ntfy_headers Optional[Dict[str, Union[str, bytes]]] A dict of headers to be used when sending a ntfy message. None
access_token Optional[str] An access token to use when sending a message, retrieved from the /account page of a ntfy server. None
username Optional[str] A username to use when sending a message None
password Optional[str] A password to use when sending a message None

ntfy_headers

If advanced functionality is desired, custom headers can be passed to each request made by the NtfyHandler with this parameter. See this page for an up-to-date list of the headers that ntfy uses.

The Authorization header may be set here. If it is, access_token, username, and password will not override the manually set value.

The X-Title and X-Priority headers will be overridden for each log record, so setting them here would be ineffectual.

log_level_priority_map

By default, log levels are mapped to their corresponding priorities in ntfy (defined here). However, a custom mapping from log level -> ntfy priority can be defined with the log_level_priority_map parameter when constructing a NtfyHandler.

The default map is as follows:

{
    logging.DEBUG: 1,
    logging.INFO: 2,
    logging.WARNING: 3,
    logging.ERROR: 4,
    logging.FATAL: 5,
}

Levels for log records are truncated to the nearest 10, to normalize them into the above options. With the default configuration, a log record with a level outside of range(9, 60) will be sent with ntfy's default priority (3). To customize this option, provide a dict with keys set to log levels truncated to the nearest 10 and values set to the ntfy priorities to be used for those levels.

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_handler-1.0.4.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

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

ntfy_handler-1.0.4-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file ntfy_handler-1.0.4.tar.gz.

File metadata

  • Download URL: ntfy_handler-1.0.4.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.1.dev0+g94f810c.d20240510 CPython/3.12.5

File hashes

Hashes for ntfy_handler-1.0.4.tar.gz
Algorithm Hash digest
SHA256 75557c7374350b1303aa53ddfdf22774e7fb37e9f50432cc1306f6dfaa8a994f
MD5 8b076b878c504230516e2b9edb79cc4c
BLAKE2b-256 4a32794adf701bb2f13ae70b7710e3c1236d439092a2226203cc1415a02cc1f4

See more details on using hashes here.

File details

Details for the file ntfy_handler-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: ntfy_handler-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.1.dev0+g94f810c.d20240510 CPython/3.12.5

File hashes

Hashes for ntfy_handler-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 f9c06491f4c3276c3bad65bbf54918f4df14e7ce884ba8aebec341241f990bab
MD5 e81ce24a2d96df54fb9c5865a81eb48f
BLAKE2b-256 9d6461eeef9fd108947ef98334fd7eba47b25e4ec3a0333427ecd70c0c40a284

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