Skip to main content

Python logging handler for Logstash.

Project description

python-logstash

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

Changelog

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

Contributors

  • Volodymyr Klochan

  • Kiall Mac Innes

  • Alexander Boyd

  • joel-wright

  • Dale O’Brien

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

python-logstash-0.4.5.tar.gz (6.3 kB view details)

Uploaded Source

File details

Details for the file python-logstash-0.4.5.tar.gz.

File metadata

File hashes

Hashes for python-logstash-0.4.5.tar.gz
Algorithm Hash digest
SHA256 f2a8cc501d595dfcfaf9dd63be569f66005a42be51c8798d33b7cb7d1d12a70c
MD5 401462a61563f992894bd65c976e556b
BLAKE2b-256 9cc69055f6b22b95e00d8b58549e8bdce8129e4d0da343304eb46091299853ab

See more details on using hashes here.

Provenance

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