如何使用JMS Appender?
在有关JMS Appender的调查中,我找到了turorial1和tutorial2。我尝试遵循它们,但是无法运行示例程序。
我首先创建了文件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();
}
}
}
我需要使Receiver收集项目中的所有日志,但是我什至无法运行此简单示例。可能我不知道如何正确配置它,因为我得到了以下输出:
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
类中设置Logger
。但是我认为在Receiver
Logger中也会创建日志并发送日志,因此可能应该在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
回答:
要配置log4j,请使用:-Dlog4j.configuration =配置文件的路径
conf文件的路径可以是:Àfile位于classpath之外的路径中,如果这样,请使用file:///对其进行前缀修复,例如:
- -Dlog4j.configuration =文件:/ c:/foobar.lcf
在这种情况下,其他在类路径中:
- -Dlog4j.configuration = foobar.lcf,其中foobar.lcf在源文件夹的根目录下
看到:
- http://logging.apache.org/log4j/1.2/manual.html
对于jms:
-将jms.jar至少添加到classpath
- 确保您正在运行JMS代理(例如activemq)
问候
菲利普
以上是 如何使用JMS Appender? 的全部内容, 来源链接: utcz.com/qa/434680.html