Skip to main content
Python Software Foundation 20th Year Anniversary Fundraiser  Donate today!

Python logging handler for Logstash.

Project description


Python logging handler for Logstash.


  • Updated field names to match java counterparts supported by logstash crew
  • Allow passing exchange’s routing key to AMQP handler
  • Fixed urllib import python3 compatibility.
  • Added long type to easy_types.
  • Added AMQP handler.
  • Updated README
  • Added tags parameter to handler
  • Added TCP handler.
  • Added support for Python 3
  • Split Handler into Handler and Formatter classes


Using pip:

pip install python-logstash


LogstashHandler is a custom logging handler which sends Logstash messages using UDP.

For example:

import logging
import logstash
import sys

host = 'localhost'

test_logger = logging.getLogger('python-logstash-logger')
test_logger.addHandler(logstash.LogstashHandler(host, 5959, version=1))
# test_logger.addHandler(logstash.TCPLogstashHandler(host, 5959, version=1))

test_logger.error('python-logstash: test logstash error message.')'python-logstash: test logstash info message.')
test_logger.warning('python-logstash: test logstash warning message.')

# 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'],
}'python-logstash: test extra fields', extra=extra)
When using extra field make sure you don’t use reserved names. From Python documentation.
“The keys in the dictionary passed in extra should not clash with the keys used by the logging system. (See the Formatter documentation for more information on which keys are used by the logging system.)”

To use the AMQPLogstashHandler you will need to install pika first.

pip install pika

For example:

import logging
import logstash

test_logger = logging.getLogger('python-logstash-logger')
test_logger.addHandler(logstash.AMQPLogstashHandler(host='localhost', version=1))'python-logstash: test logstash info message.')
    test_logger.exception('python-logstash-logger: Exception with stack trace!')

Using with Django

Modify your to integrate python-logstash with Django’s logging:

  'handlers': {
      'logstash': {
          'level': 'DEBUG',
          'class': 'logstash.LogstashHandler',
          'host': 'localhost',
          'port': 5959, # Default value: 5959
          'version': 1, # Version of logstash event schema. Default value: 0 (for backward compatibility of the library)
          'message_type': 'logstash',  # 'type' field in logstash message. Default value: 'logstash'.
          'fqdn': False, # Fully qualified domain name. Default value: false.
          'tags': ['tag1', 'tag2'], # list of tags. Default: None.
  'loggers': {
      'django.request': {
          'handlers': ['logstash'],
          'level': 'DEBUG',
          'propagate': True,


Example Logstash Configuration (logstash.conf) for Receiving Events from python-logstash is:

input {
  tcp {
    port => 5000
    codec => json
output {
  stdout {
    codec => rubydebug

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for python-logstash, version 0.4.6
Filename, size File type Python version Upload date Hashes
Filename, size python-logstash-0.4.6.tar.gz (6.5 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page