Benefits: The project log is written to Logstash and then sent to Elasticsearch, which makes it easy to view the search log, as well as report analysis.
Logstash is a data acquisition tool, there are a variety of channels, such as files, TCP,UDP, etc., if it is to collect log files, you need to store files on the server, start a Logstash service, not easy to quickly deploy, and the way to adopt tcp/udp relatively simple , you just need to send the data to the Logstash service. Here is the main way to talk about network transmission.
First, both TCP and UDP need to add dependencies in Maven:
<dependency> <groupId>net.logstash.logback</groupId> <artifactId> logstash-logback-encoder</artifactid> <version>4.8</version></dependency>
Here we are using the Logback log framework
Tcp
<appender name= " tcp_logstash Span style= "COLOR: #800000" > " class =" net.logstash.logback.appender.logstashtcpsocketappender " > <destination>192.168 . 10.11 : 2233 </destination> <!--encoder must be configured with a variety of options-- <encoder charset= " utf-8 " class = " net.logstash.logback.encoder.logstashencoder " /> </appender>
Note: class is using Logstashtcpsocketappender, which supports TCP transport
Udp
<appender name="udp_logstash"class=" Net.logstash.logback.appender.LogstashSocketAppender"> 192.168. 10.113333</port> </appender>
Note: class is using Logstashsocketappender, which supports UDP transmission
Finally, in <root>, add the appropriate reference to the line, for example:
<root level="warn"> <appender-ref ref="Udp_logstash"/> <appender-ref ref="Console"/> <appender-ref ref="Info"/> <appender-ref ref="warn"/> <appender-ref ref="Error"/> </root>
Here, the configuration in our code is complete, and after the program is started, the corresponding level+ log is sent to Logstash.
Java project log written to LOGSTASH-TCP/UDP