1.pom文件添加logstash依赖包
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>5.0</version>
</dependency>
2.所有的日志都拦截到es,logback.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="LOGSTASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<destination>116.196.117.183:4560</destination>
<encoder charset="UTF-8" class="net.logstash.logback.encoder.LogstashEncoder" />
</appender>
<root level="INFO">
<appender-ref ref="LOGSTASH" />
</root>
</configuration>
3.拦截指定的内容到ES,logback.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true">
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yy-MM-dd.HH:mm:ss.SSS} [%-16t] %-5p %-22c{0} %X{ServiceId} - %m%n</pattern>
</encoder>
</appender>
<appender name="LOGSTASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<destination>116.196.117.183:4560</destination>
<encoder charset="UTF-8" class="net.logstash.logback.encoder.LogstashEncoder" >
<customFields>{"projectName":"foc"}</customFields>
</encoder>
</appender>
<logger name="behaviorRecordLog" level="INFO" additivity="true">
<appender-ref ref="LOGSTASH"/>
</logger>
<root level="INFO">
<appender-ref ref="CONSOLE" />
</root>
</configuration>
3.1 指定类的编写
package com.swcares.logstash;
import com.alibaba.fastjson.JSON;
import com.swcares.dto.BehaviorRecordDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.util.Date;
/**
* @author wjw
* @Description: 用户行为日志
* @date 2018-11-21
*/
@Component("behaviorRecordLogHandler")
public class BehaviorRecordLogHandler {
private final static Logger logger = LoggerFactory.getLogger("behaviorRecordLog");
/**
* 行为记录写入日志
* @Title: log
* @param behaviorRecordVo
* @author: wjw
* @date: 2018年11月21日
*/
public void log(BehaviorRecordDto behaviorRecordVo) {
logger.info(JSON.toJSONString(behaviorRecordVo));
}
/**
* 行为记录写入日志
* @Title: log
* @param record 行为记录
* @param operatorName 操作人名称
* @author: wjw
* @date: 2018年11月21日
*/
public void log(String record,String operatorName) {
BehaviorRecordDto behaviorRecordVo = new BehaviorRecordDto();
behaviorRecordVo.setOperatorName(operatorName);
behaviorRecordVo.setOperatorRecord(record);
behaviorRecordVo.setOperatorTime(new Date());
log(behaviorRecordVo);
}
/**
* 行为记录写入日志
* @Title: log
* @param record 行为记录
* @param operatorName 操作人名称
* @param reassignment 操作备注
* @author: wjw
* @date: 2018年11月21日
*/
public void logRemark(String record,String operatorName,String reassignment) {
BehaviorRecordDto behaviorRecordVo = new BehaviorRecordDto();
behaviorRecordVo.setOperatorName(operatorName);
behaviorRecordVo.setOperatorRecord(record);
behaviorRecordVo.setOperatorTime(new Date());
behaviorRecordVo.setOperatorMask(reassignment);
log(behaviorRecordVo);
}
}
4.logstash配置文件编写
input {
# For detail config for log4j as input,
# See: https://www.elastic.co/guide/en/logstash/current/plugins-inputs-log4j.html
tcp {
mode => "server"
host => "0.0.0.0"
port => 4560
codec => json_lines
}
}
filter {
#Only matched data are send to output.
}
output {
# For detail config for elasticsearch as output,
# See: https://www.elastic.co/guide/en/logstash/current/plugins-outputs-elasticsearch.html
elasticsearch {
action => "index" #The operation on ES
hosts => "116.196.117.183:9200" #ElasticSearch host, can be array.
index => "%{[projectName]}" #The index to write data to.
}
}