How can I fix log4j warning messages in Tomcat

2019-08-27 07:43发布

问题:

I am new to JMS and Activemq.using Google and stack overflow help I am sending messages from my Java program to Activemq using the following code.

  package PackageName;
  import java.io.IOException;
  import javax.jms.Connection;
  import javax.jms.ConnectionFactory;
  import javax.jms.Queue;
  import javax.jms.Session;
  import javax.jms.TextMessage;
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import org.apache.activemq.ActiveMQConnectionFactory;
 //@SuppressWarnings("serial")
 public class MessageProducer extends HttpServlet {
@Override
protected void service(HttpServletRequest arg0, HttpServletResponse arg1)
        throws ServletException, IOException {
    try {
        //created ConnectionFactory object for creating connection 
        ConnectionFactory factory = new ActiveMQConnectionFactory("admin","admin","tcp://localhost:61617");
        //Establish the connection
        Connection connection = factory.createConnection();
        Session session = connection.createSession(false,
        Session.AUTO_ACKNOWLEDGE);
        Queue queue = session.createQueue("MessageTesing123");
        //Added as a producer
        javax.jms.MessageProducer producer = session.createProducer(queue);
        // Create and send the message
        TextMessage msg = session.createTextMessage();
        msg.setText("TestMessage3");
        producer.send(msg);
    } catch (Exception e) {
        // TODO: handle exception
    }
}
}

If I run this .java file using Apache Tomcat,it's working fine but I am getting the following warnings.

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.

How can I fix this.

Thanks.

回答1:

This is not an ActiveMq warning; it's a log4j warning.

  1. If you're running in Tomcat, get a standard log4j.properties or log4j.xml file.

  2. Configure it for your environment.

  3. Place log file at <TOMCAT>/webapps/<MyProject>/WEB-INF/classes, or, put in WAR file under same relative path: /WEB-INF/classes



回答2:

No appenders could be found for logger ...

Means you dont have a logger relating to a rootLogger. Which means, WireFormatNegotiator is trying to log something, but finds no logger. Neither console nor file or anything else.

For example, in my project I log all logging statements from package com.foo so my properties looks like :

log4j.logger.com.foo=DEBUG, MYPROJECT
... 

and later on I have

 log4j.appender.MYPROJECT=org.apache.log4j.RollingFileAppender

and other RollingFileAppender details. The log4j.properties should be in your class path. Typical location is web-inf/classes.