Skip to main content

Installation

pip install splunk-hec-handler

Features

  1. Log messages to Splunk via HTTP Event Collector (HEC). See Splunk HEC Documentation
  2. All messages are logged as '_json' sourcetype by default.
  3. A dictionary with 'log_level' and 'message' keys are constructed for logging records of type string.

String log record representation in Splunk

  1. Dictionary objects are preserved as JSON.

Dictionary log record representation in Splunk

  1. If log record (dict) does not contains a 'time' field, one is added with the value set to current time.

Examples

Basic

import logging
from splunk_hec_handler import SplunkHecHandler
logger = logging.getLogger('SplunkHecHandlerExample')
logger.setLevel(logging.DEBUG)

# If using self-signed certificate, set ssl_verify to False
# If using http, set proto to http
splunk_handler = SplunkHecHandler('splunkfw.domain.tld',
                    'EA33046C-6FEC-4DC0-AC66-4326E58B54C3',
                    port=8888, proto='https', ssl_verify=True,
                    source="HEC_example")
logger.addHandler(splunk_handler)

Following should result in a Splunk entry with _time set to current timestamp.

logger.info("Testing Splunk HEC Info message")

Basic Example

Following should result in a Splunk entry of Monday, 08/06/2018 4:33:43 AM, and contain two custom fields (color, api_endpoint). Custom fields can be seen in verbose mode.

dict_obj = {'time': 1533530023, 'fields': {'color': 'yellow', 'api_endpoint': '/results'},
                    'user': 'foobar', 'app': 'my demo', 'severity': 'low', 'error codes': [1, 23, 34, 456]}
logger.error(dict_obj)

Fields Example

⚠️ In order to use custom fields, 'sourcetype' property must be specified in the event and sourcetype definition must enable indexed field extractions.

See http://dev.splunk.com/view/event-collector/SP-CAAAE6P for 'fields'

Advanced

Using 'fields', many of the metadata fields associated with an event can be changed from the default. Additionally, new fields, which are not part of the event, can be also added.

In the following example, we are sending events to two different indexes (see "Select Allowed Indexes (optional)" setting) and overriding 'host', 'source', 'sourcetype' fields, while adding some new fields ('color', 'api_endpoint').

import logging
from splunk_hec_handler import SplunkHecHandler

logger = logging.getLogger('SplunkHecHandlerExample')
logger.setLevel(logging.DEBUG)

stream_handler = logging.StreamHandler()
stream_handler.level = logging.DEBUG
logger.addHandler(stream_handler)

token = "EA33046C-6FEC-4DC0-AC66-4326E58B54C3'
splunk_handler = SplunkHecHandler('splunkfw.domain.tld',
                                 token, index="hec",
                                 port=8080, proto='https', ssl_verify=False
                                 source="evtx2json", sourcetype='xxxxxxxx_json')
logger.addHandler(splunk_handler)


dict_obj = {'fields': {'color': 'yellow', 'api_endpoint': '/results', 'host': 'app01', 'index':'hec'},
            'user': 'foobar', 'app': 'my demo', 'severity': 'low', 'error codes': [1, 23, 34, 456]}
logger.info(dict_obj)

log_summary_evt = {'fields': {'index': 'adhoc', 'sourcetype': '_json', 'source': 'adv_example'}, 'exit code': 0, 'events logged': 100}
logger.debug(log_summary_evt)

Advanced Fields Example

Todo

  1. Event acknowledgement support

Metadata

Release files for splunk-hec-handler 1.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for splunk-hec-handler 1.2.0
File Size Uploaded Provenance Actions
splunk_hec_handler-1.2.0.tar.gz 7.0 kB Without provenance Details for splunk_hec_handler-1.2.0.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for splunk-hec-handler 1.2.0
File Interpreter ABI Platform Actions
splunk_hec_handler-1.2.0-py3.11.egg Legacy Egg format - - Details for splunk_hec_handler-1.2.0-py3.11.egg
splunk_hec_handler-1.2.0-py2.py3-none-any.whl Python 3, Python 2 none any Details for splunk_hec_handler-1.2.0-py2.py3-none-any.whl

Total release size: 25.6 kB

Release files / splunk_hec_handler-1.2.0.tar.gz

Download URL splunk_hec_handler-1.2.0.tar.gz
Size 7.0 kB
Tags Source
SHA-256 checksum
How to use checksums
69d5284f245cf0ce48e55b2b19bfa8d5bde003d6e518403bb8fa986a796f61a5
BLAKE2b-256 checksum
How to use checksums
67f8ebdee911502f7dc21be0d253083ce89e18dd0addcb53f2d8cd11e0010743
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.9

Release files / splunk_hec_handler-1.2.0-py3.11.egg

Download URL splunk_hec_handler-1.2.0-py3.11.egg
Size 11.5 kB
Tags Egg
SHA-256 checksum
How to use checksums
0ff30a9ee570cf34c749d7cf6db09b27b73ff18047c97f96e961069b1504af19
BLAKE2b-256 checksum
How to use checksums
add4708403d3b86abaa25ac3913a1a9b6c41dd189acea39cf93c470504584282
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.9

Release files / splunk_hec_handler-1.2.0-py2.py3-none-any.whl

Download URL splunk_hec_handler-1.2.0-py2.py3-none-any.whl
Size 7.1 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
789d3a2960b31bdeb0de102a4f823dde13918ed6be136626247b1160e9d4de63
BLAKE2b-256 checksum
How to use checksums
a74c5b9ba26959b5c44da62fd2809b6a9fcdd49accc596ba9b65b244e7542624
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.9

Security for splunk-hec-handler 1.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page