本文转自:http://blog.csdn.net/flyjimi/archive/2006/01/09/574388.aspx
如有侵权,敬请告知
这个Log4jInitServlet主要方便之处是设置了系统变量webapp.root,在log4j.properties文件中就可以这样指定log文件路径。使用${webapp.root}表示web应用的根目录。全小写字母。
log4j.appender.FILE.File=${webapp.root}/WEB-INF/testlog4j.log
参考Spring对log4j的增强,增加webAppRootKey参数,防止多web应用,系统变量重复。
package com.suncode.log4j;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.PropertyConfigurator;
/**
* A servlet for initializing Log4j. See <a href=
* "http://jakarta.apache.org/log4j/docs/documentation.html">Log4j documentation</a>
* for how to use Log4j.
* <p>
* can use ${webapp.root} as your web app root in your log4j.property file as
* follows: log4j.appender.FILE.File=${webapp.root}/WEB-INF/testlog4j.log
*
* This servlet is never called by a client, but should be called during web
* application initialization, i.e. when the servlet engine starts. The
* following code should be inserted in the web.xml file for the web
* application:
* </p>
* <servlet> <servlet-name>Log4jInit</servlet-name>
* <servlet-class>com.suncode.log4j.Log4jInitServlet</servlet-class>
* <load-on-startup>1</load-on-startup>
* <init-param>
* <param-name>log4j-configuration</param-name>
* <param-value>/WEB-INF/log4j.properties</param-value>
* </init-param>
* <!--如果有多个web应用,怕webapp.root变量重复,可以在init-param里定义webAppRootKey-->
* <init-param>
* <param-name>webAppRootKey</param-name>
* <param-value>webapp.root</param-value>
* </init-param>
* </servlet>
*
* 2006-1-9 增加webAppRootKey参数,防止多web应用,系统变量重复.
*/
public class Log4jInitServlet extends HttpServlet {
private final String WEB_APP_ROOT_DEFAULT = "webapp.root";
public void init() throws ServletException {
String prefix = getServletContext().getRealPath("/");
// System.setProperty("webapp.root", prefix);
String webAppRootKey = getServletConfig().getInitParameter(
"webAppRootKey");
if(webAppRootKey == null || webAppRootKey.length() < 1){
webAppRootKey = WEB_APP_ROOT_DEFAULT;
}
System.setProperty(webAppRootKey, prefix);
String propfile = getServletConfig().getInitParameter(
"log4j-config-file");
if (propfile != null) {
PropertyConfigurator.configure(prefix + propfile);
System.out.println("Log4J Configured.");
}
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Method stub generated by Lomboz
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Method stub generated by Lomboz
}
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/flyjimi/archive/2006/01/09/574388.aspx
分享到:
相关推荐
这可以通过在部署环境中覆盖`log4j.properties`或`log4j.xml`文件来实现,或者使用环境变量或命令行参数动态设置。 总的来说,通过合理配置Log4j,可以轻松地实现跨平台的日志管理,使得在Windows和Linux系统中日志...
log4j.appender.logfile.File=${webapp.root}/WEB-INF/logs/myfuse.log log4j.appender.logfile.MaxFileSize=10MB log4j.appender.logfile.MaxBackupIndex=10 log4j.appender.logfile.layout=org.apache.log4j...
然后在log4j.properties中使用`${webApp.root}`变量: ```properties log4j.appender.logfile.File=${webApp.root}/WEB-INF/logs/app.log ``` 这种方法的好处是能够通过更改配置文件来改变日志文件的位置,提高...
<tree-loader path="${resin.root}/ext-webapp-lib"/> <tree-loader path="D:\ext_lib"/> </class-loader> </web-app-default> ``` 以上步骤完成了 Eclipse 中 Resin 服务器的基本配置。通过这种方式,可以在 ...
log4j.rootLogger=INFO, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender....
4. UPGRADE - 升级指南,如果你之前用过旧版本的Yii,可以参考这个来升级。 5. demos - 示例项目,用于学习和测试框架功能。 6. framework - 框架的核心库。 7. requirements - 配置需求检查工具,用于检测你的PHP...
建造 docker build --no-cache -t [image-name]:latest .... 可以通过设置LOG_CONF环境变量来完成,支持的值为: 价值 行为 全部 将访问和错误日志分别重定向到STDOUT和STDERR 使用权 将访问日志重