How do I use log4j JMS appender with ActiveMQ

 FAQ > Using Apache ActiveMQ > How do I use log4j JMS appender with ActiveMQ

Log4j JMS appender can be used to send your log messages to JMS broker. To use ActiveMQ as a destination of your messages, you need to configure JMS appender properly. The code sample below shows example configuration:

log4j.rootLogger=INFO, stdout, jms

## Be sure that ActiveMQ messages are not logged to 'jms' appender, stdout

log4j.appender.stdout.layout.ConversionPattern=%d %-5p %c - %m%n

## Configure 'jms' appender. You'll also need file in order to make it work

The important thing is not to send ActiveMQ logs to JMS appender, as it can cause errors since the broker will want to log before the connection is established. You will also need a JNDI configuration, so that appender can find appropriate topic to send log messages to. The example file can look like this:


Finally, you can subscribe to the topic and listen for log messages:

public class Log4jJMSAppenderExample implements MessageListener {

	public Log4jJMSAppenderExample() throws Exception {
		// create a logTopic topic consumer
		ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
		Connection conn = factory.createConnection();
		Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
		MessageConsumer consumer = sess.createConsumer(sess.createTopic("logTopic"));
		// log a message
		Logger log = Logger.getLogger(Log4jJMSAppenderExample.class);"Test log");
		// clean up
	public static void main(String[] args) throws Exception {
		new Log4jJMSAppenderExample();

	public void onMessage(Message message) {
		try {
			// receive log event in your consumer
			LoggingEvent event = (LoggingEvent)((ActiveMQObjectMessage)message).getObject();
			System.out.println("Received log [" + event.getLevel() + "]: "+ event.getMessage());
		} catch (Exception e) {

Note that appender send logging event wrapped in an object message, so you can extract information such as logger name, level and of course the message.

Starting with ActiveMQ 5.3, this example is included in the standard distribution. You can run it by executing

ant log4j-jms

inside example/ folder. You can expect the following output as a result:

     [echo] Running a Log4j JMS Appender example
     [java] 2009-08-11 13:58:46,767 INFO  Log4jJMSAppenderExample - Test log
     [java] Received log [INFO]: Test log
     [java] Java Result: 1

Apache ActiveMQ, ActiveMQ, ActiveMQ Artemis, Apache, the Apache feather logo, and the Apache ActiveMQ project logo are trademarks of The Apache Software Foundation. Copyright © 2019, The Apache Software Foundation. Licensed under Apache License 2.0.