blob: 8179ec5aa4c3b073f19268b20bca93ee842e08ce [file] [log] [blame]
<div class="wiki-content maincontent"><p><a shape="rect" href="http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/net/JMSAppender.html">Log4j JMS appender</a> 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:</p>
<structured-macro ac:macro-id="1e4e9cda-6e94-4f52-9920-cdafe5b54326" ac:name="code" ac:schema-version="1"><plain-text-body>
log4j.rootLogger=INFO, stdout, jms
## Be sure that ActiveMQ messages are not logged to 'jms' appender
log4j.logger.org.apache.activemq=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %-5p %c - %m%n
## Configure 'jms' appender. You'll also need jndi.properties file in order to make it work
log4j.appender.jms=org.apache.log4j.net.JMSAppender
log4j.appender.jms.InitialContextFactoryName=org.apache.activemq.jndi.ActiveMQInitialContextFactory
log4j.appender.jms.ProviderURL=tcp://localhost:61616
log4j.appender.jms.TopicBindingName=logTopic
log4j.appender.jms.TopicConnectionFactoryBindingName=ConnectionFactory
</plain-text-body></structured-macro>
<p>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 <code>jndi.properties</code> file can look like this:</p>
<structured-macro ac:macro-id="c31e68dc-dc6a-4b98-a5d0-39136b232d7d" ac:name="code" ac:schema-version="1"><plain-text-body>
topic.logTopic=logTopic
</plain-text-body></structured-macro>
<p>Finally, you can subscribe to the topic and listen for log messages:</p>
<structured-macro ac:macro-id="66ba3987-6740-460b-8c38-f1c5528edcd1" ac:name="code" ac:schema-version="1"><plain-text-body>
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);
conn.start();
MessageConsumer consumer = sess.createConsumer(sess.createTopic("logTopic"));
consumer.setMessageListener(this);
// log a message
Logger log = Logger.getLogger(Log4jJMSAppenderExample.class);
log.info("Test log");
// clean up
Thread.sleep(1000);
consumer.close();
sess.close();
conn.close();
System.exit(1);
}
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) {
e.printStackTrace();
}
}
}
</plain-text-body></structured-macro>
<p>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.</p>
<p>Starting with ActiveMQ 5.3, this example is included in the standard distribution. You can run it by executing</p>
<structured-macro ac:macro-id="f04a5d05-04c5-4f1f-a6ba-53458c756417" ac:name="code" ac:schema-version="1"><plain-text-body>
ant log4j-jms
</plain-text-body></structured-macro>
<p>inside <code>example/</code> folder. You can expect the following output as a result:</p>
<structured-macro ac:macro-id="56c65ae1-19f4-4da9-9411-2adf43fa2cf9" ac:name="code" ac:schema-version="1"><plain-text-body>
log4j-jms:
[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
</plain-text-body></structured-macro></div>