【问题标题】:Issue in sending python logs to Splunk using splunk_hec_handler使用 splunk_hec_handler 将 python 日志发送到 Splunk 的问题
【发布时间】:2021-12-09 20:14:19
【问题描述】:

我正在使用Python logging library 将日志推送到 splunk。该包使用HEC方式将日志推送到splunk。

我面临的问题是,在我的应用程序中的许多记录器语句中,我希望有选择地只使用少数记录器语句来扩展而不是全部。 所以我在下面创建了一种方法,它转换 json 中的字符串日志(键/值)并推送到 splunk。所以我在我希望推送到 splunk 的记录器语句之后调用此方法。但是休息一下我不想发送给 splunk 的所有记录器语句,它们也会被推送到 splunk。

为什么会这样?

class Test:


    def __init__(self):
 
        self.logger = logging.getLogger('myapp')

    def method_test(self,url,data,headers):

            response = requests.post(url=url, data=json.dumps(data), headers=abc.headers)

            ##i dont want to push this below log message to splunk but this is also getting pushed to splunk
            self.logger.debug(f"request code:{response.request.url} request body:{response.request.body}")

            ##I wish to send this below log to splunk
            self.logger.debug(f"response code:{response.status_code} response body:{response.text}")
            log_dic = {'response_code': response.status_code,'response_body': response.text}
            splunklogger = self.logging_override(log_dic, self.splunk_host,
                                               self.index_token, self.port,
                                               self.proto, self.ssl_verify,
                                                self.source)
            splunklogger.info(log_dic)


        return response    

    def logging_override(log_dict: dict, splunk_host,index_token,splunk_port,splunk_proto,ssl_ver,source_splnk):
        """
        This function help in logging custom fields in JSON key value form by defining fields of our choice in log_dict dictionary
        and pushes logs to Splunk Server
        """
        splunklogger = logging.getLogger()
        splunklogger.setLevel(logging.INFO)
        stream_handler = logging.StreamHandler()
        basic_dict = {"time": "%(asctime)s", "level": "%(levelname)s"}
        full_dict = {**basic_dict, **log_dict}
        stream_formatter = logging.Formatter(json.dumps(full_dict))
        stream_handler.setFormatter(stream_formatter)
        if not splunklogger.handlers:
            splunklogger.addHandler(stream_handler)
        splunklogger.handlers[0] = stream_handler
        splunk_handler = SplunkHecHandler(splunk_host,
                                          index_token,
                                          port=splunk_port, proto=splunk_proto, ssl_verify=ssl_ver,
                                          source=source_splnk)
        splunklogger.addHandler(splunk_handler)
        splunklogger.addHandler(splunk_handler)

        return splunklogger   

【问题讨论】:

    标签: python python-3.x splunk python-logging splunk-hec


    【解决方案1】:

    我认为问题在于您对logging.getLogger 的调用,即当您配置应用记录器时,您指定了记录器名称,但在配置 splunk 记录器时,您没有指定任何,因此获取、配置 SplunkHandler 并将其附加到根记录器。

    默认情况下,当事件进入较低级别的记录器时,它们会将其事件传播到较高级别的记录器(例如根记录器),从而被发送到 Splunk。

    我怀疑一个简单的解决方案是查看您的记录器名称...可能将 Splunk 记录器置于比您的组件更低的级别?或查看记录器的传播。上面链接的同一个文档页面讨论了一些关于记录器对象及其传播的内容。

    【讨论】:

    • 谢谢查理..这是问题:)
    猜你喜欢
    • 1970-01-01
    • 2021-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-08
    相关资源
    最近更新 更多