将Log4J JMSAppender与ActiveMQ一起使用 [英] Using the Log4J JMSAppender with ActiveMQ

查看:174
本文介绍了将Log4J JMSAppender与ActiveMQ一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用log4J JMSAppender通过JMS从Log4J交付日志记录的概念证明.我已经尝试过ActiveMQ及其附带的示例.我把这个例子拆开了,使它更加通用并与多个平台兼容.

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.

看起来好像一切都正常了,因为我看到正在发生与ActiveMQ的连接,但是当我获得InitialContext时代码挂起(使用-Dlog4j.debug设置,ActiveMQ客户端类似乎调用了log4J并加载了属性,这些属性依次尝试与JMSAppender的JMS建立连接),然后代码只是挂起.我试图通过仅为单个命名记录器定义附加程序来隔离去往JMS的日志消息,并且org.apache.activemq包被配置为使用ConsoleAppender

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

当指向配置了JMS队列的Weblogic Server时,相同的代码也可以正常工作,但是为了获得最大的兼容性,我需要尝试使其与ActiveMQ一起使用

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

是否缺少一些使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

此操作的目的是创建一个名为"demo"的附加程序,并在示例代码中抓取该日志以确保activemq日志未尝试将其自身发送到JMS

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

代码示例.有点混乱,因为我一直在努力尝试以使事情正常进行.就目前而言,当我将其指向Weblogic并类似地切换log4j配置时,它将可以正常工作.这段代码中的对象是确保我的侦听器在单独的线程中运行

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);
                }
            }
        }

} }

在设置-Dlog4j.debug时显示的日志记录

******* 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.

这里只是挂起并最终超时

推荐答案

最后似乎是在装入log4j配置时出现问题,而在JMS附加程序进行连接时,JMS配置处于加载状态.如果我加载的配置没有定义ActiveMQ日志级别或附加程序,那么一旦附加了JMSAppender,就不会出现问题,那么我可以加载其他配置,以使其登录到JMS

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

这篇关于将Log4J JMSAppender与ActiveMQ一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