views:

1105

answers:

3

I am trying to produce a proof of concept shipping logging from Log4J through JMS using the log4J JMSAppender. I have tried ActiveMQ and the example supplied with it. I have torn this example apart, and made it more generic and compatible with multiple platforms.

It looks like I have it all plumbed up OK as I can see a connection to the ActiveMQ happening, but the code hangs when I get the InitialContext ( with -Dlog4j.debug set the ActiveMQ client classes seem to invoke log4J and load the properties which in turn try to make a connection to the JMS for the JMSAppender) and then the code just hangs. I have tried to isolate the log messages heading to the JMS by only defining the appender for a single named logger, and the org.apache.activemq package is configured to use the ConsoleAppender

The same code works just fine when pointed to a Weblogic Server with a JMS queue configured up but for maximum compatibility I need to try to make it work with ActiveMQ

Is there some 'magic' bit of configuration that I am missing to make ActiveMQ work correctly?

-- some sample bits from the work so far to flesh this question out a bit now I have the code to hand

log4j-jms.properties

log4j.rootLogger=INFO, stdout

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

log4j.logger.org.apache=ERROR, stdout
log4j.logger.javax=ERROR,stdout
log4j.logger.java=ERROR,stdout

log4j.logger.demo=DEBUG,jms

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=topic.logTopic
log4j.appender.jms.TopicConnectionFactoryBindingName=ConnectionFactory

The object of this was to create a named appender 'demo' and in the sample code grab that to log to make sure that activemq logging was not trying to send itself to JMS

code example. Its a bit of a mess as I have been hacking around with it to try to make things work. As it stands it will work when I point it to Weblogic, and switch the log4j config similarly. The object in this code was to make sure I had the listener for the topic running in a separate thread

NewLog4jJMSAppenderExample.java

import javax.jms.*; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import org.apache.log4j.Logger; import org.apache.log4j.spi.LoggingEvent;

import java.util.Properties;

/**
 * A simple example of log4j jms appender in conjuction with ActiveMQ
 */
public class NewLog4jJMSAppenderExample {
    Runnable listener;
    Thread runner;

    private enum MQImplementation {
        ActiveMQ, Weblogic
    };

    public NewLog4jJMSAppenderExample() {
        // create a logTopic topic consumer

        listener = new BigEars();
        System.out.println("******* Listener Created **********");

        runner = new Thread(listener);
        runner.start();

        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

    }

    public static void main(String[] args) throws Exception {
        System.out.println("******* I HAVE STARTED **********");


        new NewLog4jJMSAppenderExample();


        System.out.println("******* LOGGING **********");

        // log a message

        Logger log = Logger.getLogger("demo");
        log.error("Test log");

        Thread.sleep(100000);

        System.exit(1);

    }

    public class BigEars implements Runnable, MessageListener {
        ConnectionFactory factory;
        Connection conn;
        Session sess;
        MessageConsumer consumer;

