log4j的初始化

1. Tomcat下的初始化 

   默认的Log4j initialization典型的应用是在web-server 环境下。在tomcat3.x和tomcat4.x下,你应该将配置文件Log4j.properties放在你的web应用程序的WEB- INF/classes 目录下。  
   Log4j将发现属性文件,并且以此初始化。这是使它工作的最容易的方法。
   你也可以选择在运行tomcat前设置系统属性Log4j.configuration 。对于tomcat 3.x,TOMCAT_OPTS 系统变量是用来设置命令行的选项。对于tomcat4.0,用系统环境变量CATALINA_OPTS 代替了TOMCAT_OPTS

 

UNIX 命令行
  export TOMCAT_OPTS="-DLog4j.configuration=foobar.txt"


告 诉Log4j用文件foobar.txt作为默认的配置文件。这个文件应该放在WEB-INF/classes 目录下。这个文件将被PropertyConfigurator所读。每个web-application将用不同的默认配置文件,因为每个文件是和它的 web-application 相关的。

  • export TOMCAT_OPTS="-DLog4j.debug -DLog4j.configuration=foobar.xml" export TOMCAT_OPTS="-DLog4j.debug -DLog4j.configuration=foobar.xml"
    告 诉Log4j输出Log4j-internal的调试信息,并且用foobar.xml作为默认的配置文件。这个文件应该放在你的web- application的WEB-INF/classes 目录下。因为有.xml的扩展名,它将被DOMConfigurator所读。每个web-application将用不同的默认配置文件。因为每个文件 都和它所在的web-application 相关的。
  • set TOMCAT_OPTS=-DLog4j.configuration=foobar.lcf
    -DLog4j.configuratorClass=com.foo.BarConfigurator

    告 诉Log4j用文件foobar.lcf作为默认的配置文件。这个文件应该放在你的web-application的WEB-INF/classes 目录下。因为定义了Log4j.configuratorClass 系统属性,文件将用自定义的com.foo.barconfigurator类来解析。每个web-application将用不同的默认配置文件。因为 每个文件都和它所在的web-application 相关的。
  • set TOMCAT_OPTS=-DLog4j.configuration=file:/c:/foobar.lcf set TOMCAT_OPTS=-DLog4j.configuration=file:/c:/foobar.lcf
    告诉Log4j用文件foobar.lcf作为默认的配置文件。这个配置文件用URL file:/c:/foobar.lcf定义了全路径名。这样同样的配置文件将被所有的web-application所用。

    不同的web-application将通过它们自己的类装载器来装载Log4j。这样,每个Log4j的环境将独立的运作,而没有任何的相互同步。例 如:在多个web-application中定义了完全相同的输出源的FileAppenders将尝试写同样的文件。结果好象是缺乏安全性的。你必须确 保每个不同的web-application的Log4j配置没有用到同样的系统资源。

2.  Servlet 的初始化

用一个特别的servlet来做Log4j的初始化也是可以的。如下是一个例子:

Java代码 复制代码
  1. public class Log4jInit extends HttpServlet {  
  2.      public void init() {  
  3.           String prefix = getServletContext().getRealPath("/");  
  4.           String file = getInitParameter("Log4j-init-file");  
  5.           if(file != null) {  
  6.              PropertyConfigurator.configure(prefix+file);  
  7.           }  
  8.       }  
  9.   
  10.        public void doGet(HttpServletRequest req, HttpServletResponse res) {  
  11.        }  
  12. }  
public class Log4jInit extends HttpServlet {      public void init() {           String prefix = getServletContext().getRealPath("/");           String file = getInitParameter("Log4j-init-file");           if(file != null) {              PropertyConfigurator.configure(prefix+file);           }       }         public void doGet(HttpServletRequest req, HttpServletResponse res) {        } }
 

在web.xml中定义随后的servlet为你的web-application。

Xml代码 复制代码
  1. <servlet>  
  2. <servlet-name>Log4j-init</servlet-name>  
  3. <servlet-class>xx.xx.Log4jInit</servlet-class>  
  4. <init-param>  
  5. <param-name>Log4j-init-file</param-name>  
  6. <param-value>WEB-INF/classes/Log4j.properties</param-value>  
  7. </init-param>  
  8. <load-on-startup>1</load-on-startup>  
  9. </servlet>  
 

写一个初始化的servlet是最有弹性的初始化Log4j的方法。代码中没有任何限制,你可以在servlet的init方法中定义它。

3.根据配置文件初始化log4j

log4j可以使用3中配置器来初始化:BasicConfigurator,DOMConfigurator,PropertyConfigurator
其语法为:

  • BasicConfigurator.configure (): 自动快速地使用缺省Log4j环境。
  • PropertyConfigurator.configure ( String configFilename) :读取使用Java的特性文件编写的配置文件。
  • DOMConfigurator.configure ( String filename ) :读取XML形式的配置文件。

使用PropertyConfigurator适用于所有的系统。如下的语句:  
PropertyConfigurator.configure("log4j.properties");


就以log4j.properties为配置文件初始化好了log4j环境。
注意一点:这个语句只需要在系统启动的时候执行一次。

例如,在ActionServlet的init()方法中调用一次。

Java代码 复制代码
  1. public class ActionServlet extends HttpServlet{  
  2. ...  
  3. /** 
  4. * Initialize global variables 
  5. */  
  6. public void init() throws ServletException {  
  7. // 初始化Action资源  
  8. try{  
  9. initLog4j();  
  10. ...  
  11. }catch(IOException e){  
  12. throw new ServletException("Load ActionRes is Error");  
  13. }  
  14. }  
  15. ...  
  16. protected void initLog4j(){  
  17. PropertyConfigurator.configure("log4j.properties");  
  18. }  
  19. ...  
  20. }//end class ActionServlet
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值