Announcement Announcement Module
Collapse
No announcement yet.
log4j with spring Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • log4j with spring

    While trying to test a program using Eclipse 3.2.2, Spring... i encounter this message on Eclipse console:

    Code:
    log4j:WARN No appenders could be found for logger (org.springframework.util.ClassUtils).
    log4j:WARN Please initialize the log4j system properly.
    The program is a basic stand alone program and is executed from the eclipse ide... stepping into the code, i think it's these lines that cause the message:


    Code:
        ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"dpsdk/dpsdk.xml"});
    	MySqlEmployeeDao empdb = (MySqlEmployeeDao)ctx.getBean("MySqlEmployeeDao");

    I've only been using
    Code:
    System.out.println
    statements for debugging and I want to move into logging

    How do I set this up properly?

    below is the xml file that's setup for this test..
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
    <beans>
    
    	<bean id="dataSource"
    		class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    		<property name="driverClassName"
    			value="com.mysql.jdbc.Driver">
    		</property>
    		<property name="url" value="jdbc:mysql://127.0.0.1/pay"></property>
    		<property name="username" value="root"></property>
    		<property name="password" value="wowiesy"></property>
    	</bean>
    	<bean id="MySqlEmployeeDao" class="dpsdk.dao.MySqlEmployeeDao" >
    		<property name="dataSource" ref ="dataSource"></property></bean>
    
    </beans>

  • #2
    You need log4j jar and a log4j.properties file in your classpath. Statements like

    Code:
    public static Logger LOG = Logger.getLogger(MyClass.class);
    .
    .
    .
    LOG.debug("Your log statement here!");
    gets you going. Check out http://logging.apache.org/log4j/docs/index.html

    Good luck!

    Comment


    • #3
      Just to add, if you have a look at the examples that ship with Spring these have log4j.properties files in e.g. petclinic\war\WEB-INF\log4j.properties. That might get you started.

      Comment


      • #4
        I get the same message when using
        Code:
        <context-param>
        	<param-name>log4jConfigLocation</param-name>
        	<param-value>/WEB-INF/classes/net/acme/resources/log4j.xml</param-value>
        </context-param>
        in my web.xml. I guess (never verified this) that when the log4j base classes get loaded (static) without correct configuration (via log4j.xml or properties file), the warnings get issued. If you only get those two warnings, but logging afterwards is ok, I wouldn't worry.

        Cheers
        Simon

        Comment


        • #5
          Originally posted by sniederb View Post
          I get the same message when using
          Code:
          <context-param>
          	<param-name>log4jConfigLocation</param-name>
          	<param-value>/WEB-INF/classes/net/acme/resources/log4j.xml</param-value>
          </context-param>
          in my web.xml. I guess (never verified this) that when the log4j base classes get loaded (static) without correct configuration (via log4j.xml or properties file), the warnings get issued. If you only get those two warnings, but logging afterwards is ok, I wouldn't worry.

          Cheers
          Simon
          But there's no logging happening.. or am I looking at the wrong place? When I was trying to experiment with hibernate (with the given sample application downloaded with the hibernate files) and the log output were actually shown in the eclipse console. But now that I am trying to experiment with Spring, these two lines appear and no other log messages (from spring) come out in the eclipse console. I'm still stuck with System.out.println statements for my debug output.

          By the way, I have a log4j.properties file in the ./bin folder of this test program that I mentioned above. Any more clues?

          Comment


          • #6
            got it...

            i put a line like this in the class declaration section (where the class variables are):

            Code:
            static final Logger logger = logger.getLogger(MyTest.class);
            and at the constructor of the class:

            Code:
            BasicConfigurator.configure();
            Right now I get too much logging... mostly from Spring. I searched my computer for log4j.properties files and I found a lot and all of them have their settings to WARN. How do I know which file is the test program using? Where does the program usually look for the log4j.properties?

            Comment


            • #7
              I still couldn't find which actual log4j.properties file my test program is reading. I did a search of log4j.properties in my drive and found more than 20 of them in different places. Where does log4j look for this file in default?

              Comment


              • #8
                I recommend a throughout reading of the docs/Wiki etc at the log4j homepage.

                Comment


                • #9
                  May i know what server you are using for development and also, can you post the Log4j.properties ?

                  Comment


                  • #10
                    I think it's going to look on the root of the classpath. I've had problems before when there are multiple ones there and the wrong one gets loaded.

                    Comment

                    Working...
                    X