This version is still in development and is not considered stable yet. For the latest stable version, please use Spring AMQP 3.2.0!spring-doc.cn

Logging Subsystem AMQP Appenders

The framework provides logging appenders for some popular logging subsystems:spring-doc.cn

The appenders are configured by using the normal mechanisms for the logging subsystem, available properties are specified in the following sections.spring-doc.cn

Common properties

The following properties are available with all appenders:spring-doc.cn

Table 1. Common Appender Properties
Property Default Description
 exchangeName
 logs

Name of the exchange to which to publish log events.spring-doc.cn

 exchangeType
 topic

Type of the exchange to which to publish log events — needed only if the appender declares the exchange. See declareExchange.spring-doc.cn

 routingKeyPattern
 %c.%p

Logging subsystem pattern format to use to generate a routing key.spring-doc.cn

 applicationId

Application ID — added to the routing key if the pattern includes %X{applicationId}.spring-doc.cn

 senderPoolSize
 2

The number of threads to use to publish log events.spring-doc.cn

 maxSenderRetries
 30

How many times to retry sending a message if the broker is unavailable or there is some other error. Retries are delayed as follows: N ^ log(N), where N is the retry number.spring-doc.cn

 addresses

A comma-delimited list of broker addresses in the following form: host:port[,host:port]* - overrides host and port.spring-doc.cn

 host
 localhost

RabbitMQ host to which to connect .spring-doc.cn

 port
 5672

RabbitMQ port to which to connect.spring-doc.cn

 virtualHost
 /

RabbitMQ virtual host to which to connect.spring-doc.cn

 username
 guest

RabbitMQ user to use when connecting.spring-doc.cn

 password
 guest

RabbitMQ password for this user.spring-doc.cn

 useSsl
 false

Whether to use SSL for the RabbitMQ connection. See RabbitConnectionFactoryBean and Configuring SSLspring-doc.cn

 verifyHostname
 true

Enable server hostname verification for TLS connections. See RabbitConnectionFactoryBean and Configuring SSLspring-doc.cn

 sslAlgorithm
 null

The SSL algorithm to use.spring-doc.cn

 sslPropertiesLocation
 null

Location of the SSL properties file.spring-doc.cn

 keyStore
 null

Location of the keystore.spring-doc.cn

 keyStorePassphrase
 null

Passphrase for the keystore.spring-doc.cn

 keyStoreType
 JKS

The keystore type.spring-doc.cn

 trustStore
 null

Location of the truststore.spring-doc.cn

 trustStorePassphrase
 null

Passphrase for the truststore.spring-doc.cn

 trustStoreType
 JKS

The truststore type.spring-doc.cn

 saslConfig
 null (RabbitMQ client default applies)

The saslConfig - see the javadoc for RabbitUtils.stringToSaslConfig for valid values.spring-doc.cn

 contentType
 text/plain

content-type property of log messages.spring-doc.cn

 contentEncoding

content-encoding property of log messages.spring-doc.cn

 declareExchange
 false

Whether or not to declare the configured exchange when this appender starts. See also durable and autoDelete.spring-doc.cn

 durable
 true

When declareExchange is true, the durable flag is set to this value.spring-doc.cn

 autoDelete
 false

When declareExchange is true, the auto-delete flag is set to this value.spring-doc.cn

 charset
 null

Character set to use when converting String to byte[]. Default: null (the system default charset is used). If the character set is unsupported on the current platform, we fall back to using the system character set.spring-doc.cn

 deliveryMode
 PERSISTENT

PERSISTENT or NON_PERSISTENT to determine whether or not RabbitMQ should persist the messages.spring-doc.cn

 generateId
 false

Used to determine whether the messageId property is set to a unique value.spring-doc.cn

 clientConnectionProperties
 null

A comma-delimited list of key:value pairs for custom client properties to the RabbitMQ connection.spring-doc.cn

 addMdcAsHeaders
 true

MDC properties were always added into RabbitMQ message headers until this property was introduced. It can lead to issues for big MDC as while RabbitMQ has limited buffer size for all headers and this buffer is pretty small. This property was introduced to avoid issues in cases of big MDC. By default this value set to true for backward compatibility. The false turns off serialization MDC into headers. Please note, the JsonLayout adds MDC into the message by default.spring-doc.cn

Log4j 2 Appender

The following example shows how to configure a Log4j 2 appender:spring-doc.cn

<Appenders>
    ...
    <RabbitMQ name="rabbitmq"
        addresses="foo:5672,bar:5672" user="guest" password="guest" virtualHost="/"
        exchange="log4j2" exchangeType="topic" declareExchange="true" durable="true" autoDelete="false"
        applicationId="myAppId" routingKeyPattern="%X{applicationId}.%c.%p"
        contentType="text/plain" contentEncoding="UTF-8" generateId="true" deliveryMode="NON_PERSISTENT"
        charset="UTF-8"
        senderPoolSize="3" maxSenderRetries="5"
        addMdcAsHeaders="false">
    </RabbitMQ>
</Appenders>

Starting with versions 1.6.10 and 1.7.3, by default, the log4j2 appender publishes the messages to RabbitMQ on the calling thread. This is because Log4j 2 does not, by default, create thread-safe events. If the broker is down, the maxSenderRetries is used to retry, with no delay between retries. If you wish to restore the previous behavior of publishing the messages on separate threads (senderPoolSize), you can set the async property to true. However, you also need to configure Log4j 2 to use the DefaultLogEventFactory instead of the ReusableLogEventFactory. One way to do that is to set the system property -Dlog4j2.enable.threadlocals=false. If you use asynchronous publishing with the ReusableLogEventFactory, events have a high likelihood of being corrupted due to cross-talk.spring-doc.cn

