如下:
1.配置Log4j.propertity文件在webroot下(我的目录是:/logconfig/log4j.propertity)
## LOGGER ##
## configure root Logger
log4j.rootLogger=INFO,console,flow,errorfile
## APPENDERS ##
# define an appender named console, which is set to be a ConsoleAppender
log4j.appender.console=org.apache.log4j.ConsoleAppender
# define an appender named file, which is set to be a DailyRollingFileAppender
log4j.appender.flow=org.apache.log4j.DailyRollingFileAppender
log4j.appender.flow.File=${catalina.base}/logs/flowlog/Flowlog.txt
log4j.appender.flow.DatePattern=yyyy-MM-dd'.txt'
log4j.appender.flow.Threshold=INFO
log4j.appender.flow.Append=true
log4j.appender.errorfile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.errorfile.File=${catalina.base}/logs/errorlog/errorlog.txt
log4j.appender.errorfile.DatePattern=yyyy-MM-dd'.txt'
log4j.appender.errorfile.Threshold=WARN
log4j.appender.file.Append=true
## LAYOUTS ##
# assign a PatternLayout to console appender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %l %n%-5p: %m%n
# assign a PatternLayout to file appender
log4j.appender.flow.layout=org.apache.log4j.PatternLayout
log4j.appender.flow.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %l %n%-5p: %m%n
log4j.appender.errorfile.layout=org.apache.log4j.PatternLayout
log4j.appender.errorfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %l %n%-5p: %m%n
2.初始化Log4j
package com.bjzc.utility;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class LogUtil {
/**
* 功能:初始化日志
* @author jiadong
*
*/
public static String ConfigPath ;
/**
* 初始化日志属性设置
*
*/
public static void init(){
String log4jPath = ConfigPath ;
PropertyConfigurator.configure(log4jPath);
}
/**
* 流程记录日志获取
*/
public static Logger getFlowLogger(){
return Logger.getLogger("flow");
}
/**
* 错误日志获取
*/
public static Logger getErrorLogger(){
return Logger.getLogger("error");
}
}
ackage com.bjzc.utility;
import org.apache.log4j.Logger;
/**
* 功能:后台输出日志
* @author jiadong
*版本:1.0
*时间:2007年11月12日
*/
public class InitLogger {
public static Logger writeLog ;
public static void init(){
InitLogger.writeLog = LogUtil.getFlowLogger() ;
}
}
3.在项目启动的时候初始化日志配置,写个servlet通过web.xml写入
package com.bjzc.utility;
import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServlet;
/**
* 初始化的Servlet
* @author jiadong
*版本:1.0
*时间:2007年11月09日
*/
@SuppressWarnings("serial")
public class InitServlet extends HttpServlet {
@SuppressWarnings("unused")
private ServletConfig config ;
public void init(ServletConfig config){
try {
this.config = config;
String configPath = config.getServletContext().getRealPath(
"/logconfig/log4j.properties");
LogUtil.ConfigPath = configPath;
LogUtil.init();
InitLogger.init();
InitLogger.writeLog.info("初始化成功!");
} catch (Exception e) {
InitLogger.writeLog.info("初始化错误!");
}
}
}
4.写入web.xml
<servlet>
<description>Servlet of init Log</description>
<display-name>by jiadong</display-name>
<servlet-name>InitServlet</servlet-name>
<servlet-class>com.bjzc.utility.InitServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>InitServlet</servlet-name>
<url-pattern>/com/bjzc/utility/InitServlet</url-pattern>
</servlet-mapping>
posted on 2007-11-12 12:21
jiadong 阅读(667)
评论(0) 编辑 收藏 所属分类:
OTHERS