Skip to main content

Splunk logger sends log messages to splunk directly from your Python code.

Project description

Splunk logger

A logging handler for Splunk. Lets you send information to Splunk directly from your Python code.

Build Status

Usage

import logging
from splunk_logger import SplunkLogger

ACCESS_TOKEN = '...'
PROJECT_ID = '...'

splunk_logger = SplunkLogger(access_token=ACCESS_TOKEN, project_id=PROJECT_ID)
logging.getLogger('').addHandler(splunk_logger)

logging.error('This is sent to splunk')

After a couple of seconds of waiting for Splunk to process the new information, you should be able to see something like this in the web interface:

{
    data : "This is sent to splunk",
    level : "ERROR",
    line : 1,
    module : "<stdin>"
}

When using the code in a real Python program, and not from the python console, the real line number and module name are used.

Configuration file

It is always a good idea to avoid hardcoded credentials in your source code. The module can fetch the credentials from a YAML file in the current directory or the user’s home. The filename is named .splunk_logger and has the following format:

credentials:
    project_id: ...
    access_token: ...

Once the file is in place, you can use the module as follows:

import logging
from splunk_logger import SplunkLogger

splunk_logger = SplunkLogger()
logging.getLogger('').addHandler(splunk_logger)

logging.error('This is sent to splunk')

Configuration through environment variables

Another project ID and access token source which this module accepts are the SPLUNK_PROJECT_ID and SPLUNK_ACCESS_TOKEN. Once again, you can use them to avoid hard-coding credentials in the source code.

Enhancements

There are a couple of things which could be improved in this module

  • The logger could be refactored to send the messages in an async manner, this will make logging.foo() calls return immediately instead of waiting for the log message to be sent.

  • Send messages in batches

Pull requests are more than welcome!

Reporting bugs

Report your issues and feature requests in Splunk Logger’s issue tracker and I’ll be more than glad to fix them.

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

splunk_logger-0.2.0.tar.gz (5.7 kB view details)

Uploaded Source

File details

Details for the file splunk_logger-0.2.0.tar.gz.

File metadata

File hashes

Hashes for splunk_logger-0.2.0.tar.gz
Algorithm Hash digest
SHA256 7f7b39e6629a09e14589542f9e3a825efcd7e3a7218a785c58129207879d42b1
MD5 83bd7be1240e387cd37704e3d22b3719
BLAKE2b-256 af4d1b756748deae67803694c53e024d53305da84b7f11905d98982efae29901

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page