Python -如何将日志写入已经存在的Kibana?

mmvthczy  于 2023-11-15  发布在  Kibana
关注(0)|答案(1)|浏览(162)

我被要求将log写入现有的Kibana。
我得到了一个host和一个port,我想这是日志存储的hostport
我被告知搜索如何将log发送到该主机。仅此而已。
我一直在研究,但什么也做不了。
我的代码到目前为止:

import logging
from logging import StreamHandler
import logstash
import sys

host = '1**.**.**.***'   #this is the host I was given.

test_logger = logging.getLogger('venus-local')
test_logger.setLevel(logging.INFO)
test_logger.addHandler(logstash.TCPLogstashHandler(host, 5000, version=1))
test_logger.addHandler(StreamHandler())

try:
    test_logger.error('venus-local: test logstash error message.')
    test_logger.info('venus-local: test logstash info message.')
    test_logger.warning('venus-local: test logstash warning message.')

    # add extra field to logstash message
    extra = {
        'test_string': 'python version: ' + repr(sys.version_info),
        'test_boolean': True,
        'test_dict': {'a': 1, 'b': 'c'},
        'test_float': 1.23,
        'test_integer': 123,
        'test_list': [1, 2, '3'],
    }
    test_logger.info('venus-local: test extra fields', extra=extra)
except:
    print("Error")

字符串
当我运行上面的代码时,没有错误,它工作(没有例外)。但是Kibana上没有变化。

wb1gzix0

wb1gzix01#

这就是它通常的工作方式。
Source -> Logstash pipeline(输入插件接受来自source的输入-> filter对数据执行某些处理->输出插件发送到elasticsearch)-> elasticsearch(作为数据库)-> kibana(作为UI)
因此,根据logstash输入插件的配置,您的源可能需要不同的配置来发送到logstash。
您也可以尝试直接从logging.handlers中使用TCPORM,并将logstash服务器主机和端口作为输入。

相关问题