第一种在 spring 的配置文件中进行加载:
<bean id="log4jInitializer" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass"
value="org.springframework.util.Log4jConfigurer" />
<property name="targetMethod" value="initLogging" />
<property name="arguments">
<list>
<value>classpath:log4j.properties</value>
</list>
</property>
</bean>
第二种代码编写:
public class Log4jInit implements ServletContextListener{
Logger log = Logger.getLogger(Log4jInit.class);
public void contextDestroyed(ServletContextEvent sce) {
log.info("Log4jInit contextDestroyed!");
}
public void contextInitialized(ServletContextEvent sce) {
//得到servletContext对象的方法
ServletContext sc = sce.getServletContext();
//指明文件的相对路径就能够得到文件的绝对路径
System.out.println(sc.getRealPath("/"));
String path = sc.getRealPath("/config/log4j.properties");
//启动服务器的时候加载日志的配置文件
init(path,sc);
log.info("log4j");
}
/**
*
* @param path 配置文件的路径
* @param sc ServletContext对象
*/
public void init(String path,ServletContext sc){
FileInputStream istream = null;
try{
Properties props = new Properties();
//加载配置文件
istream = new FileInputStream(path);
props.remove("log4j.appender.file.File");
System.out.println(sc.getRealPath("/log/hb.log"));
//指明log文件的位置
props.put("log4j.appender.file.File", sc.getRealPath("/log/hb.log"));
//加载文件流,加载Log4j文件的配置文件信息
props.load(istream);
PropertyConfigurator.configure(props);
} catch (Exception ex){
try {
throw new Exception(ex);
} catch (Exception e) {
e.printStackTrace();
}
} finally{
try {
istream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
第三种web项目工程可以在web.xml中进行配置加载:
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>
<listener>
<listener- class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
记得配置监听器