CMRESHandler

1.0.0 · abandoned · verified Thu Apr 16

CMRESHandler is a Python library that provides an Elasticsearch logging appender compatible with the standard `logging` library. It enables logging application events to Elasticsearch in JSON format, supporting various authentication types (Basic, Kerberos, AWS Signed) and flexible index naming frequencies (daily, weekly, monthly, yearly). The library is currently at version 1.0.0, released in 2017. Although functional, the project appears to be unmaintained, with a community fork existing due to its lack of active development.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to configure and use CMRESHandler with basic authentication (if environment variables are set) to send log messages to an Elasticsearch instance. Logs will include additional custom fields and exceptions will be sent with full tracebacks. The `flush_frequency_in_sec` is set to 1 for quick testing, but should be adjusted for production.

import logging
import os
from cmreslogging.handlers import CMRESHandler

# Configure basic logging
log = logging.getLogger("PythonTest")
log.setLevel(logging.INFO)

# Elasticsearch host configuration
ES_HOST = os.environ.get('ES_HOST', 'localhost')
ES_PORT = int(os.environ.get('ES_PORT', 9200))
ES_INDEX_NAME = os.environ.get('ES_INDEX_NAME', 'python_app_logs')

# Optional: Basic Auth details
ES_USER = os.environ.get('ES_USER')
ES_PASSWORD = os.environ.get('ES_PASSWORD')

hosts = [{'host': ES_HOST, 'port': ES_PORT}]
auth_type = CMRESHandler.AuthType.NO_AUTH
auth_details = None

if ES_USER and ES_PASSWORD:
    auth_type = CMRESHandler.AuthType.BASIC_AUTH
    auth_details = (ES_USER, ES_PASSWORD)

try:
    handler = CMRESHandler(
        hosts=hosts,
        auth_type=auth_type,
        auth_details=auth_details,
        es_index_name=ES_INDEX_NAME,
        es_additional_fields={'App': 'MyAppName', 'Environment': 'Dev'},
        # Ensure flush frequency is reasonable for testing
        flush_frequency_in_sec=1
    )
    log.addHandler(handler)

    log.info("This is an info statement that will be logged into Elasticsearch.")
    log.warning("A warning message from the application.")
    try:
        1 / 0
    except ZeroDivisionError:
        log.exception("An exception occurred, should be logged with traceback.")

    print("Log messages sent to Elasticsearch (check your ES_INDEX_NAME in Elasticsearch).")
    # In a real application, you might need a small delay or explicit flush
    # if the application exits immediately after logging.
    # For this quickstart, we rely on flush_frequency_in_sec.

except Exception as e:
    print(f"Failed to initialize CMRESHandler or send logs: {e}")
    print("Ensure Elasticsearch is running and accessible at {ES_HOST}:{ES_PORT}.")

view raw JSON →