Logback appender to post message as HTTP message

2019-08-15 17:24发布

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条回答
神经病院院长
2楼-- · 2019-08-15 18:14

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)

查看更多
登录 后发表回答