1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

django log を elasticsearch に入れる python-logstash 使い方

Last updated at Posted at 2021-04-02

これも英語の文献があまりなかったので書いてみた
本家も記述が不親切だった

本家 python-logstash

django を用意する

mkdir django-logstash
cd django-logstash/
python -m venv venv
. venv/bin/activate
python -m pip install Django
django-admin startproject mysite
cd mysite
python manage.py runserver
ctrl + c
pip install python-logstash
vim mysite/settings.py
python manage.py runserver

settings.pyがハマりポイントです。ネットの拾い物、なかなか動かなかった。
django.request だと飛んでこず、django.serverでやっときた。

message_type: django も大事ぽい

1行目の version:1 が必須です。

mysite/settings.py
LOGGING = {
  'version': 1,
  'handlers': {
      'logstash': {
          'level': 'DEBUG',
          'class': 'logstash.TCPLogstashHandler',
          'host': 'xx.xx.xx.xx',
          'port': 9600, # Default value: 5959
          'version': 1, # Version of logstash event schema. Default value: 0 (for backward compatibility of the library)
          'message_type': 'django',  # 'type' field in logstash message. Default value: 'logstash'.
          'fqdn': False, # Fully qualified domain name. Default value: false.
          'tags': ['django.request'],# list of tags. Default: None.
      },
  },
  'loggers': {
      'django.server': {
          'handlers': ['logstash'],
          'level': 'DEBUG',
      },
  },
}

Thank you:

ブラウザで見えればok
http://localhost:8000/

logstashを用意する

configを先に作る

mkdir config 
config/logstash.yml
http.host: "0.0.0.0"
xpack.monitoring.elasticsearch.url: https://your.elasticsearch.example.com:port
xpack.monitoring.elasticsearch.username: xx
xpack.monitoring.elasticsearch.password: xx
xpack.monitoring.enabled: true
log.level: trace

vim config/logstash-django.conf

config/logstash-django.conf
input {
  tcp {
    port => 9600
    codec => json
  }
}
output {
    elasticsearch {
      hosts => ["https://your.elasticsearch.com"]
      user => ["xx"]
      password => ["xx"]
      index => "djangotest"
      document_type => "%{type}"
    }
   stdout {
      codec => rubydebug {metadata => true }
  }
}

起動する。1minくらいかかるから気長にね。sucessfully opened port 9600 みたいのが出ればokなはず。djangoからのrequestが無事飛んでくれば、consoleが動きます。

docker-compose.yml
version: '3'

services:
  logstash:
    restart: always
    container_name: logstash
    image: docker.elastic.co/logstash/logstash:6.8.6
    ports:
      - 9600:9600
    volumes:
      - ./config:/config
    logging:
      driver: "json-file"
      options:
        max-size: "3M"
        max-file: "1"

    command: logstash -r -f /config/*.conf

docker image manual
https://www.elastic.co/guide/en/logstash/6.3/docker-config.html

elasticsearch / kibana は適当に用意してください。

これとか参考になるかも
https://qiita.com/uturned0/items/584686262be1aa8a336c

実行

django に適当にアクセスしてみる
http://localhost:8000/
http://localhost:8000/unknwon-path

esにデータが入ればok. ↓はkibanaでデータを見た図。

unknown-pathへのアクセスが status 404 で入ってます。

image.png

django その他のログ

ここにいろいろありそう。(試してない)

1
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?