Log4j JMS appender示例

时间:2012-08-06 12:23:40

标签: java log4j jms

我尝试关注this example,但是当我将此类复制到我的项目时,我无法运行它。

我不知道我的导入应该是什么样子,因为eclipse提出了很多选择。我试过了

import javax.jms.Connection;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQObjectMessage;
import org.apache.log4j.Logger;
import org.apache.log4j.spi.LoggingEvent;

但我得到了:

log4j:WARN No appenders could be found for logger (org.apache.activemq.transport.tcp.TcpTransport).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

本教程对我来说并不清楚。你知道更多细节(对于绝对的乞丐)吗?或许你知道如何用这个例外来解决我的问题吗?

编辑:

log4.properties是示例中文件的精确副本

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

2 个答案:

答案 0 :(得分:1)

我认为您在配置log4j.properties文件时遇到问题。仔细查看它的配置。您可以尝试使用google查找有关配置log4j的教程,例如look this

答案 1 :(得分:1)

这是经过测试的代码 - 在log4j.xml中为JMSAppender类创建条目,如下所示 -

<appender name="amqAppender" class="com.appender.JMSQueueAppender"> 
         <param name="brokerUri" value="failover:(tcp://host1:port,tcp://host2:port,tcp://host3:port)?randomize=false" /> 
         <param name="queueName" value="MobiviteQueue" /> 
</appender>

并使用它 -

<root>
    <level value="ERROR" />
    <appender-ref ref="amqAppender" />
</root>

上诉人类是 -

package com.appender;

import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.log4j.Appender;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.Logger;
import org.apache.log4j.spi.LoggingEvent;


public class JMSQueueAppender extends AppenderSkeleton implements Appender {

private static Logger logger = Logger.getLogger(JMSQueueAppender.class);

private String brokerUri;
private String queueName;


@Override
protected synchronized void append(LoggingEvent event) {

    try {
        //System.out.println("JMSQueueAppender -----append  method is called  brokerUri ------ "+brokerUri);
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(brokerUri);

        // Create a Connection
        javax.jms.Connection connection = connectionFactory.createConnection();
        connection.start();

        // Create a Session
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        // Create the destination (Topic or Queue)
        Destination destination = session.createQueue(queueName);

        // Create a MessageProducer from the Session to the Topic or Queue
        MessageProducer producer = session.createProducer(destination);
        producer.setDeliveryMode(DeliveryMode.PERSISTENT);

        ObjectMessage message = session.createObjectMessage(event);

        // Tell the producer to send the message
        producer.send(message);
        // Clean up
        session.close();
        connection.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}



public String getBrokerUri() {
    return brokerUri;
}



public void setBrokerUri(String brokerUri) {
    this.brokerUri = brokerUri;
}



public String getQueueName() {
    return queueName;
}



public void setQueueName(String queueName) {
    this.queueName = queueName;
}



       public void close() {
       // TODO Auto-generated method stub

       }

        public boolean requiresLayout() {
        // TODO Auto-generated method stub
        return false;
    }
}