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

  • log4j not logging

    Before asking, I looked at several posts (1, 2, 3, 4 and 5) which seemed related, and tried out everything which seemed relevant - still no success.

    I have a pretty straightforward log4j.xml file:

    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="QooxdooServletTestBedLog" class="org.apache.log4j.FileAppender">
    		<param name="File" value="/var/log/qxapp.log" />
    		<param name="Append" value="true" />
    		<layout class="org.apache.log4j.SimpleLayout" />
    	</appender>
    
    	<appender name="Console" class="org.apache.log4j.ConsoleAppender">
    		<param name="Threshold" value="DEBUG" />
    		<layout class="org.apache.log4j.PatternLayout">
    			<param name="ConversionPattern" value="%d{yyyy-MM-dd} %-5p - %r - %c - %t - %m\n" />
    		</layout>
    	</appender>
    
    	<logger name="com.test.jsonrpc">
    		<level value="debug" />
    		<appender-ref ref="Console" />
    	</logger>
    
    	<logger name="com.test.test">
    		<level value="debug" />
    		<appender-ref ref="Console" />
    	</logger>
    
    	<root>
    		<priority value="debug" />
    		<appender-ref ref="Console" />
    	</root>
    
    </log4j:configuration>
    Momentarily it's somewhat garbled because I first tried with a file, then with a console appender, and left all loggers and the like in it.

    The file is loaded via a org.springframework.util.Log4jConfigurer configured in applicationContext.xml:

    Code:
    	<bean id="log4jInitialization"
    		class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    		<property name="targetClass" value="org.springframework.util.Log4jConfigurer" />
    		<property name="targetMethod" value="initLogging" />
    		<property name="arguments">
    			<list>
    				<value>file:/WEB-INF/log4j.xml</value>
    			</list>
    		</property>
    	</bean>
    I know for sure the file is read, i.e. that the value file:/WEB-INF/log4j.xml is right, because if I change it to something invalid I get a FileNotFoundException in tomcat's logs.

    By analyzing the stack trace and the behavior of the application in case a bad log4j.xml path is passed to the configurer, I got pretty confident that the configurer gets called before the servlet which does the logging gets called - it I set a breakpoint in the servlet, when running with the debugger, I never reach it, if the path to log4j.xml is not right.

    In my code, I have the following:

    Code:
    		Logger logger = Logger.getLogger("com.test.jsonrpc.JSONRPCServlet");
    		logger.debug("Received request: " + requestString);
    and many more logging statements. I temporarily replaced this.getClass().getName() with a string constant just to be sure about what's passed to getLogger(), but I also tried passing the class name, with no success.

    I get no exception at all. The code which actually does something works without a glitch. Part of it does also logging and is packed inside a bean which is loaded via the same applicationContext.xml where the log configurer is configured. Nevertheless, I can't see any trace of the log statements outputting something, neither in /var/log/qxapp.log, nor in tomcat's own logs, no matter which appender I refer in the <logger/> elements or in the <root/> element inside log4j.xml.

    I created the file /var/log/qxapp.log in advance, and set tomcat6's group and owner on it. Before that, I didn't create a file, but configured log4j.xml to create the log file inside tomcat's logs directory. The log file was not created.

    I'm running tomcat6 on karmic. I replaced the openjdk jvm with Sun's jvm, since tomcat wouldn't start in remote debug mode using openjdk.

    What am I doing wrong? Logging seems the only hurdle I have to take before being able to actually start working on the application to be developed.
    Last edited by flj; May 12th, 2010, 11:00 AM. Reason: To mark as solved

  • #2
    Hi

    Hope the following thread helps you

    How to use Log4j in your Spring application?

    Further to this, inside your class, you can use

    Code:
    private static Log logger = LogFactory.getLog(Your Classname.class);
    Let me know if this helps!

    Thanks
    Priya

    My blog: Java programming tips

    Comment


    • #3
      Not helping

      No it doesn't help. I successfully logged via log4j in a desktop application. It only happens inside tomcat. Log files are not created, and none of the messages get through, even if I log via the console appender, in which case I'd expect the messages to show up in tomcat's logs.

      There are several threads concerning the same problem - I listed the ones which seemed relevant to me in the initial post. I tried what seemed useful, but nothing helped.

      Comment


      • #4
        Got it!

        One thing I have disregarded (and which other web app/tomcat newbies like me are IMO likely to disregard too) are policy files for tomcat. With tomcat6 on kubuntu, in the folder /var/lib/tomcat6/conf/policy.d, there are files which are interpreted in alphabetical order. As long as your package/your web app doesn't get permissions granted there, logging to files can't work.

        Whoever wants to add permissions for his web app, can do so in one of the files there. The names of the files are self-explaining, and there are comments inside explaining what a file is for.

        In order to understand how to grant permissions, look at the file <nn>catalina.policy, where <nn> stands for a two-digits number. The permissions for tomcat itself to access files in the logs folder are granted in <nn>catalina.policy, and can be used as an example for adding similar permissions in other files.

        As long as you properly configure and package your app, there is no need to interfere with tomcat's own logging mechanism in order to use log4j from within your application.

        Another file which is useful for debugging the problem (on karmic) is /var/log/daemon.log. Whenever jsvc (the java service launcher) errs or needs to warn of something, or when tomcat or a web app it runs do so in a non-critical way, if logging is damaged (during experimentation I got tomcat not to log anything at all several times), it will appear in that log.

        I won't paste any code in here because on one hand the format of the files may change in future tomcat versions, and on the other hand the policy files themselves are quite self-explaining.

        Comment


        • #5
          Similar problem solved

          I had a similar problem and solved this by putting the log4j entry in the web xml and making sure it is first to get read [ie at the top of the file].

          <!-- Log4j configuration -->
          <context-param>
          <param-name>log4jConfigLocation</param-name>
          <param-value>/WEB-INF/log4j.properties</param-value>
          </context-param>
          <context-param>
          <param-name>log4jRefreshInterval</param-name>
          <param-value>1000</param-value>
          </context-param>
          <listener>
          <listener-class>org.springframework.web.util.Log4jConfigList ener</listener-class>
          </listener>

          Comment


          • #6
            I suppose you had tomcat's security disabled?

            Or the policy files already edited? 'Cause otherwise there's no way of working, other than a serious security flaw in tomcat.

            Comment


            • #7
              Hi flj,
              <logger name="com.test.jsonrpc">
              <level value="debug" />
              <appender-ref ref="Console" />
              </logger>.
              I think you are missing a reference of the configuration done. Please try adding a reference from the
              <appender name="QooxdooServletTestBedLog" class="org.apache.log4j.FileAppender">
              <param name="File" value="/var/log/qxapp.log" />
              <param name="Append" value="true" />
              <layout class="org.apache.log4j.SimpleLayout" />
              </appender>
              So the new configuration should look like
              <logger name="com.test.jsonrpc">
              <level value="debug" />
              <appender-ref ref="Console" />[B]
              <appender-ref ref="QooxdooServletTestBedLog"/>
              </logger>

              Please add these and let me know if its working. Also try this. Dont create a log folder and see if you are getting a warning saying that /var/log/qxapp.log doesn't exist. By this you can assure yourself that the log4j.xml is being parsed.

              Comment


              • #8
                You don't get it

                The problem was that I had tomcat security enabled, and there were several permissions missing from the policy files. Other than that the configuration was proper. I had posted again just so other people having a similar problem might use what I have found. Once I added the permissions in the policy files, everything worked properly.

                Comment

                Working...
                X