问题描述
我创建了一个自定义类,该类将日志推送到splunk,但不知何故它不起作用。这是课程。
class Splunk(logging.StreamHandler):
def __init__(self,url,token):
super().__init__()
self.url = url
self.headers = {f'Authorization': f'Splunk {token}'}
self.propagate = False
def emit(self,record):
mydata = dict()
mydata['sourcetype'] = 'mysourcetype'
mydata['event'] = record.__dict__
response = requests.post(self.url,data=json.dumps(mydata),headers=self.headers)
return response
我从记录器类中调用该类,就像这样(添加其他处理程序),以便它可以与发送到splunk一起登录控制台
if splunk_config is not None:
splunk_handler = Splunk(splunk_config["url"],splunk_config["token"])
self.default_logger.addHandler(splunk_handler)
但是不知何故,我看不到任何日志。虽然我可以在控制台中看到日志。 当我尝试从python3终端运行上述逻辑的精简版本时,它是成功的。
import requests
import json
url = 'myurl'
token = 'mytoken'
headers = {'Authorization': 'Splunk mytoken'}
propagate = False
mydata = dict()
mydata['sourcetype'] = 'mysourcetype'
mydata['event'] = {'name': 'root','msg': 'this is a sample message'}
response = requests.post(url,headers=headers)
print(response.text)
我已经尝试过的事情,使用下面的链接使我的字典数据可序列化为JSON,但没有帮助。
https://pynative.com/make-python-class-json-serializable/
还有其他尝试吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)