Skip to main content

Send logs to New Relic via their Log REST API

Project description

nr-log-handler

A lightweight Python library for sending logs to New Relic via their Log API. No New Relic agent required — pure REST API.

Features

  • Works with Python's built-in logging module as a drop-in logging.Handler
  • Standalone NewRelicLogger with .info(), .error() etc.
  • US and EU region support
  • Sync (immediate) and async (batched, background thread) modes
  • Configurable batch size and flush interval
  • Exponential backoff retry — never crashes your application
  • Global and per-call custom attributes

Requirements

  • Python 3.10+
  • requests

Installation

pip install nr-log-handler

Quick Start

Standalone logger

from newrelic_logger import NewRelicLogger

logger = NewRelicLogger(
    api_key="YOUR_NEW_RELIC_LICENSE_KEY",
    region="us",               # "us" (default) or "eu"
    mode="async",              # "sync" (default) or "async"
    attributes={"service": "my-app", "environment": "production"},
)

logger.info("Application started")
logger.error("Something went wrong", extra_attributes={"request_id": "abc-123"})

# Always call close() on shutdown to flush buffered logs
logger.close()

As a logging.Handler

import logging
from newrelic_logger import NewRelicHandler

handler = NewRelicHandler(
    api_key="YOUR_NEW_RELIC_LICENSE_KEY",
    region="us",
    mode="sync",
    attributes={"service": "my-app"},
)

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("myapp")
logger.addHandler(handler)

logger.info("Hello from standard logging")

Configuration

Parameter Type Default Description
api_key str required New Relic license key
region str "us" "us" or "eu"
mode str "sync" "sync" or "async"
batch_size int 100 Max logs per batch (async only)
flush_interval float 5.0 Seconds between flushes (async only)
timeout int 10 HTTP timeout in seconds
max_retries int 5 Max retry attempts on transient failure
backoff_factor float 0.5 Exponential backoff multiplier
attributes dict None Global attributes on every log entry

Error Handling

The library never raises exceptions from logging calls. On failure it retries up to max_retries times with exponential backoff, then emits a warning via Python's stdlib logging to the newrelic_logger logger and silently drops the batch.

To capture these internal warnings:

import logging
logging.getLogger("newrelic_logger").setLevel(logging.WARNING)

Changelog

See CHANGELOG.md for release history.

Latest: 0.1.1 (2026-03-29)

  • Fixed deprecated license table format in pyproject.toml (SPDX string)
  • Added LICENSE file (MIT)
  • Added GitHub Actions workflow for automated PyPI publishing

License

MIT

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

nr_log_handler-0.1.1.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

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

nr_log_handler-0.1.1-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file nr_log_handler-0.1.1.tar.gz.

File metadata

  • Download URL: nr_log_handler-0.1.1.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for nr_log_handler-0.1.1.tar.gz
Algorithm Hash digest
SHA256 501f110e3fb039b22b056d67db6cc3bb34f47ffbf36f3a0dfb71267570ec0778
MD5 c0e3c070ef7e5411f188e0622dd65751
BLAKE2b-256 9096302030eab1aa4f4fc3de76c4d907da48bc7b66ad49c8cafbef7a41da4936

See more details on using hashes here.

File details

Details for the file nr_log_handler-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: nr_log_handler-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for nr_log_handler-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8adf70978387dc1d5a9f52fe2d93cc36652f686ebbeee57649400cb5138affe3
MD5 d6daad205681582b07a2e7c91f0063de
BLAKE2b-256 8328aa1d14efd30414780111a699af4fa1ff524cfc1023c8bd4b033b73b082ff

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