简体   繁体   English

Java Web应用程序中的Log4j错误

[英]Log4j error in java Web Application

I am trying to add Log4j in my Java EE project. 我试图在我的Java EE项目中添加Log4j。 I have added the log4j.jar file to my WEB-INF/lib folder and try to implement log4j in one of the Servlets in my project. 我已将log4j.jar文件添加到我的WEB-INF / lib文件夹中,并尝试在项目中的Servlet之一中实现log4j。

Initially I tried it out by using log4j with a basic configuration (without using the log4j.properties file). 最初,我通过使用具有基本配置的log4j(不使用log4j.properties文件)进行了尝试。

My Servlet 我的Servlet

import org.apache.log4j.*;

public class classname extends HttpServlet {
    private static final long serialVersionUID = 1L;

    private static Logger log = Logger.getLogger(classname.class);

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

BasicConfigurator.configure();

log.debug("************************Test Message from Log4j*****************************");

I tried this without the log4j.properties file but I am getting an error like: 我在没有log4j.properties文件的情况下尝试了此操作,但出现了如下错误:

log4j:WARN No appenders could be found for logger (org.apache.axis.i18n.ProjectResourceBundle).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

I tested the same in another Java application (not web), it works fine with a basic configuration. 我在另一个Java应用程序(不是Web)中进行了测试,它在基本配置下可以正常工作。

Is there any thing I need to add in my web project? 我需要在Web项目中添加任何内容吗?

All these are warnings.To avoid these warnings 所有这些都是警告。为避免这些警告

Configure the log4j in web.xml first place.I think you configured in some part of wex.xml. 首先在web.xml中配置log4j。我认为您在wex.xml的某些部分中进行了配置。

If you configure that log4j at top then you can avoid these warnings. 如果在顶部配置该log4j,则可以避免这些警告。

While loading Axis, log4j is using the default search method for finding a log4j.properties and it's not finding one. 加载Axis时,log4j使用默认的搜索方法查找log4j.properties,但未找到。 This will always cause that warning. 这将始终导致该警告。 I expect the same will happen for each sperate log4j implementation on your classpath. 我希望您的类路径上的每个单独的log4j实现也会发生同样的情况。 It depends on which class loader is handling the loading of Axis to determine where it will look for and find a properties file. 这取决于哪个类加载器正在处理Axis的加载,以确定它将在哪里查找并找到属性文件。

You don't have to declare nothing in your web.xml. 您无需在web.xml中声明任何内容。

Because your project is a web application the config file should be on WEB-INF/classes after deployment. 由于您的项目是Web应用程序,因此配置文件在部署后应位于WEB-INF / classs上。 I advise you to create a java resource folder to do that. 我建议您创建一个Java资源文件夹来做到这一点。 Another approach is to put the config file in your src/main/java. 另一种方法是将配置文件放在src / main / java中。

Beware with the configuration name file. 当心配置名称文件。 If you are using xml the file name is log4j.xml otherwise log4j.properties. 如果使用的是xml,则文件名为log4j.xml,否则为log4j.properties。

Write a custom class that implements ServletContextListener like the one shown below. 编写一个实现ServletContextListener的自定义类,如下所示。 The class looks for a context param named log4jFileName that is defined in web.xml. 该类查找在web.xml中定义的名为log4jFileName的上下文参数。 This locates the log4j XML configuration file. 这将找到log4j XML配置文件。 This will configure log4j before other classes use it. 这将在其他类使用log4j之前对其进行配置。

Custom listener:

public class StartupListener implements ServletContextListener
{
    @Override
    public void contextDestroyed(ServletContextEvent arg0)
    {
        // Cleanup code goes here
    }

    @Override
    public void contextInitialized(ServletContextEvent sce)
    {
        Logger logger = null;
        ServletContext servletContext = sce.getServletContext();
        String log4jFile = servletContext.getInitParameter("log4jFileName");
        DOMConfigurator.configure(log4jFile);
        logger = LogManager.getLogger(StartupListener.class.getName());
        logger.debug("Loaded: " + log4jFile);
    }
}
  1. In your web.xml define the listener and a context param like so 在您的web.xml中,定义侦听器和上下文参数,如下所示

      <context-param> <param-name>log4jFileName</param-name> <param-value>app/log4j/config/log4jConfig.xml</param-value> </context-param> <listener> <listener-class> com.yourpackage.YourListener </listener-class> </listener> 
  2. Now configure your log4jConfig.xml appropriately to ensure that your application logs are logged under an appropriate path. 现在,适当地配置log4jConfig.xml,以确保将应用程序日志记录在适当的路径下。 Use a file appender like RollingFileAppender to handle the retention policy for your applications. 使用RollingFileAppender之类的文件追加器来处理应用程序的保留策略。

Here is a sample log4j XML to help you 这是一个示例log4j XML可以帮助您

Sample log4j XML: (Customize as necessary)

<?xml version="1.0" encoding="UTF-8"?>
<log4j:configuration xmlns:log4j = "http://jakarta.apache.org/log4j/" debug = "false">
    <appender name = "FILE" class = "org.apache.log4j.RollingFileAppender">
        <param name = "File" value = "/usr/local/path/to/application.log"/>
        <param name = "MaxFileSize" value = "5MB"/>
        <param name = "MaxBackupIndex" value = "50"/>
        <layout class = "org.apache.log4j.PatternLayout">
            <param name = "ConversionPattern" value = "[%d{dd/MM/yy hh:mm:ss:sss z}] %5p %c{2}: %m%n"/>
        </layout>
    </appender>
    <category name = "com.your.application.package">
        <priority value = "debug"/>
    </category>
    <category name = "org.apache.catalina">
        <priority value = "error"/>
    </category>
    <category name = "org.hibernate">
        <priority value = "error"/>
    </category>
    <root>
        <priority value = "info"/>
        <appender-ref ref = "FILE"/>
    </root>
</log4j:configuration>

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

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