Logback appender to post message as HTTP message

夙愿已清 提交于 2019-12-11 13:18:47

问题


As per my requirement I just wanted to post HTTP message to other end which is logged by org.slf4j.LoggerFactory.getLogger().

The following JSON string logged at INFO level.

{
  "studentName": "My Name",
  "Deratment": "Computer Science",
  "address": {
     "Address Line1": "My Address Line1",
     "Address Line2": "My Address Line2",
     "Address Line3": "My Address Line3"
  }
}

Considerations,

  1. Http message should post with MIME type application/json

  2. should process only the particular log in INFO level not all.

Is there any built-in appender in Logback to achieve this?

If not, what is the best way to do it?


回答1:


Logback works really well with logstash: https://github.com/logstash/logstash-logback-encoder.

First step would be to configure logback to logstash connection.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <appender name="stash" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
      <destination>127.0.0.1:4560</destination>

      <!-- encoder is required -->
      <encoder class="net.logstash.logback.encoder.LogstashEncoder" />
  </appender>

  <root level="DEBUG">
      <appender-ref ref="stash" />
  </root>
</configuration>

Once it is done, you would need to create a pipeline from tcp input to http output plugin that may look as follows

input {
    tcp {
            port => 4560
            codec => json_lines
        }
}

output {
    http {
        http_method => ...
        url => ...
    }
}

(see https://www.elastic.co/guide/en/logstash/current/plugins-outputs-http.html)



来源:https://stackoverflow.com/questions/38271359/logback-appender-to-post-message-as-http-message

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!