-1

Not printing in log file using Log4j.xml. I'm getting value in console, but not in file, even file is getting created

FileLog4j.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM  "http://logging.apache.org/log4j/1.2/apidocs/org/
 apache/log4j/xml/doc-files/log4j.dtd">
 <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="true">

    <appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">
        <param name="File" value="Logger.log" />
        <param name="Append" value="true"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d %-5p %c{1}:%L %m %n" />
        </layout>
    </appender>
    
    <appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d %-5p %c{1}:%L %m %n" />
        </layout>
    </appender>


    <!-- sets the priority log level for org.springframework -->
    <logger name="org.springframework">
        <level value="info" />
    </logger>

    <!-- sets the default priority log level -->
    <root>
        <priority value="info"></priority>
        <appender-ref ref="fileAppender" />
        <appender-ref ref="consoleAppender" />
    </root>
    
 </log4j:configuration>

File Class:

    public class logsample {
    
    static final Logger logger = Logger.getLogger("logsample.class");
    DOMConfigurator.configure("C:/---- location path of my log4j.xml file----");
    logger.info(" @@@@@ FileAppender Message ==> HI");

File WEB.xml:

  <?xml version="1.0" encoding="UTF-8"?>
  <web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" >
  <display-name>Member Portal</display-name>
  <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:applicationContext.xml
            classpath:spring-database.xml 
            classpath:spring-application-flow.xml 
            classpath:spring-member.xml     
        </param-value>
    </context-param>
    
    <!-- location of log4j config file -->
    
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>/WEB-INF/classes/log4j.xml</param-value>
    </context-param> 
    
  <filter>    
    <filter-name>struts2</filter-name>    
    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>    
  </filter> 
  
   <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
   </filter-mapping>
         
    <listener>
        <listener-class>
    org.springframework.web.context.ContextLoaderListener
    </listener-class>
    </listener>
    
        <!-- applies log4j configuration -->
    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener
    </listener-class>
    </listener>

  
    <servlet>
       <servlet-name>InitServlet</servlet-name>
       <servlet-class> --- </servlet-class>
       <load-on-startup>1</load-on-startup>
     </servlet>  
    
 

   </web-app>

like so I placed my log4j.xml file in both places, JBOSS/bin/ and in web-inf/classes/

Correct me if I'm Wrong? I'm Using JBOSS 7.1 and Maven, struts2-spring based app.

Roman C
  • 49,761
  • 33
  • 66
  • 176
user2326298
  • 1
  • 1
  • 5

3 Answers3

0

You have mismatch logger configuration and instance implementation of the logger. To use log4j logger you should import corresponding class Logger. Put it in import section

import org.appache.log4j.Logger;

also to instantiate the logger you need either put Class param or use FQCN.

private static final Logger logger = Logger.getLogger(LogSample.class);

Follow the Java naming conventions and capitalize your class name.

FilterDispatcher is deprecated since Struts 2.2.1. Use the StrutsPrepareAndExecuteFilter instead in the web.xml.

<filter-class>
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • 1
    Thanks Roman C. But Eventhough I'm Not Getting any value printed on my log file.. Any Other Suggesstions??? – user2326298 Apr 28 '13 at 11:50
  • you need to put jog4j.jar to the `WEB-INF/lib` if there's no such jar. Also use full path name `` and log level ``. There's an option to add ``. – Roman C Apr 28 '13 at 12:03
  • Yes, I did whatever u said upto now.. but still not getting desired output.. what do you mean by ???? – user2326298 Apr 28 '13 at 12:06
  • In the class you instantiate a logger then `org.yourappname` is the package name where that class. – Roman C Apr 28 '13 at 12:08
  • Remove `log4jConfigLocation` and `DOMConfigurator.configure(` it's not required. – Roman C Apr 28 '13 at 12:14
  • Roman C, i think the issue is about placing the jars and files in respecting location. I'm Not getting any ERRORS in my Console.. And i have Created a 'classes' Folder under WEB-INF and placed my log4j.xml there. I included log4j.jar dependency in pom.xml, but its not getting download. So i added log4j.jar as external jar. – user2326298 Apr 28 '13 at 12:14
  • I think you don't understand me, I said to put it in `WEB-INF/lib` – Roman C Apr 28 '13 at 12:15
  • you have probably not fixed every error fixes I've commented here. – Roman C Apr 28 '13 at 12:17
  • Yes Roman C, I placed in lib only. I Just Said what i did. I found someone answered like, put log4j.xml in web-inf/classes/log4j.xml.. So i created a folder called classes in web-inf and placed my xml there. I thought its also a issue. so i just let you to know more about my app locations – user2326298 Apr 28 '13 at 12:21
  • It's not nessesary to put it manually there but it should be there after build. Also may be unrelative the Struts2 uses `commons-logging` and you might use `static final Logger LOG = LoggerFactory.getLogger(` instead of your code. Again `Logger` is not the same so you should fix the imports. – Roman C Apr 28 '13 at 12:26
  • Roman C, I’m Facing some Issue, Can you please Look here.., http://stackoverflow.com/questions/21474295/getting-ognlnoconversationpossible-error – user2326298 Feb 03 '14 at 13:51
0

Modify the name of xml file to log4j2.xml because 2 is latest version. And don't create log file it will automatically created because of code we provided in xml file.

0

Modify the name of xml file to log4j2.xml because 2 is latest version. And don't create log file it will automatically created because of code we provided in xml file and refresh the project