        public BigEars() {

            MQImplementation inUse = MQImplementation.ActiveMQ;

            System.out.println("Constructing Bigears");
            try {
                Properties env = new Properties();

                switch (inUse) {

                    case Weblogic:
                        env.put(Context.INITIAL_CONTEXT_FACTORY,
                                "weblogic.jndi.WLInitialContextFactory");
                        env.put(Context.PROVIDER_URL,
                                "t3://localhost:7001");
                        break;

                    case ActiveMQ:
                        env.put(Context.INITIAL_CONTEXT_FACTORY,
                                "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
                        env.put(Context.PROVIDER_URL,
                                "tcp://localhost:61616");
                        break;
                }

                System.out.println("Initial Context");

                InitialContext jndi = new InitialContext(env);
                System.out.println("Factory");
                factory = (TopicConnectionFactory) jndi.lookup("ConnectionFactory");

                Topic theTopic = (Topic) jndi.lookup("topic.logTopic");


                System.out.println("Connection");
                conn = factory.createConnection();

                System.out.println("******* I HAVE set up and created connection **********");
                System.out.println("session");
                sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
                System.out.println("consumer");
                consumer = sess.createConsumer(theTopic);
                System.out.println("listener");
                consumer.setMessageListener(this);

                conn.start();



            } catch (JMSException jme) {
                System.out.println(jme);
            } catch (NamingException ne) {
                System.out.println(ne);
            }
        }


        public void run() {
            try {
                System.out.println("******* zzzzzzzz! **********");

                Thread.sleep(100000);
            } catch (Exception e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }

        public void onMessage(Message message) {
            try {
                try {
                    System.out.println("******* I GOT A MESSAGE **********");
                    // receive log event in your consumer
                    System.out.println(message.toString());

                    LoggingEvent event = (LoggingEvent) (((ObjectMessage) message).getObject());
                    System.out.println("Received log [" + event.getLevel() + "]: " + event.getMessage());
                } catch (Exception e) {
                    e.printStackTrace();
                }

            } finally {

                try {
                    consumer.close();
                    sess.close();
                    conn.close();
                } catch (JMSException jme) {
                    System.out.println(jme);
                }
            }
        }

} }

logging presented when -Dlog4j.debug set

******* I HAVE STARTED **********
Constructing Bigears
Initial Context
log4j: Trying to find [log4j-jms.properties] using context classloader sun.misc.Launcher$AppClassLoader@2c2bbd86.
log4j: Using URL [file:/Users/kevin/Desktop/apache-activemq-5.3.0/example/target/classes/log4j-jms.properties] for automatic log4j configuration.
log4j: Reading configuration from URL file:/Users/kevin/Desktop/apache-activemq-5.3.0/example/target/classes/log4j-jms.properties
log4j: Parsing for [root] with value=[INFO, stdout].
log4j: Level token is [INFO].
log4j: Category root set to INFO
log4j: Parsing appender named "stdout".
log4j: Parsing layout options for "stdout".
log4j: Setting property [conversionPattern] to [%d %-5p %c - %m%n].
log4j: End of parsing for "stdout".
log4j: Parsed "stdout" options.
log4j: Parsing for [org.apache] with value=[ERROR, stdout].
log4j: Level token is [ERROR].
log4j: Category org.apache set to ERROR
log4j: Parsing appender named "stdout".
log4j: Appender "stdout" was already parsed.
log4j: Handling log4j.additivity.org.apache=[null]
log4j: Parsing for [demo] with value=[DEBUG,jms].
log4j: Level token is [DEBUG].
log4j: Category demo set to DEBUG
log4j: Parsing appender named "jms".
log4j: Setting property [initialContextFactoryName] to [org.apache.activemq.jndi.ActiveMQInitialContextFactory].
log4j: Setting property [topicBindingName] to [topic.logTopic].
log4j: Setting property [topicConnectionFactoryBindingName] to [ConnectionFactory].
log4j: Setting property [providerURL] to [tcp://localhost:61616].
log4j: Getting initial context.
log4j: Looking up [ConnectionFactory]
log4j: About to create TopicConnection.
log4j: Creating TopicSession, non-transactional, in AUTO_ACKNOWLEDGE mode.

Here it just hangs and eventually times out

A: 

As you didn't provide much details (on the configuration, logs, traces, etc), may I ask if you followed How do I use log4j JMS appender with ActiveMQ. If yes, and if this sample was working, what changes did you make.

As a side note, instead of log4j, I would consider using logback, its successor. Check out its Appenders.

Pascal Thivent
Followed that example exactly and with ActiveMQ 5.3 where it is supplied with the code it does not work out of the box. I'll have a look at logback, but one of my main criteria for this work is that I have to work transparently with existing code that has log4J logging already in the code. I did try plugging in slf4J but that did not solve the issue with ActiveMQ. I have added some example code,config and log output to the original question now that I have the code to hand again
Kevin
A: 

You might try using the AsynchAppender which allows for the log4j threads to NOT block (i.e. hang) on a log statement if there's an error. I was able to do this for a JMSAppender and a console appender both within an AsynAppender. However, you have to change your log4j.properties file to a log4j.xml file config. format.

joe blow
A: 

Finally seemed to be an issue with the loading of the log4j config, with the JMS config in when the JMS appender was making the connection. If I load the config with no ActiveMQ log levels or appenders defined then the problem does not occur once the JMSAppender is attached then I can load the additional config to allow it to log to JMS

Kevin