如何在ActiveMQ中使用log4j2 JMSAppender [英] How to use log4j2 JMSAppender with ActiveMQ

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

问题描述

我正在努力编写一个简单的POC程序,该程序将消息记录到队列中.我发现的所有教程和问答(此处此处)使用log4j 1.2版,并将消息放到主题,而不是放在队列中.我的要求是登录到队列.

I am struggling to write a simple POC program that logs messages to a Queue. All tutorials and Q&As I find (here and here) use log4j version 1.2 and they put messages onto a Topic and not onto a queue. My requirement is to log to a queue.

我遵循了官方网站上提到的文档,但无法使其正常工作.

I followed the documentation mentioned on the official site, but was not able to get it working.

我在类路径上有log4j2和ActiveMQ JAR,我创建了队列"logQueue",我能够在ActiveMQ Web控制台中看到该队列,当我尝试执行该程序以编写日志时,得到以下信息错误:

I have log4j2 and ActiveMQ JARs on my classpath, I have created the queue "logQueue", I am able to see the queue in the ActiveMQ web console and when I try execute the program to write the logs, I get the following error:

ERROR Error creating JmsManager using ConnectionFactory [ConnectionFactory] and Destination [logQueue]. javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
    at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)
    at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)

这显然看起来像是一些JNDI问题,但无法弄清楚是什么.根据log4j 1.2教程,我还将jndi.properties文件添加到我的类路径中,值是

It clearly looks like some JNDI issue, but am not able to figure out what. As per the log4j 1.2 tutorials, I also added the jndi.properties file to my classpath with the value

queue.logQueue=logQueue

但是那显然没有帮助.下面是我的log4j2.xml

but thats not helping apparently. Below is my log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
  <Appenders>
    <JMS name="jmsQueue" destinationBindingName="logQueue"
         factoryBindingName="ConnectionFactory"
         providerURL="tcp://localhost:61616"/>
  </Appenders>
  <Loggers>
    <Root level="all">
      <AppenderRef ref="jmsQueue"/>
    </Root>
  </Loggers>
</Configuration>

谢谢!

推荐答案

在JMS元素中添加factoryName="org.apache.activemq.jndi.ActiveMQInitialContextFactory"解决了该问题.

Adding factoryName="org.apache.activemq.jndi.ActiveMQInitialContextFactory" to the JMS element solved the problem.

log4j2.xml中的最后一个JMS元素如下所示:

The final JMS element in the log4j2.xml looks like this:

<JMS name="jmsQueue" destinationBindingName="logQueue"
        factoryName="org.apache.activemq.jndi.ActiveMQInitialContextFactory"
        factoryBindingName="ConnectionFactory"
        providerURL="tcp://localhost:61616"/>

这篇关于如何在ActiveMQ中使用log4j2 JMSAppender的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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