Spring通过Rabbitmq将日志发送到logstash并存储至ES-笔记

一、pom.xml添加配置

<dependency>
            <groupId>net.logstash.logback</groupId>
            <artifactId>logstash-logback-encoder</artifactId>
            <version>5.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <optional>true</optional>
        </dependency>
        <!--用户logback Filter动态编译,分离行为日志-->
        <dependency>
            <groupId>org.codehaus.janino</groupId>
            <artifactId>janino</artifactId>
            <version>3.0.12</version>
        </dependency>

二、appender配置

<appender name="AMQP" class="org.springframework.amqp.rabbit.logback.AmqpAppender">
        <!--<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">-->
            <!--<pattern>${AMQP_LOG_PATTERN}</pattern>-->
        <!--</encoder>-->
        <includeCallerData>true</includeCallerData>
        <!--<encoder class="net.logstash.logback.encoder.LogstashEncoder" >-->
            <!--<customFields>{"appname":"bdcor2"}</customFields>-->
            <!--<includeMdcKeyName>user.uuid</includeMdcKeyName>-->
            <!--<includeCallerData>true</includeCallerData>-->
            <!--<includeContext>false</includeContext>-->
        <!--</encoder>-->
        <encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder" >
            <providers>
                <timestamp/>
                <pattern>
                    <pattern>
                        {
                        "env":"${SPRING_PROFILES_ACTIVE}",
                        "appName":"${SPRING_APPLICATION_NAME}",
                        "logType":"behavior",
                        "logger": "%logger",
                        "level": "%level",
                        "thread": "%thread",
                        "message": "%message"
                        }
                    </pattern>
                </pattern>
            </providers>
        </encoder>

        <host>${LOGBACK_APPENDER_AMQP_HOST}</host>
        <port>${LOGBACK_APPENDER_AMQP_PORT}</port>
        <username>${LOGBACK_APPENDER_AMQP_USERNAME}</username>
        <password>${LOGBACK_APPENDER_AMQP_PASSWORD}</password>
        <applicationId>${LOGBACK_APPENDER_AMQP_APPLICATION_ID}</applicationId>
        <routingKeyPattern>${LOGBACK_APPENDER_AMQP_ROUTING_KEY_PATTERN}</routingKeyPattern>
        <declareExchange>${LOGBACK_APPENDER_AMQP_DECLARE_EXCHANGE}</declareExchange>
        <exchangeType>${LOGBACK_APPENDER_AMQP_EXCHANGE_TYPE}</exchangeType>
        <exchangeName>${LOGBACK_APPENDER_AMQP_EXCHANGE_NAME}</exchangeName>
        <generateId>${LOGBACK_APPENDER_AMQP_GENERATE_ID}</generateId>
        <charset>UTF-8</charset>
        <durable>${LOGBACK_APPENDER_AMQP_DURABLE}</durable>
        <deliveryMode>${LOGBACK_APPENDER_AMQP_DELIVERY_MODE}</deliveryMode>
        <virtualHost>${LOGBACK_APPENDER_AMQP_VIRTUAL_HOST}</virtualHost>
    </appender>

 

三、logstash.conf配置

input { 
   rabbitmq {
       type =>"all"
       durable => false
       exchange => "claves-log"
	   exchange_type => "topic"
	   queue => "claves-log-logstash"
	   key => "claves-demo-app"
	   host => "127.0.0.1"
	   port => 5672
	   user => "admin"
	   password => "admin"
       auto_delete => false
	   codec => json
  }	
 }

 
output {
  elasticsearch { 
       hosts => ["192.168.1.1:18080"] 
	   index => "%{appName}-%{env}-%{logType}"
}
}

 

分享到:更多 ()