LOGSTASH与logback整合

项目pom文件添加依赖

<dependency>  
	 <groupId>net.logstash.logback</groupId>  
	 <artifactId>logstash-logback-encoder</artifactId>  
	 <version>7.0.1</version>  
</dependency>

logback.xml添加

<appender name="LOGSTASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender">  

	 <destination>localhost:9527</destination>  

	  <!-- encoder必须配置,有多种可选 -->  
	  <encoder charset="UTF-8" class="net.logstash.logback.encoder.LogstashEncoder">  
		 <customFields>{"app_name":"csmf_mall"}</customFields>  
	 </encoder>  
</appender>

root标签中添加

<appender-ref ref="LOGSTASH"/>

说明

destination: logback日志输入的目标地址

LOGSTASH安装

logstash下载地址:https://www.elastic.co/cn/downloads/logstash

安装参考: https://www.cnblogs.com/cpy-devops/p/9287531.html

修改config/logstash.yml

#开启配置文件热加载
config.reload.automatic: true

配置字段说明:https://segmentfault.com/a/1190000016591476

修改config/logstash-sample.conf

input {
    tcp {        
		# 在9527端口接收logback传来的日志
        host => "0.0.0.0"
        port => 9527
        mode => "server"
        tags => ["tags"]
        codec => json_lines
    }
}
filter {
}
output {
	#控制台输出
    stdout { codec => rubydebug }
    #输出到es
    elasticsearch { 
	    hosts => ["127.0.0.1:9200"]
	    #user => "user"
	    #password => "password"
	    index => "csmfmall_provider-%{+YYYY.MM.dd}"
    }
}

如果需要过滤日志输出,需要在filter下配置。
input、filter、output插件说明:https://www.elastic.co/guide/en/logstash/current/index.html

启动logstash

./bin/logstash -f ./config/logstash-simple.conf

按上面启动方式启动后,可在控制台日志输出

nohup ./bin/logstash -f ./config/logstash-simple.conf >/dev/null 2>log &

上面方式为后台启动方式

posted on 2022-02-24 16:53  idanyang  阅读(1596)  评论(0编辑  收藏  举报