springboot logback.xml 整合ELK

本文介绍了如何在SpringBoot应用中通过logback.xml配置将日志数据发送到ELK(Elasticsearch, Logstash, Kibana)堆栈。首先,在pom文件中添加了logstash依赖。然后,详细展示了logback.xml配置,设置日志拦截器将所有日志信息发送到Logstash的4560端口。此外,还提到了如何配置Logstash以接收特定内容并将其索引到Elasticsearch。" 133369125,19673921,Android StrictMode:性能分析与调试实战,"['Android开发', '编程', '性能优化']
摘要由CSDN通过智能技术生成

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.
  }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值