Django Logstash的日志记录格式 [英] Django logging format for Logstash

查看:310
本文介绍了Django Logstash的日志记录格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将django应用程序配置为以Logstash易于使用的格式写入日志. (灵感来自Node的Winston日志记录程序包)

I'm trying to configure my django application to write logs in a format which is easy for Logstash to consume. (Inspired by the Winston logging package for Node)

Logstash需要一个JSON对象,其日志消息的键为"@message",时间戳为"@timestamp".有谁知道如何格式化Django日志?

Logstash expects a JSON object with the log message in a key "@message" and a timestamp "@timestamp". Does anyone know how to format the Django logs like that?

到目前为止,我有与django文档中类似的内容.

So far I have something similar to what is in the django docs.

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
        }
    },
    'handlers': {
        'file': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': './logs/app.log',
        },
    },
    'loggers': {
        'django.request': {
            'handlers': ['file'],
            'level': 'DEBUG',
            'propagate': True,
        },
    },
}

这将使日志如下:

WARNING 2015-05-22 21:20:19,082 base 3360 4588670976 Not Found: /register/fwq

有人对如何以Logstash格式对消息进行JSON编码有任何建议吗?

Anyone have suggestions on how to JSON encode the message in the Logstash format?

推荐答案

您应该能够使用以下命令捕获Django日志:

You should be able to catch Django logs with the following:

模式:

DJANGO_LOGLEVEL (DEBUG|INFO|ERROR|WARNING|CRITICAL)
DJANGO_LOG %{DJANGO_LOGLEVEL:log_level}\s+%{TIMESTAMP_ISO8601:log_timestamp}\s+%{TZ:log_tz}\s+%{NOTSPACE:logger}\s+%{WORD:module}\s+%{POSINT:proc_id}\s+%{GREEDYDATA:content}

过滤器:

filter {
     if [type] == "django" {
        grok {
             match => ["message", "%{DJANGO_LOG}" ]
        }

        date {
            match => [ "timestamp", "ISO8601", "YYYY-MM-dd HH:mm:ss,SSS"]
            target => "@timestamp"
        }
     }
}

这篇关于Django Logstash的日志记录格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