Logstash将日志信息转换为字符串

时间:2019-07-14 10:11:18

标签: python elasticsearch logstash kibana elastic-stack

我已按照here中所述成功通过docker启动了ELK。然后我实现了一个测试记录器类,以使用此堆栈,如下所示:

import logging
from logstash_async.handler import AsynchronousLogstashHandler


class ELKLogger:
    def __init__(self, logger_name, host, port):
        self.logger = logging.getLogger('python-logstash-logger')
        self.logger.setLevel(logging.INFO)
        self.logger.addHandler(
            AsynchronousLogstashHandler(host=host, port=port, database_path='logstash.db')
        )

    def info(self, message, **extra):
        self.logger.info(message, extra=extra)


if __name__ == "__main__":
    import random

    logger = ELKLogger('elk_logger', host='localhost', port=5000)
    logger.info('info test1', psuccessfull=True, pname='pname', prate=random.random())

代码工作正常,它发送我的日志数据,但麋鹿将我的所有日​​志存储为字符串形式的message属性,但我想将其存储为对象。

示例存储的日志:

  

{           “ _index”:“ logstash”,           “ _type”:“ _doc”,           “ _id”:“ 3rDy72sBBPPgf4IYzCkZ”,           “ _score”:1.0,           “_资源” : {             “主机”:“网关”,             “ message”:“”“ {” type“:” python-logstash“,” @timestamp“:” 2019-07-14T10:06:34.587Z“,” extra“:{” line“:14,” prate“ :0.6982092931167561,“ process_name”:“ MainProcess”,“ psuccessfull”:true,“ path”:“ main.py”,“ interpreter_version”:“ 3.5.2”,“ pname”:“ pname”,“ logstash_async_version”:“ 1.5.1“,”解释器“:” /home/sina/Desktop/logstash-test/venv3.5/bin/python3“,” func_name“:”信息“,” logger_name“:” python-logstash-logger“, “ thread_name”:“ MainThread”},“ level”:“ INFO”,“ program”:“ main.py”,“ pid”:28797,“ logsource”:“ HS-Shaya”,“ message”:“ info test1 “,”主机“:” HS-Shaya“,” @version“:” 1“}”“”,             “端口”:44780,             “ @version”:“ 1”,             “ @timestamp”:“ 2019-07-14T10:06:34.924Z”           }         }

ELK版本:7.2.0

0 个答案:

没有答案