Registry / observability / splunk-handler

splunk-handler

JSON →
library3.0.0pypypi✓ verified 22d ago

The `splunk-handler` library provides a Python logging handler for sending log events to a Splunk Enterprise instance. It leverages the Splunk HTTP Event Collector (HEC) for data ingestion. The current version is 3.0.0, and the project maintains an active release cadence, addressing bug fixes, new features, and Python compatibility updates.

pip install splunk-handler
INSTALL
IMPORT
SIG · SPLUNK-HANDLER
S
splunk-handler
observabilitypythonv3.0.0
Install
2.2s avg
Import
349ms
Disk
19MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v3.0.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.366s · 21.1MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.2s · import 0.332s · 22MB
19MB installed
● package 19MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

SplunkHandler
from splunk_handler import SplunkHandler
force_flush
from splunk_handler import force_flush
Specifically needed for environments like AWS Lambda to ensure logs are flushed before process termination.

This quickstart demonstrates how to configure and use `splunk-handler` to send log messages to Splunk Enterprise via the HTTP Event Collector. It uses environment variables for sensitive connection details and includes an example of `force_flush` for critical environments like AWS Lambda. Ensure your Splunk HEC is properly configured and accessible from where this code runs.

import logging import os from splunk_handler import SplunkHandler, force_flush # Configure Splunk HEC details via environment variables SPLUNK_HOST = os.environ.get('SPLUNK_HOST', 'splunk.example.com') SPLUNK_PORT = os.environ.get('SPLUNK_PORT', '8088') SPLUNK_TOKEN = os.environ.get('SPLUNK_TOKEN', 'YOUR_SPLUNK_HEC_TOKEN') SPLUNK_INDEX = os.environ.get('SPLUNK_INDEX', 'main') # Initialize the SplunkHandler try: splunk_handler = SplunkHandler( host=SPLUNK_HOST, port=SPLUNK_PORT, token=SPLUNK_TOKEN, index=SPLUNK_INDEX, protocol='https', # Use 'http' if SSL is not configured verify=True, # Set to False if using self-signed certs and not providing CA flush_interval=1.0 # Send logs every 1 second for demonstration ) # Add the handler to the root logger logging.getLogger('').addHandler(splunk_handler) logging.getLogger('').setLevel(logging.INFO) # Example log messages logging.info('Hello from splunk-handler!') logging.warning('This is a warning message.') logging.error('An error occurred: %s', 'something went wrong') # For environments like AWS Lambda, ensure logs are flushed before exiting. # In a typical application, the atexit hook handles this, but explicit call might be needed. force_flush() print('Logs sent to Splunk (check your Splunk instance).') except Exception as e: print(f"Failed to configure Splunk handler or send logs: {e}") print("Please ensure SPLUNK_HOST, SPLUNK_PORT, SPLUNK_TOKEN, and SPLUNK_INDEX are correctly set.") print("Also, verify that Splunk HEC is enabled and accessible.")
Debug
Known issues
breakingVersion 3.0.0 removed official support for Python 2.7, 3.4, and 3.5. Applications running on these Python versions must either stay on an older `splunk-handler` version (e.g., <3.0.0) or upgrade their Python runtime.
fix
Upgrade Python to 3.6+ or pin `splunk-handler` version to <3.0.0.
affects: <3.0.0 to 3.0.0
gotchaIn serverless environments like AWS Lambda, where the main thread can terminate unexpectedly, logs might be dropped. To prevent this, explicitly call `splunk_handler.force_flush()` as the last action in your Lambda handler to ensure all queued logs are sent.
fix
Call `from splunk_handler import force_flush` and `force_flush()` at the end of your main application or function execution.
affects: All versions
gotchaThe `SplunkHandler` requires a Splunk Enterprise server with the HTTP Event Collector (HEC) enabled and configured. Misconfiguration of HEC (e.g., incorrect token, port, or protocol) will result in logs not being ingested by Splunk.
fix
Verify HEC configuration in Splunk, including token, port, and allowed indexes. Ensure network connectivity between the application and the Splunk HEC endpoint.
affects: All versions
gotchaIf your Splunk instance uses a self-signed SSL certificate, `verify=True` (the default) will cause connection errors. You may need to set `verify=False` or, preferably, provide the certificate authority (CA) bundle to `requests` for proper SSL verification.
fix
Set `verify=False` (less secure) or configure `requests` to trust your custom CA certificate by passing `verify='/path/to/your/ca-bundle.pem'` to the handler.
affects: All versions
gotchaWhile race conditions for large payloads were fixed in v2.2.2, general asynchronous logging can still lead to lost events if the application exits abruptly without proper shutdown. Ensure the `flush_interval` is appropriate for your traffic and application lifecycle.
fix
Ensure graceful shutdown of your application, allowing the handler to flush its queue. Consider using `splunk_handler.wait_until_empty()` or `splunk_handler.force_flush()` at application exit points for critical logs.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'splunk_handler'
The `splunk-handler` package has not been installed in the current Python environment.
fix
pip install splunk-handler
requests.exceptions.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed
The Python environment cannot verify the SSL certificate presented by the Splunk HEC endpoint, often due to self-signed certificates or missing root CAs.
fix
Set `verify=False` in the `SplunkHandler` configuration (not recommended for production) or provide the path to a custom CA bundle using the `ca_certs` parameter: `SplunkHandler(..., verify=True, ca_certs='/path/to/your/ca_bundle.pem')`.
requests.exceptions.ConnectionError: Failed to establish a new connection: [Errno 111] Connection refused
The `splunk-handler` cannot establish a network connection to the specified Splunk HEC endpoint, possibly due to an incorrect host/port, firewall rules, or the Splunk instance being down.
fix
Verify the `host` and `port` configured for `SplunkHandler`, check network connectivity to the Splunk server (e.g., using `ping` or `telnet`), and ensure Splunk's HEC is enabled and listening.
Failed to send event to Splunk: HTTP Error 401: Unauthorized
The Splunk HEC token provided to `SplunkHandler` is incorrect, expired, or lacks the necessary permissions to send data to the specified index.
fix
Verify the HEC `token` in your `SplunkHandler` configuration against your Splunk HEC setup, and ensure it is valid and has appropriate write access to the specified index in Splunk.
Upgrade
Version history
3.0.0latest on PyPI · released Aug 17, 2021
Audit
Dependencies
requestsrequiredUsed for making HTTP requests to the Splunk HTTP Event Collector.
Agent activity
25 hits · last 30 days
node
20
Bingbot
1
OpenAI (training)
1
Resources