2012-08-06 43 views
2

Inmy關於JMS Appenders的研究我發現turorial1tutorial2。我試圖關注他們,但我無法運行示例程序。如何使用JMS Appender?

Fistly我創建的文件log4j.properties

log4j.rootLogger=INFO, stdout, jms 

# 
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 

# 
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 

和jndi.properties

topic.logTopic=logTopic 

然後我說Receiver.java到我的項目

public class Receiver implements MessageListener { 

    public Receiver() throws Exception { 
     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); 
     Logger log = Logger.getLogger(Receiver.class); 

     log.info("Test log"); 

     Thread.sleep(1000); 
     consumer.close(); 
     sess.close(); 
     conn.close(); 
     System.exit(1); 
    } 

    public static void main(String[] args) throws Exception { 
     new Receiver(); 
    } 

    @Override 
    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(); 
     } 
    } 
} 

我需要做接收器收集項目中的所有日誌,但我甚至無法運行這個簡單的例子。也許我不知道如何正確地配置它,因爲我得到這個輸出:

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

我錯過了在添加一些代碼行或某些文件類路徑?我是log4j的新手。

編輯: 我在AspectJ類設置記錄器。但我認爲在Receiver中也會創建日誌併發送日誌,因此可能應該在Receiver中完成,而不是在我的項目中的其他類中。

static final Logger logger = Logger.getLogger(ReportingAspect.class); 

@Before("setLoggingFile()") 
public void setProperties() { 
    PropertyConfigurator.configure("log4j.properties"); 
} 

ProjectJMS 
| 
\_ src 
| \_ packages... 
\_jndi.propeties 
\_log4j.properties 
+1

WARN只是說你沒有正確設置log4j。這不是代碼不起作用的原因。 – CoolBeans 2012-08-06 14:39:38

+0

@CoolBeans正如我所說,它可能沒有配置,所以你是對的。我的問題是:如何配置它。不在源代碼中?在哪裏/怎麼樣?你能幫忙嗎? – alicjasalamon 2012-08-06 14:41:41

+0

請將您初始化記錄器的代碼以及您將屬性文件放在哪裏? – Tomer 2012-08-06 14:43:36

回答

0

配置Log4j使用: -Dlog4j.configuration =路徑配置文件

路徑的conf文件可以: 文件在類路徑之外的路徑,如果是用文件前綴是: ///,例如:

  • -Dlog4j.configuration =文件:/ C:/foobar.lcf

否則在類路徑中那情況下:

  • -Dlog4j.configuration = foobar.lcf其中foobar.lcf是在源文件夾的根

參見:

爲JMS :

- 將jms.jar至少添加到cl asspath

  • 確保你有一個JMS代理運行(ActiveMQ的舉例)

問候

菲利普

+0

我已將'activemq-all-5.60.jar'添加到類路徑中,並且在我的控制檯中運行了'activemq'。你能解釋更多的詳細信息添加配置?我在參數中添加了'-Dlog4j.configuration = log4j.properties'(在eclipse中),但它沒有幫助。可能我誤解了我應該做的事情。 – alicjasalamon 2012-08-06 14:56:36

0

您確認,您的方法setProperties()你用你的記錄器之前被稱爲?

通常只有當配置文件(log4j。屬性)在初始化log4j時出現PropertyConfigurator,或者您嘗試記錄一些內容,然後用PropertyConfigurator初始化log4j。

首先你應該確保,setProperties()被稱爲你期望它被調用的方式,如果這應該工作,你可以嘗試驗證是否可以加載配置。從我看到的,我猜你應該找到你的配置文件。爲了驗證這一點,你可以在幾個步驟加載配置,以確保,人們發現:

Properties props = new Properties(); 
    props.load(new FileInputStream("log4j.properties")); 
    PropertyConfigurator.configure(props); 

如果您的配置將不會被發現,你會收到一個FileNotFoundException

1

您需要將log4j.properties移動到'src'文件夾下,以便它將包含在類路徑中,因爲它不在其中。