Using the Logback AmqpAppender in Spring AMQP

Let's start by clarifying what the Spring AmqpAppender does. If we go to the Class Documentation we see the following: "The AmqpAppender is a Logback appender that publishes logging events to an AMQP Exchange." Again, "publishes events to an AMQP Exchange."  We aren't logging to a RabbitMQ log file like /var/log/rabbitmq/rabbit@server.log, but to a RabbitMQ Exchange.

With that cleared up we'll look at the logback.xml config. The Exchange we'll be logging to is nixmashmq.log.exchange. We'll be using a key of %c.%p, which in Logback Speak is the ClassName and Logging Level. Notice the commented-out routingKeyPattern line. We COULD send all logging events to a single Queue, but we're going to break up the logging endpoints by Class.

Configuring Our Logging Endpoints

Let's jump to the end[points] of our AMQP Logging configuration. As I said we're logging by ClassName. Our app handles two types of data: Simple String Messages and Reservation Data Objects. These endpoints are in MsgReceiver.java and DataReceiver.java respectively and will serve as the ClassNames, or the %c from our logback.xml file. Also notice that the Logging Level is capitalized.

Logging the Event

We're in a Reservation endpoint now and will log a message to our nixmashmq.log.exchange RabbitMQ Exchange.

Our AMQP Appender Logging Endpoints simply display a message for us. Let's look at the output where we send a Simple String Message to a Queue and a Reservation object to two different Queues. The output of the above displayReservation() method begins with the first "Received Reservation <Bob>".

What's Happening on the RabbitMQ Server

We'll finish up by looking at what's happening in RabbitMQ.

We're not specifying a particular queue but instead are binding a Routing Key to an Exchange, so a temporary queue is created for us and bound appropriately.

Source Code Notes for this Post

Source code discussed in our Spring AMQP Series is found in NixMash RabbitMQ on GitHub. Code for this particular post is found in branch nixmashmq-v0.1.0 - AMQPAppender Logging.

Posted April 26, 2017 03:09 PM EDT

More Like This Post