Python Logstash
python-logstash is a Python logging handler for Logstash. It allows applications to send log messages to a Logstash instance using UDP, TCP, or AMQP protocols. The library is currently at version 0.4.8 and focuses on providing reliable, direct log forwarding, with updates addressing Python 3 compatibility and bug fixes.
Warnings
- gotcha The default `LogstashHandler` uses UDP. If you intend to use TCP for more reliable delivery, you must explicitly use `TCPLogstashHandler`.
- gotcha When using `extra` fields in log records, ensure the dictionary keys do not clash with reserved names used by the Python logging system (e.g., `pathname`, `lineno`, `levelname`).
- gotcha Logstash itself requires proper configuration (e.g., `udp { port => 5959 codec => json }`) to receive messages. Common issues arise from misconfigured Logstash inputs (wrong port, protocol, or codec).
- breaking Version 0.4.8 fixed Python 3 issues with JSON serialization. Earlier 0.4.x versions might have had broken JSON serialization for Python 3 environments, potentially leading to malformed logs.
- gotcha For high-performance or web applications, consider `python-logstash-async` (a fork) if synchronous logging blocking your main thread is an issue. The original `python-logstash` is synchronous.
- deprecated The `distutils` module, used by older setup tools, is deprecated in Python 3.10+ and removed in Python 3.12. While not a direct API change, it can affect installation or packaging processes.
Install
-
pip install python-logstash
Imports
- LogstashHandler
from logstash import LogstashHandler
- TCPLogstashHandler
from logstash import TCPLogstashHandler
- AMQPLogstashHandler
from logstash import AMQPLogstashHandler
Quickstart
import logging
import logstash
import sys
import os
# Configure Logstash host and port (use environment variables for production)
LOGSTASH_HOST = os.environ.get('LOGSTASH_HOST', 'localhost')
LOGSTASH_PORT = int(os.environ.get('LOGSTASH_PORT_UDP', 5959))
test_logger = logging.getLogger('my_logstash_app')
test_logger.setLevel(logging.INFO)
# Add Logstash UDP handler (default behavior)
test_logger.addHandler(logstash.LogstashHandler(LOGSTASH_HOST, LOGSTASH_PORT, version=1))
# Optional: Add Logstash TCP handler (uncomment and adjust port as needed)
# LOGSTASH_TCP_PORT = int(os.environ.get('LOGSTASH_PORT_TCP', 5000))
# test_logger.addHandler(logstash.TCPLogstashHandler(LOGSTASH_HOST, LOGSTASH_TCP_PORT, version=1))
# Log messages
test_logger.info('python-logstash: test logstash info message.')
# Add extra fields to logstash message
extra_data = {
'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_data)
try:
1 / 0
except:
test_logger.exception('python-logstash-app: Exception with stack trace!')
print(f"Logs sent to Logstash at {LOGSTASH_HOST}:{LOGSTASH_PORT} (UDP)")