Skip to main content

logstashpy : python logging handlers for logstash with SSL/TLS support

License image image image image

image

This repository is inspired from original project python-logstash and lot of code and pending PR's are also integrated here as well, some of them like SSL/TLS support. Moreover it is planned to support more serialization formats as supported in logstash codecs. Currently msgpack is integrated.

Some old formatters are shipped but ELK stack now promotes use of ECS. Check out ecs-logging for latest update. For all handlers default formatter is now StdlibFormatter from ecs_logging but you can always chose some other formatter or the old formatters.

Currently no class is exposed with __all__under __init__.py for now as API might change in future.

Installation

To install logstashpy, simply use all time favorite pip and type :

$ pip install logstashpy✨

Usage

>>> from logstash.handlers.tcp_handler import TCPLogstashHandler
>>> from ecs.logger 
>>> host = 'localhost'
>>> logger = logging.getLogger(__name__)
>>> logger.setLevel(logging.INFO)
>>> handler = TCPLogstashHandler(host, 5959, ssl=False, serializer='msgpack')
>>> logger.addHandler(handler)

>>> # add extra field to logstash message
>>> extra = {
>>>     'test_string': 'python version: ' + repr(sys.version_info),
>>>     'test_boolean': True,
>>>     'test_dict': {'a': 1, 'b': 'c'},
>>>     'test_float': 1.23,
>>>     'test_integer': 123,
>>>     'test_list': [1, 2, '3'],
>>> }
>>> logger.info('python-logstash: test extra fields', extra=extra)

SSL/TLS Support

SSL is enabled by default. To disable pass ssl=False in Handler's constructor.

To quickly setup SSL/TLS related certificates follow some commands below:

Generate certificates for Certificate Authority (for self signed certificates)

Generate CA key (kind of like private key for CA, will prompt for password, keep it safe)

openssl genrsa -des3 -out localCA.key 2048

Generate CA pem file (kind of like public key for CA)

openssl req -x509 -new -nodes -key localCA.key -sha256 -days 1024 -out localCA.pem

Now we have a local certificate authority ready to sign some certificates.

Lets generate some private key along with CSR (Certificate Signing Request) for our local logstash server which will be signed by our local certificate authority. You can pass some other configs here but left for simplicity.

openssl req -new -sha256 -nodes -out logserver.csr -newkey rsa:2048 -keyout logserver.key

Let's submit our CSR to our local CA and get out certificate (as protected, you will be prompted for the password as set before)

openssl x509 -req -in server.csr -CA localCA.pem -CAkey localCA.key -CAcreateserial -out logserver.crt -days 1000 -sha256

You will get a logserver.crt which logstash server can present to its clients valid for 1000 days.

Python Version Support

logstashpy supports python3

Sample Logstash Configuration

Example Logstash Configuration (logstash.conf) for Receiving Events from logstashpy via TCP (omit ssl related fields if not required):

input {
  tcp {
    port => 5959
    codec => json
    ssl_enable => true
    ssl_cert => "/path/to/server.crt"
    ssl_key => "/path/to/server.key"
    ssl_verify => false
  }
}
output {
   stdout {
     codec => rubydebug
   }
}

Documentation

Coming Soon.

Release files for logstashpy 0.0.1

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

Source distribution (sdist)

Source distribution for logstashpy 0.0.1
File Size Uploaded
logstashpy-0.0.1.tar.gz 3.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for logstashpy 0.0.1
File Interpreter ABI Platform
logstashpy-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size:11.4 kB

Release files / logstashpy-0.0.1.tar.gz

Download URL logstashpy-0.0.1.tar.gz
Size 3.6 kB
Tags Source
SHA-256 checksum
How to use checksums
d79208c806f209b804c32f894cba82f46bc0b811348faff08bedc7fcf3b0f520
BLAKE2b-256 checksum
How to use checksums
bf07738eea977c38bb3efd7162b3521d1577ad13b7795cad0165d529b1e08a70
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.8

Release files / logstashpy-0.0.1-py3-none-any.whl

Download URL logstashpy-0.0.1-py3-none-any.whl
Size 7.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a7361b126657161acd558fd54734a047558a61eb17a9031f2ad2e73e5298ef64
BLAKE2b-256 checksum
How to use checksums
99aeac5fccbcc22496d944c6cf14fb14d6efc6263461b55a16cff1e546fe4d4f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.8

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files

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