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)

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.0.tar.gz (9.4 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.0-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: nr_log_handler-0.1.0.tar.gz
  • Upload date:
  • Size: 9.4 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.0.tar.gz
Algorithm Hash digest
SHA256 6316560f99cb6e3fb0c48f4452019c139208a13ce5b0bec7314b990e7dfdb596
MD5 2c813b8af8c8067692938c744f2e77c0
BLAKE2b-256 390bbada547ec22a1b845364a6a3904409f88937479c8e7cd10f101dbe25de2a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: nr_log_handler-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8ac8716bab549482f839349bf1f811cdf276df3b4e9bd7955dc831c380b40798
MD5 514acf4b999ebdcab88f61c30d9a2a15
BLAKE2b-256 0fb12c3d52ac5d0a6aa0b1d1274ce0e0a16698da4cc9a155d8dc3ebc6302441f

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