Skip to main content

python-logstash

Python logging handler for Logstash. http://logstash.net/

Changelog

0.6.1
  • Repair FileLogstashHandler’s version parameter useless problem.

0.6.0
  • Add parameters to the class that can customize the formatter.

0.5.0
  • Add File handler.

0.4.7
  • Add couple of sensitive fields to the skip_list

0.4.6
  • Updated field names to match java counterparts supported by logstash crew

0.4.5
  • Allow passing exchange’s routing key to AMQP handler

0.4.4
  • Fixed urllib import python3 compatibility.

  • Added long type to easy_types.

0.4.3
  • Added AMQP handler.

0.4.2
  • Updated README

  • Added tags parameter to handler

0.4.1
  • Added TCP handler.

0.3.1
  • Added support for Python 3

0.2.2
  • Split Handler into Handler and Formatter classes

0.2.1

Installation

Using pip:

pip install python-logstash

Usage

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.setLevel(logging.INFO)
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.')
test_logger.info('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'],
}
test_logger.info('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.setLevel(logging.INFO)
test_logger.addHandler(logstash.AMQPLogstashHandler(host='localhost', version=1))

test_logger.info('python-logstash: test logstash info message.')
try:
    1/0
except:
    test_logger.exception('python-logstash-logger: Exception with stack trace!')

Using with Django

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

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.
      },
      'logstash-file': {
          'level': 'DEBUG',
          'class': 'logstash.FileLogstashHandler',
          'filename': 'logstash.log',
      }
  },
  'loggers': {
      'django.request': {
          'handlers': ['logstash', 'logstash-file'],
          'level': 'DEBUG',
          'propagate': True,
      },
  },
  ...
}

Example Logstash Configuration

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

input {
  udp {
    port => 5959
    codec => json
  }
}
output {
  stdout {
    codec => rubydebug
  }
}

For TCP input you need to change the logstash’s input to tcp and modify django log handler’s class to logstash.TCPLogstashHandler

Metadata

Release files for python-logstash-g 0.7.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 python-logstash-g 0.7.0
File Size Uploaded
python-logstash-g-0.7.0.tar.gz 9.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for python-logstash-g 0.7.0
File Interpreter ABI Platform
python_logstash_g-0.7.0-py3-none-any.whl Python 3 none any Details

Total release size: 19.0 kB

Release files / python-logstash-g-0.7.0.tar.gz

Download URL python-logstash-g-0.7.0.tar.gz
Size 9.0 kB
Tags Source
SHA-256 checksum
How to use checksums
53edf318c90897675c6bb8b3661720be1de1ea6ab1b551907cc8f58198726a4c
BLAKE2b-256 checksum
How to use checksums
4fae3a895aed0bfbfcee14305fb24ec9962de56f11213a0004a55ba0d9d0f83a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.4.2 requests/2.20.0 setuptools/41.0.0 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.6.0

Release files / python_logstash_g-0.7.0-py3-none-any.whl

Download URL python_logstash_g-0.7.0-py3-none-any.whl
Size 10.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
82bb212e8a365de0324f90f40c77a100f7529ead0c82050dc5600e4a5bce8aec
BLAKE2b-256 checksum
How to use checksums
ea779d0c727689f7405edc5546efcf978c4e3c3fe2fb89d158ed98ece4b5f16f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.4.2 requests/2.20.0 setuptools/41.0.0 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.6.0

Release history Release notifications | RSS feed

This release

0.7.0 This release

2 release files

0.6.2

2 release files

0.6.0

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