log4j 的配置
web.xml中配置:
<servlet>
<servlet-name>LoggingLoader</servlet-name>
<servlet-class>
com.neusoft.brm.util.LoggingLoader
</servlet-class>
<init-param>
<param-name>log</param-name>
<param-value>/config/LogConfig.properties</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
自定义java类(LoggingLoader.java):
package com.neusoft.brm.util;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.apache.log4j.PropertyConfigurator;
public class LoggingLoader extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* <pre>
* 初期化のメソッド
* <pre>
* @param config ServletConfig
* @throws ServletException
*/
public void init(ServletConfig config) throws ServletException {
super.init(config);
String prefix = getServletContext().getRealPath("/");
String file = getInitParameter("log");
System.out.println("======== 【BRM】 Logger Start ========");
if (file != null) {
PropertyConfigurator.configure(prefix + file);
}
}
}
自定义property文件(/config/LogConfig.properties):
#Loggers
log4j.rootLogger=INFO, brmConsole, brmFile
#Appenders
log4j.appender.brmConsole=org.apache.log4j.ConsoleAppender
log4j.appender.brmFile=org.apache.log4j.RollingFileAppender
log4j.appender.brmFile.File=/..//logs//log.txt
log4j.appender.brmFile.MaxFileSize=5120KB
log4j.appender.brmFile.MaxBackupIndex=10
#Layouts
log4j.appender.brmConsole.layout=org.apache.log4j.PatternLayout
log4j.appender.brmConsole.layout.ConversionPattern=%d %p [%c] - <%m>%n
log4j.appender.brmFile.layout=org.apache.log4j.PatternLayout
log4j.appender.brmFile.layout.ConversionPattern=%d %p [%c] - <%m>%n
在应用中使用即可。