简体   繁体   English

不使用 Log4j.xml 在日志文件中打印

[英]Not Printing in Log File Using Log4j.xml

Not printing in log file using Log4j.xml .不使用Log4j.xml在日志文件中Log4j.xml I'm getting value in console, but not in file, even file is getting created我在控制台中获得了价值,但没有在文件中获得价值,甚至文件也在被创建

File Log4j.xml :文件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 :文件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/这样我把我的log4j.xml文件放在两个地方, JBOSS/bin/web-inf/classes/

Correct me if I'm Wrong?如果我错了纠正我? I'm Using JBOSS 7.1 and Maven, struts2-spring based app.我正在使用 JBOSS 7.1 和 Maven,基于struts2-spring的应用程序。

You have mismatch logger configuration and instance implementation of the logger.您的记录器配置和记录器实例实现不匹配。 To use log4j logger you should import corresponding class Logger .要使用 log4j 记录器,您应该导入相应的类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.还要实例化记录器,您需要放置Class参数或使用 FQCN。

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

Follow the Java naming conventions and capitalize your class name.遵循 Java 命名约定并将类名大写。

FilterDispatcher is deprecated since Struts 2.2.1.自 Struts 2.2.1 起,不推荐使用FilterDispatcher Use the StrutsPrepareAndExecuteFilter instead in the web.xml .web.xml使用StrutsPrepareAndExecuteFilter代替。

<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.修改 xml 文件名称为 log4j2.xml,因为 2 是最新版本。 And don't create log file it will automatically created because of code we provided in xml file.并且不要创建日志文件,它会因为我们在 xml 文件中提供的代码而自动创建。

Modify the name of xml file to log4j2.xml because 2 is latest version.修改 xml 文件名称为 log4j2.xml,因为 2 是最新版本。 And don't create log file it will automatically created because of code we provided in xml file and refresh the project并且不要创建日志文件它会因为我们在xml文件中提供的代码而自动创建并刷新项目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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