Apache Commons Logging和log4j
Apache Commons Logging不是logger,而是立足于制定一个统一接口来管理各类不同的logger的logFactory,有些项目里加入log4j的同时加入了Apache Commons Logging可能是为了将来可能变更日志系统而考虑的。所以加入Apache Commons Logging后,显著地有两个变化:
1.多一个配置文件commons-logging.properties用来决定使用什么logging toolkit
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
2.取得log的方式改成使用logFactory来取
public class CLASS
{
private Log log = LogFactory.getLog(CLASS.class);
...
}
所以如果后期觉得log4j不适用了,要换成别的log工具,只要在commons-logging.properties里改下,然后把新的log工具jar包和配置引入就可以自然过渡过去了
然后Apache Commons Logging可以帮助简化log4j的使用,如果单纯使用log4j要有一个麻烦的初始化如下:
public class Log4jInit extends HttpServlet {
public
void init() {
String prefix =getServletContext().getRealPath("/");
String file = getInitParameter("log4j-init-file");
// if the log4j-init-file is not set, then no point in trying
if(file != null) {
PropertyConfigurator.configure(prefix+file);
}
}
public
void doGet(HttpServletRequest req, HttpServletResponse res) {
}
}
而Apache Commons Logging会自动去读取对应的配置文件
然后spring也有一个对log4j的额外功能,每隔预定时间,自动更新读取log4j的配置文件,在web.xml里配置
<context-param>
<param-name>log4fRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
这样就能动态更改log输出级别而不重启服务器了
页:
[1]