log4j 日志整合ELK

1.pom.xml 添加log4j依赖


    log4j
    log4j
    1.2.14

2.log4j.properties 编写

### set log levels ALL
package com.v2.swcares.logstash;

import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Component;
import org.apache.log4j.Logger;
import java.util.Date;

/**
 * @author wjw
 * @Description: 用户行为日志
 * @date 2018-11-21
 */
@Component("behaviorRecordLogHandler")
public class BehaviorRecordLogHandler {
    private final static Logger logger = Logger.getLogger(BehaviorRecordLogHandler.class);
    /**
     * 行为记录写入日志
     * @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
  log4j {
    host => "0.0.0.0"
    port => 4561
  }
}
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  => "tvpsi"         #The index to write data to.
  }
}

你可能感兴趣的:(ELK日志分析系统搭建及应用)