log4j2通過tcp傳送json日誌到logstash Elasticsearch

後開啟撒打發了發表於2017-11-28

參考資料(官網):https://www.elastic.co/guide/en/logstash/6.0/plugins-inputs-tcp.html

通過tcp的傳送資料到logstash需要設定好ip+port就好,用你熟悉的方式傳送資料即可。

log4j2 tcp 方式 傳送json資料到logstash

log4j2.xml的配置參考

<Configuration>
  <Appenders>
     <Socket name="Socket" host="localhost" port="12345">
       <JsonLayout compact="true" eventEol="true" />
    </Socket>
  </Appenders>
  <Loggers>
    <Root level="info">
      <AppenderRef ref="Socket"/>
    </Root>
  </Loggers>
</Configuration>

logstash的配置參考

input {
    tcp {  
         
        host => "192.168.0.153"
        port => 4567  
        #codec => plain { charset => "GB2312" }  
    	codec => json
    }  
} 


filter {
    json {
        source => "message"
        add_field => ["type", "%{dtype}"]
        remove_field => [ "server", "server.fqdn", "timestamp" ]
    }
}

output {

    if "_jsonparsefailure" not in [tags] {
        stdout { codec => rubydebug }
        elasticsearch {
            hosts => "192.168.2.181:9200"
        }
    }
}

相關文章