eht16 / python-logstash-async

Python logging handler for sending log events asynchronously to Logstash.
MIT License
182 stars 52 forks source link

Not working on django #61

Closed orvillelim closed 2 years ago

orvillelim commented 3 years ago

I tried to integrate the library with django backend and seems its not working base on documentation

What I tried so far:

LOGGING = {
  'formatters': {
      'logstash': {
          '()': 'logstash_async.formatter.DjangoLogstashFormatter',
          'message_type': 'python-logstash',
          'fqdn': False, # Fully qualified domain name. Default value: false.
          'extra_prefix': 'dev', #
          'extra': {
              'environment': 'local'
          }
      },
  },
  'handlers': {
      'logstash': {
          'level': 'DEBUG',
          'class': 'logstash_async.handler.AsynchronousLogstashHandler',
          'transport': 'logstash_async.transport.TcpTransport',
          'host': 'my.remote.host', 
          'port': 5000,
          'ssl_enable': False,
          'ssl_verify': False,
          'database_path': 'memory',
      },
  },
  'loggers': {
      'django.request': {
          'handlers': ['logstash'],
          'level': 'DEBUG',
          'propagate': True,
      },
  },
}

Logstash conf:

input {
stdin {}
    tcp {
        port => 5000
        codec => json
    }
    gelf {
        type => docker
        port => 12201
    }

}

## Add your filters / logstash plugins configuration here

output {
stdout {}
        elasticsearch {
                hosts => "elasticsearch:9200"
        }
}

I also posted the question on Stackoverflow: https://stackoverflow.com/questions/65712591/python-logstash-and-python-logstash-async-not-working-with-django

Details:

Python Version: 3.7.2 Django version: 2.1.3 Re-created django project:

Python Version: 3.7.9 Django Version: 3.1.5

OS: elementary OS 5.1.7 Hera Built on Ubuntu 18.04.4 LTS Linux 5.4.0-60-generic

eht16 commented 3 years ago

Except for the missing version key in your LOGGING config, it worked for me fine.

I suggest to add a root logger (to catch all logs or at least LogProcessingWorker) to your config, so also messages of other loggers are handled, e.g.:

  'handlers': {
        'root': {
            'handlers':['console', 'logstash'],
            'level': 'DEBUG',
            'propagate': False,
        },
    ...

Not related to the problem but anyway not what you probably want to achieve:

     'database_path': 'memory',

actually uses a SQLite database named "memory" (you should find it in the current working directory). To use an in-memory database use: 'database_path': None,

eht16 commented 2 years ago

Closing on missing response. Re-open if the problem persists.