Announcement Announcement Module
Collapse
No announcement yet.
Log4j not working well when log messages are appeneded to a file Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Log4j not working well when log messages are appeneded to a file

    I am using Log4j as my logger with Spring. I did everything as per the basic tutorial and tried to write log messages in a log file. But every attempt to this is failed

    my web.xml file is
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    	id="WebApp_ID" version="2.5">
    	<display-name>Erp</display-name>
    	<context-param>
    		<param-name>webAppRootKey</param-name>
    		<param-value>Erp</param-value>
    	</context-param>
    	<context-param>
    		<param-name>log4jConfigLocation</param-name>
    		<param-value>/WEB-INF/log4j.xml</param-value>
    	</context-param>
    	<listener>
    		<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    	</listener>
    	<context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>/WEB-INF/applicationContext-hibernate.xml,/WEB-INF/Erp-servlet.xml</param-value>
    	</context-param>
    	<listener>
    		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    	</listener>
    	<servlet>
    		<servlet-name>Erp</servlet-name>
    		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    		<load-on-startup>2</load-on-startup>
    	</servlet>
    	<servlet-mapping>
    		<servlet-name>Erp</servlet-name>
    		<url-pattern>*.do</url-pattern>
    	</servlet-mapping>
    	<!-- Configuration for Jersey Web Services starts here -->
    	<servlet>
    		<servlet-name>jersey</servlet-name>
    		<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    	<init-param>
    		<param-name>com.sun.jersey.config.property.packages</param-name>
    		<param-value>org.pack.pack1.pack2</param-value>
    	</init-param>
    		<load-on-startup>1</load-on-startup>
    		</servlet>
    	<servlet-mapping>
    		<servlet-name>jersey</servlet-name>
    		<url-pattern>/services/*</url-pattern>
    	</servlet-mapping>
    	<!-- Configuration for Jersey Web Services ends here -->
    	<session-config>
    		<session-timeout>10</session-timeout>
    	</session-config>
    	<welcome-file-list>
    		<welcome-file>index.jsp</welcome-file>
    	</welcome-file-list>
    	<error-page>
    		<exception-type>java.lang.Exception</exception-type>
    		<location>/WEB-INF/jsp/uncaughtException.jsp</location>
    	</error-page>
    </web-app>
    The log4j.properties file is

    Code:
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
    <log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'><!--
    	<appender name="Erp-stdout" class="org.apache.log4j.ConsoleAppender">
    		<layout class="org.apache.log4j.PatternLayout">
    			<param name="ConversionPattern" value="%d{HH:mm:ss,SSS} %5p [%-20c{1}] %m%n"/>
    		</layout>
    	</appender>
    	
    	--><appender name="application.log" class="org.apache.log4j.RollingFileAppender">
    		<param name="MaxFileSize"  value="1024KB"/>
    		<param name="MaxBackupIndex" value="4"/>
    
    		<param name="File" value="/WEB-INF/application.log"/>
    		
    		<layout class="org.apache.log4j.PatternLayout">
    			<param name="ConversionPattern" value="%d %-5p [%-c{1}] - %m%n"/>
    	   </layout>
    	</appender>
    	
    	<category name="org.tbingo.project">
    		<priority value="info" />
    		<appender-ref ref="Erp-stdout"/>
    	</category>
    	
    </log4j:configuration>
    Then in my controller i wrote a code to add message to text file
    Code:
    	private static Logger log = Logger.getLogger(Controller.class);
    
            @RequestMapping(method = RequestMethod.POST)
    	public String setupForm(LoginReg model,Model model1) {
        	System.out.println("Inside Login Controller");
        	log.info("Inside a method of Login Controller");
            //log.debug("Inside a method of Login Controller");
        	//log.error("I think there is an error!!!");
    No matter what, it's not working. but when i try to print it in console, its printing. Please help me where i went wrong?

  • #2
    Hi friends,

    i googled a lot and find no working solutions. Is there someone to help me?

    Comment


    • #3
      Code:
      	<category name="org.tbingo.project">
      		<priority value="info" />
      		<appender-ref ref="Erp-stdout"/>
      	</category>
      You haven't configured the logger to append to the file log. Try:

      Code:
      	<category name="org.tbingo.project">
      		<priority value="info" />
      		<appender-ref ref="application.log"/>
      	</category>
      The following wiki page is pretty helpful on configuration http://wiki.apache.org/logging-log4j/Log4jXmlFormat. Configuring a root logger as described in that wiki page generally makes things a lot easier as you don't need to specify the appenders explicitly for each category.

      Comment


      • #4
        Originally posted by cmelgar View Post
        You haven't configured the logger to append to the file log. Try:

        Code:
        	<category name="org.tbingo.project">
        		<priority value="info" />
        		<appender-ref ref="application.log"/>
        	</category>
        The following wiki page is pretty helpful on configuration http://wiki.apache.org/logging-log4j/Log4jXmlFormat. Configuring a root logger as described in that wiki page generally makes things a lot easier as you don't need to specify the appenders explicitly for each category.

        I tried that too. No use. I have the application.log file under WEB-INF and log4j.xml under the same location, but still its not writing.

        I tried this code
        Code:
        <?xml version="1.0" encoding="UTF-8" ?>
        <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
        
        <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
        	<appender name="application" class="org.apache.log4j.RollingFileAppender">
        		<param name="MaxFileSize"  value="1024KB"/>
        		<param name="MaxBackupIndex" value="4"/>
        		<param name="File" value="application.log"/>		
        		<layout class="org.apache.log4j.PatternLayout">
        		<param name="ConversionPattern" value="%d %-5p [%-c{1}] - %m%n"/>
        	    </layout>
        	</appender>
        
          <root> 
            <priority value ="info" /> 
            <appender-ref ref="application" /> 
          </root>
          
        </log4j:configuration>
        And for a surprise, the log messaged get printed in console instead of File. Why this happens? I just scratched my head thinking over and over. Please get me out of this.. :o

        Comment


        • #5
          I think log4j doesn't care about a classic or web application.
          Your logfile path '/WEB-INF/...' is an absolute one, so log4j tries to write to your Linux root directory and can't. Try a valid absolute path at first (like /tmp), this way there won't be any problem regarding file/directory permissions.

          Comment


          • #6
            Originally posted by mlarchet View Post
            I think log4j doesn't care about a classic or web application.
            Your logfile path '/WEB-INF/...' is an absolute one, so log4j tries to write to your Linux root directory and can't. Try a valid absolute path at first (like /tmp), this way there won't be any problem regarding file/directory permissions.
            That works great. Thanks for the help. But is there a way i can make it access a log file under my project folder? That would be great, isn't it?

            Comment


            • #7
              I think if your file path doesn't start with a '/', then it's relative to log4j.xml file. You should give it a try.

              Comment


              • #8
                Hi.. A new Problems

                This time i came up with a new problem. Here is my log4j.xml
                Code:
                <?xml version="1.0" encoding="UTF-8" ?>
                <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
                <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
                
                	<appender name="CA" class="org.apache.log4j.ConsoleAppender">
                		<layout class="org.apache.log4j.PatternLayout">
                		<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n"/>
                		</layout>
                	</appender>
                
                	<appender name="FA" class="org.apache.log4j.RollingFileAppender">
                		<param name="File" value="/tmp/application.log"/>
                		<param name="Append" value="true"/> 
                		<param name="Threshold" value="ERROR"/>
                		<layout class="org.apache.log4j.PatternLayout">
                		<param name="ConversionPattern" value="%d{MM-dd-yyyy@HH:mm:ss} %-4r [%t] %-5p %c %x - %m%n"/>
                		</layout>
                	</appender>
                	
                	<category name="org.tbingo.project">
                		<priority value="INFO" />
                		<appender-ref ref="CA"/>
                	</category>
                     
                	<category name="org.tbingo.project.web">
                		<priority value="ERROR" />
                		<appender-ref ref="FA"/>
                	</category> 
                
                </log4j:configuration>
                Now the problem is, no logging message get printed in console. Even a INFO doesnt. But when i change the line from this

                Code:
                	<category name="org.tbingo.project">
                		<priority value="INFO" />
                		<appender-ref ref="CA"/>
                	</category>
                to

                Code:
                	<category name="org.tbingo.project">
                		<priority value="INFO" />
                		<appender-ref ref="CA"/>
                                <appender-ref ref="FA"/>
                	</category>
                it works, means get printed in console and in file. Please let me know what the problem is?

                Comment


                • #9
                  Originally posted by mlarchet View Post
                  I think if your file path doesn't start with a '/', then it's relative to log4j.xml file. You should give it a try.
                  That means? (Sorry for asking dumb questions but i have no idea what you're talking about.)

                  Comment


                  • #10
                    First, I'll answer your previous problem :

                    I think it's the 'Treshold' parameter in your appender that's preventing INFO logs, you should remove it.

                    Secondly, what I mean by relative to log4j.xml is you can try something like ../application.log if your log4j.xml is in WEB-INF/classes.

                    Comment


                    • #11
                      Originally posted by mlarchet View Post
                      First, I'll answer your previous problem :

                      I think it's the 'Treshold' parameter in your appender that's preventing INFO logs, you should remove it
                      Even if i removed that the same problem occurs. What i am saying is, INFO gets printed in console, once i refer the File Appender along with Console Appender. Otherwise ConsoleAppender fails to work.

                      .
                      Originally posted by mlarchet View Post
                      Secondly, what I mean by relative to log4j.xml is you can try something like ../application.log if your log4j.xml is in WEB-INF/classes.
                      Will try that. Thanks.
                      Last edited by rajkumar_pb; Apr 19th, 2010, 08:57 AM.

                      Comment


                      • #12
                        @mlarchet

                        Still the problem occurs. Console Appender fails to work individually. Also i copied the application.log file to WEB-INF/classes and modified the log4j.xml as mentioned by you, but it doesnt get write. If i put that outside my project folder it works like a charm.

                        Comment


                        • #13
                          Log4j pb

                          I am displaying log messages in the jsp page.
                          But log messages are not showing sometimes and for some locations not showing at all.How can i know whether there are log messages or not
                          And why log messages are not displaying
                          Last edited by swathi_log4j; Apr 15th, 2011, 06:05 AM.

                          Comment

                          Working...
                          X