简体   繁体   English

log4j的配置文件和继承?

[英]log4j configuration files and inheritance?

I have trouble to understand configuration of log4j when several configuration files are used. 使用多个配置文件时,我很难理解log4j的配置。

I explain my case: 我解释一下我的情况:

  • I have simple a Java EE app with modules (web, ejb, jpa) 我有一个带有模块(Web,ejb,jpa)的简单Java EE应用程序
  • I have also custom server authentication module (SAM) to handle security with Glassfish, I don't use realm directly in my app. 我还有自定义服务器身份验证模块(SAM)来处理Glassfish的安全性,我没有在我的应用程序中直接使用领域。

So, I have 2 xml configuration files because my SAM (JAR file) is stored inside GF_HOME\\glassfish\\lib and my EAR file is stored into GF_HOME\\glassfish\\domains\\domain1 . 因此,我有2个xml配置文件,因为我的SAM(JAR文件)存储在GF_HOME\\glassfish\\lib而我的EAR文件存储在GF_HOME\\glassfish\\domains\\domain1 Both JAR and EAR contain log4j.xml file. JAR和EAR都包含log4j.xml文件。

My log4j library is in GF_HOME\\glassfish\\domains\\domain1\\lib directory. 我的log4j库位于GF_HOME\\glassfish\\domains\\domain1\\lib目录中。

I want to handle configuration with 2 log4j.xml file. 我想使用2 log4j.xml文件处理配置。

So in the log4j.xml file of my SAM I have this : 因此,在我的SAM的log4j.xml文件中,我有以下内容:

<?xml version="1.0" encoding="UTF-8" ?>

<appender name="FILE" class="org.apache.log4j.RollingFileAppender">
    <param name="File" value="${catalina.home}/logs/sim.log" />
    <param name="Threshold" value="DEBUG" />
    <param name="Append" value="true" />
    <param name="MaxFileSize" value="1MB" />
    <param name="MaxBackupIndex" value="1" />

    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d %-5p %X{service} %X{user} [%c] %m%n" />
    </layout>
</appender>

<root>
    <priority value="DEBUG" />
    <appender-ref ref="FILE" />
</root>

And in log4j.xml in my app : 在我的应用程序的log4j.xml中:

<?xml version="1.0" encoding="UTF-8" ?>

 <appender name="FILE" class="org.apache.log4j.RollingFileAppender">
    <param name="File" value="${catalina.home}/logs/sim.log"/>
    <param name="Threshold" value="DEBUG"/>
    <param name="Append" value="true"/>
    <param name="MaxFileSize" value="1MB"/>
    <param name="MaxBackupIndex" value="1"/>

    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d %-5p %X{service} %X{user} [%c] %m%n"/>
    </layout>             
</appender>

<logger name="com.sim" additivity="true">
    <level value="FATAL" />     
</logger>

<root>
    <priority value="DEBUG" />
    <appender-ref ref="FILE" />
</root>

With this configuration I don't expect to see for example levels of INFO or DEBUG in my app because for logger com.sim level is FATAL but I receive all logs because it seems to apply configuration of SAM log4j.xml. 使用此配置,我不希望在应用程序中看到例如INFODEBUG级别,因为对于记录器com.sim级别为FATAL但是我收到所有日志,因为它似乎应用了SAM log4j.xml的配置。 Indeed, when I delete this file, I receive logs for my app for level FATAL . 确实,当我删除此文件时,我收到了应用程序日志,级别为FATAL

It seems that glassfish "mix" or "merge" all log4j.xml files found. 似乎glassfish“混合”或“合并”了所有找到的log4j.xml文件。 I don't understand well the principle. 我不太了解这个原理。

I just want to manage my SAM with 1 log4j.xml and manage my app with another independant log4j.xml. 我只想用1个log4j.xml管理我的SAM,并用另一个独立的 log4j.xml管理我的应用程序。

How can I do this? 我怎样才能做到这一点?

To answer my post : 回答我的帖子:

Remember that I have EAR file with log4j.xml file inside JAR in lib directory of my EAR. 请记住,我的EAR的lib目录中的JAR内有EAR文件和log4j.xml文件。

To apply new log4j configuration, I created new servlet launched at startup and used DOMConfigurator.configure() (notice that if you work with properties file you must use PropertyConfigurator.configure() not DOMConfigurator.configure() ). 要应用新的log4j配置,我创建了在启动时启动的新servlet,并使用了DOMConfigurator.configure() (请注意,如果使用属性文件,则必须使用PropertyConfigurator.configure()而不是DOMConfigurator.configure() )。

Here is my servlet : 这是我的servlet:

public class Log4jInit implements Servlet {

@Override
public void destroy() {}

@Override
public ServletConfig getServletConfig() {return null;}

@Override
public String getServletInfo() {return null;}

@Override
public void init(ServletConfig arg0) throws ServletException {

    try{        
        DOMConfigurator.configure(getClass().getClassLoader().getResource("log4j.xml"));            
    }catch(Exception e){
        System.out.println("Log4jInit Exception : " + e);
    }

}

@Override
public void service(ServletRequest arg0, ServletResponse arg1)  throws ServletException, IOException {}

}

Also, don't forget to modify your web.xml file : 另外,不要忘记修改您的web.xml文件:

<servlet>
    <servlet-name>Log4j Servlet</servlet-name>
    <servlet-class>your.package.Log4jInit</servlet-class>
    <load-on-startup> 1 </load-on-startup>
</servlet>

And that's all. 就这样。

If you want to add/override previous log4j configuration, keep this code. 如果要添加/覆盖以前的log4j配置,请保留此代码。

If you want to reset previous configuration, add LogManager.resetConfiguration(); 如果要重置以前的配置,请添加LogManager.resetConfiguration(); before LogManager.resetConfiguration(...); LogManager.resetConfiguration(...);之前LogManager.resetConfiguration(...);

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

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