简体   繁体   中英

Not Printing in Log File Using Log4j.xml

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

File Log4j.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.

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>

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM