如何使用JMS Appender? [英] How to use JMS Appender?
问题描述
关于JMS Appenders的Inmy研究我发现 turorial1 和 tutorial2 。我试图关注它们,但我无法运行示例程序。
Inmy research about JMS Appenders I've found turorial1 and tutorial2 . I've tried to follow them, but I couldn't run example program.
我创建了文件log4j.properties
Fistly I created file 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
and jndi.properties
topic.logTopic=logTopic
然后我将Receiver.java添加到我的项目
Then I added Receiver.java to my project
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();
}
}
}
我需要让Receiver聚集来自项目的所有日志,但我甚至无法运行这个简单的例子。可能我不知道如何正确配置它,因为我得到了这个输出:
I need make Receiver to gather all logs from project, but I cannot even run this simple example. Probably I don't know how to configure it correctly, because I get this output:
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.
我是否错过了在代码或某些文件中添加一些行到classpath?我是log4j的新手。
Did I miss adding some lines in code or some files to classpath? I'm new to log4j.
编辑:
我在 AspectJ
类中设置了Logger。但我认为在 Receiver
中也会创建Logger并发送日志,所以可能应该在 Receiver
中完成,不在我项目的其他课程中。
I set Logger in AspectJ
class. But I think that also in Receiver
Logger is created and log is sent, so probably it should be done in Receiver
, not in other class in my project.
static final Logger logger = Logger.getLogger(ReportingAspect.class);
@Before("setLoggingFile()")
public void setProperties() {
PropertyConfigurator.configure("log4j.properties");
}
ProjectJMS
|
\_ src
| \_ packages...
\_jndi.propeties
\_log4j.properties
推荐答案
要配置log4j,请使用:
-Dlog4j.configuration =配置文件的路径
To configure log4j use : -Dlog4j.configuration= path to config file
conf的路径文件可以是:$ b $bÀ文件在类路径之外的路径中,如果是这样的话,请使用file:///,例如:
Path to conf file CAN be: À file in a path outside of classpath , if so préfix it with file:/// , example:
- -Dlog4j.configuration = file:/ c:/foobar.lcf
在类似路径中的其他情况:
Else in classpath in That case:
- -Dlog4j.configuration = foobar.lcf其中foobar.lcf位于源文件夹的根目录
参见:
- http://logging.apache.org/log4j/1.2/manual.html
对于jms:
- 至少添加jms.jar到classpath
-Add jms.jar at least to classpath
- 确保您运行JMS代理(例如activemq)
Regar ds
Philippe
这篇关于如何使用JMS Appender?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!