我正在使用Spring Boot(1.5.4)。我希望通过RabbitMQ以JSON格式而不是纯文本将我的服务中的(logback)日志发送到Logstash。这将使我不必在Logstash端设置过滤器,以便可以在应用程序端控制格式化(使用Logback Encoder)。
我知道用于RabbitMQ org.springframework.amqp.rabbit.logback.AmqpAppender
的Spring logback AMQP Appender但是它使用布局(纯文本)而不是格式化JSON。我想使用LogStash编码器net.logstash.logback.encoder.LogstashEncoder
。我想将Appender与编码器一起使用(我想要一切:“)。
答案 0 :(得分:0)
我首先扩展了AMQPAppender以添加Encoder,如下所示: -
package nz.govt.mpi.util;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.logback.AmqpAppender;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.encoder.Encoder;
import lombok.Getter;
import lombok.Setter;
public class AmqpLogbackAppender extends AmqpAppender {
@Getter
@Setter
private Encoder<ILoggingEvent> encoder;
/**
* We remove the default message layout and replace with the JSON {@link Encoder}
*/
@Override
public Message postProcessMessageBeforeSend(Message message, Event event) {
return new Message(this.encoder.encode(event.getEvent()), message.getMessageProperties());
}
@Override
public void start() {
super.start();
encoder.setContext(getContext());
if (!encoder.isStarted()) {
encoder.start();
}
}
@Override
public void stop() {
super.stop();
encoder.stop();
}
}
然后我按如下方式设置logback-spring.xml配置文件: -
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<springProperty scope="context" name="rabbitMQHost" source="logback.amqp.host" defaultValue="localhost"/>
<springProperty scope="context" name="rabbitMQPort" source="logback.amqp.port" defaultValue="5672"/>
<springProperty scope="context" name="rabbitMQUsername" source="spring.rabbitmq.username" />
<springProperty scope="context" name="rabbitMQPassword" source="spring.rabbitmq.password" />
<springProperty scope="context" name="rabbitMQExchangeName" source="logback.amqp.exchange.name" defaultValue="mpi.tradedev"/>
<springProperty scope="context" name="rabbitMQRoutingKey" source="logback.amqp.routing.key" defaultValue="mpi.tradedev.logging"/>
<springProperty scope="context" name="serviceName" source="spring.application.name" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread, %X{X-B3-TraceId:-},%X{X-B3-SpanId:-}] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="AMQP" class="nz.govt.mpi.util.AmqpLogbackAppender">
<!-- layout is required but ignored as using the encoder for the AMQP message body -->
<layout><pattern><![CDATA[ %level ]]></pattern></layout>
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<customFields>{"serviceName": "${serviceName}"}</customFields>
</encoder>
<!-- RabbitMQ connection -->
<host>${rabbitMQHost}</host>
<port>${rabbitMQPort}</port>
<username>${rabbitMQUsername}</username>
<password>${rabbitMQPassword}</password>
<exchangeName>${rabbitMQExchangeName}</exchangeName>
<routingKeyPattern>${rabbitMQRoutingKey}</routingKeyPattern>
<declareExchange>true</declareExchange>
<exchangeType>topic</exchangeType>
<generateId>true</generateId>
<charset>UTF-8</charset>
<durable>true</durable>
<deliveryMode>PERSISTENT</deliveryMode>
</appender>
<root level="info">
<appender-ref ref="STDOUT" />
<appender-ref ref="AMQP" />
</root>
</configuration>
我最后将所需的属性添加到application.properties
文件中,如下所示: -
spring.application.name=my-app
logback.amqp.host=localhost
logback.amqp.port=5672
logback.amqp.exchange.name=ex_logstash
logback.amqp.routing.key=my-app.logging
spring.rabbitmq.username=rquser
spring.rabbitmq.password=rqpass
我还必须在RabbitMQ中设置必要的用户帐户。当应用程序运行时,它会创建主题(ex_logstash),但您必须创建一个绑定到该主题的队列(qu_logstash),并匹配路由键(my-app。*)。 然后,您可以创建logstash配置以匹配队列名称。
ex_logstash - &gt; qu_logstash
logstash.json
配置文件示例: -
input {
rabbitmq {
host => "localhost"
queue => "qu_logstash"
durable => true
exchange => "ex_logstash"
key => "my-app.*"
threads => 10
type => "topic"
prefetch_count => 200
port => 5672
user => "rquser"
password => "rqpass"
}
}
在应用程序端,您将需要pom.xml中所需的依赖项。这些是我使用的,涵盖了所需的类YMMV: -
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>4.9</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>