Logback Appender

The following example shows how to configure a logback appender:spring-doc.cn

<appender name="AMQP" class="org.springframework.amqp.rabbit.logback.AmqpAppender">
    <layout>
        <pattern><![CDATA[ %d %p %t [%c] - <%m>%n ]]></pattern>
    </layout>
    <addresses>foo:5672,bar:5672</addresses>
    <abbreviation>36</abbreviation>
    <includeCallerData>false</includeCallerData>
    <applicationId>myApplication</applicationId>
    <routingKeyPattern>%property{applicationId}.%c.%p</routingKeyPattern>
    <generateId>true</generateId>
    <charset>UTF-8</charset>
    <durable>false</durable>
    <deliveryMode>NON_PERSISTENT</deliveryMode>
    <declareExchange>true</declareExchange>
    <addMdcAsHeaders>false</addMdcAsHeaders>
</appender>

Starting with version 1.7.1, the Logback AmqpAppender provides an includeCallerData option, which is false by default. Extracting caller data can be rather expensive, because the log event has to create a throwable and inspect it to determine the calling location. Therefore, by default, caller data associated with an event is not extracted when the event is added to the event queue. You can configure the appender to include caller data by setting the includeCallerData property to true.spring-doc.cn

Starting with version 2.0.0, the Logback AmqpAppender supports Logback encoders with the encoder option. The encoder and layout options are mutually exclusive.spring-doc.cn

Customizing the Messages

By default, AMQP appenders populate the following message properties:spring-doc.cn

In addition they populate headers with the following values:spring-doc.cn

Each of the appenders can be subclassed, letting you modify the messages before publishing. The following example shows how to customize log messages:spring-doc.cn

public class MyEnhancedAppender extends AmqpAppender {

    @Override
    public Message postProcessMessageBeforeSend(Message message, Event event) {
        message.getMessageProperties().setHeader("foo", "bar");
        return message;
    }

}

Starting with 2.2.4, the log4j2 AmqpAppender can be extended using @PluginBuilderFactory and extending also AmqpAppender.Builderspring-doc.cn

@Plugin(name = "MyEnhancedAppender", category = "Core", elementType = "appender", printObject = true)
public class MyEnhancedAppender extends AmqpAppender {

	public MyEnhancedAppender(String name, Filter filter, Layout<? extends Serializable> layout,
			boolean ignoreExceptions, AmqpManager manager, BlockingQueue<Event> eventQueue, String foo, String bar) {
		super(name, filter, layout, ignoreExceptions, manager, eventQueue);

	@Override
	public Message postProcessMessageBeforeSend(Message message, Event event) {
			message.getMessageProperties().setHeader("foo", "bar");
		return message;
	}

	@PluginBuilderFactory
	public static Builder newBuilder() {
		return new Builder();
	}

	protected static class Builder extends AmqpAppender.Builder {

		@Override
		protected AmqpAppender buildInstance(String name, Filter filter, Layout<? extends Serializable> layout,
				boolean ignoreExceptions, AmqpManager manager, BlockingQueue<Event> eventQueue) {
			return new MyEnhancedAppender(name, filter, layout, ignoreExceptions, manager, eventQueue);
		}
	}

}

Customizing the Client Properties

You can add custom client properties by adding either string properties or more complex properties.spring-doc.cn

Simple String Properties

Each appender supports adding client properties to the RabbitMQ connection.spring-doc.cn

The following example shows how to add a custom client property for logback:spring-doc.cn

<appender name="AMQP" ...>
    ...
    <clientConnectionProperties>thing1:thing2,cat:hat</clientConnectionProperties>
    ...
</appender>
log4j2
<Appenders>
    ...
    <RabbitMQ name="rabbitmq"
        ...
        clientConnectionProperties="thing1:thing2,cat:hat"
        ...
    </RabbitMQ>
</Appenders>

The properties are a comma-delimited list of key:value pairs. Keys and values cannot contain commas or colons.spring-doc.cn

These properties appear on the RabbitMQ Admin UI when the connection is viewed.spring-doc.cn

Advanced Technique for Logback

You can subclass the Logback appender. Doing so lets you modify the client connection properties before the connection is established. The following example shows how to do so:spring-doc.cn

public class MyEnhancedAppender extends AmqpAppender {

    private String thing1;

    @Override
    protected void updateConnectionClientProperties(Map<String, Object> clientProperties) {
        clientProperties.put("thing1", this.thing1);
    }

    public void setThing1(String thing1) {
        this.thing1 = thing1;
    }

}

Then you can add <thing1>thing2</thing1> to logback.xml.spring-doc.cn

For String properties such as those shown in the preceding example, the previous technique can be used. Subclasses allow for adding richer properties (such as adding a Map or numeric property).spring-doc.cn

Providing a Custom Queue Implementation

The AmqpAppenders use a BlockingQueue to asynchronously publish logging events to RabbitMQ. By default, a LinkedBlockingQueue is used. However, you can supply any kind of custom BlockingQueue implementation.spring-doc.cn

The following example shows how to do so for Logback:spring-doc.cn

public class MyEnhancedAppender extends AmqpAppender {

    @Override
    protected BlockingQueue<Event> createEventQueue() {
        return new ArrayBlockingQueue();
    }

}

The Log4j 2 appender supports using a BlockingQueueFactory, as the following example shows:spring-doc.cn

<Appenders>
    ...
    <RabbitMQ name="rabbitmq"
              bufferSize="10" ... >
        <ArrayBlockingQueue/>
    </RabbitMQ>
</Appenders>