这个项目的目的是简化日志传送到您的graylog服务器相当简单
gray-py-gelf的Python项目详细描述
项目描述
grayloghttpgelf与Python
说明
这个库旨在简化使用python语言将日志发送到graylog服务器的过程。在
在浏览器端口9000的Graylog GUI(网页)的输入区域使用GELF HTTP创建输入之前,必须使用此库。在
第一步
第一步是通过使用pip管理python包来安装gray py gelf库:
pip install gray-py-gelf
第二步
本例中使用一些额外的库来编写自己的日志。在
^{pr2}$使用grey代码在python中导入:
from graylog_lib.graylog_http_gelf import Graylog
第三步
从其构造函数传递参数实例化graylog对象:
graylog = Graylog(url="http://localhost:12201/gelf", host="Rafael's Notebook", short_message="my_indice_name", level=6)
创建要发送到graylog GELF的日志
请记住,在将任何日志发送到Graylog进行管理之前,必须先创建GELF输入:
log = '"id":{},"name":"{}","age":{},"date":"{}","hour":"{}"'.format(1, "Rafael Sanches", 28, today.strftime("%d/%m/%Y"), strftime("%H:%M:%S", gmtime()))
log = '{'+log+'}'
resp = graylog.sender(log)
print(resp)
在您可以开始导入您的提取器日志之前,您必须上载该日志
提取器可确保您的生命安全,并将您的日志转换为单独的键和值:
在您的输入GELF HTTP中导入此提取器:
{
"extractors": [
{
"title": "extractor_json",
"extractor_type": "json",
"converters": [],
"order": 0,
"cursor_strategy": "copy",
"source_field": "json",
"target_field": "",
"extractor_config": {
"list_separator": ", ",
"kv_separator": "=",
"key_prefix": "",
"key_separator": "_",
"replace_key_whitespace": true,
"key_whitespace_replacement": "_"
},
"condition_type": "none",
"condition_value": ""
}
],
"version": "3.0.2"
}
一些提示:
- 没有标记'平坦'选项字段时,你将去创建你的提取器
- 标记“key_whitespace_replacement”并设置一些东西类型
关于属性graylog对象的含义:
- 在
在url: Your graylog server and port used to GELF
- 在
在host: Who send this log? e.g ip the machine for example
- 在
在level: A integer number that represents a level about your log content
- 短消息:使用此字段表示索引
- sender:是一种将字符串类型a json发送到lib python的方法,有一个预处理,并以json格式将所有信息发送到graylog服务器。如果一切正常,这个方法将返回一个http202代码。在
完整代码示例:
from datetime import date
from time import gmtime, strftime
today = date.today()
from graylog_lib.graylog_http_gelf import Graylog
graylog = Graylog(url="http://localhost:12201/gelf", host="Rafael's Notebook", short_message="rafael_test", level=6)
for i in range(50):
log = '"id":{},"name":"{}","age":{},"date":"{}","hour":"{}"'.format(i, "Rafael Sanches", 28, today.strftime("%d/%m/%Y"), strftime("%H:%M:%S", gmtime()))
log = '{'+log+'}'
print(type(log))
print(log)
resp = graylog.sender(log)
print(resp)
如有任何问题,请发电子邮件给我:rafaelsanches123@gmail.com 如果你想在python中看到很酷的东西,请访问我的个人blog!在
- 项目
标签: