Article · 2022-03-25

Python Application Logs: Troubleshooting ELK Integration

Troubleshooting Process

1. Check Filebeat inputs

filebeat.inputs:
  - type: log
    paths:
      - /var/log/my_service/*.log
    fields:
      project: my_service

2. Create a temporary symlink for load testing

ln -s /var/log/my_service-neo /var/log/my_service

3. Trace the source of the "-neo" suffix

Any change to the project name that isn't synchronized across all configuration files becomes a hidden failure point.

Root Cause Analysis

Solution

Two options presented:

We implemented Option B.

Structured Logging on the Python Side

import logging
import logging.config
import json_log_formatter

formatter = json_log_formatter.JSONFormatter()

LOG_CONFIG = {
    "version": 1,
    "formatters": {
        "json": {
            "class": "json_log_formatter.JSONFormatter",
            "format": "%(asctime)s %(levelname)s %(name)s %(message)s"
        }
    },
    "handlers": {
        "file": {
            "class": "logging.handlers.TimedRotatingFileHandler",
            "filename": "/var/log/my_service-neo/app.log",
            "when": "midnight",
            "backupCount": 7,
            "formatter": "json"
        }
    },
    "root": {
        "handlers": ["file"],
        "level": "INFO"
    }
}

logging.config.dictConfig(LOG_CONFIG)
logger = logging.getLogger(__name__)

logger.info({"event": "service_start", "version": "1.2.3"})

Filebeat Dynamic Discovery

filebeat.autodiscover:
  providers:
    - type: kubernetes
      hints.enabled: true
      templates:
        - condition:
            equals:
              kubernetes.labels.app: my_service-neo
          config:
            - type: log
              paths:
                - /var/log/my_service-neo/*.log
              json.keys_under_root: true
              json.add_error_key: true

Logs Persisting to Elasticsearch

Lessons Learned

© 2026 Yuxu Ge ·