名词说明:Hadoop Job 提交类,本文成为Driver
正文:
有时候我们需要在Driver里面写些逻辑代码,需要用到Log4j,但是当使用Hadoop jar xxxx.jar提交mapreduce的时候,发现Driver中的log并没有安装自己预期打印出来。通过打印classpath中log4j.properties文件路径,发现driver运行时,classpath中有多个log4j.properties文件,如下:
/etc/hadoop-0.20/conf.empty/log4j.properties
file:/usr/lib/hadoop-0.20/hadoop-core-0.20.2-cdh3u4.jar!/log4j.properties
/tmp/hadoop/hadoop/hadoop-unjar1904689375813052409/log4j.properties
file:/Application/adam/publish/CLA_Hadoop_publish.jar!/log4j.properties
/Application/adam/publish/conf/log4j.properties
file:/usr/lib/hadoop-0.20/hadoop-core-0.20.2-cdh3u4.jar!/log4j.properties
/tmp/hadoop/hadoop/hadoop-unjar1904689375813052409/log4j.properties
file:/Application/adam/publish/CLA_Hadoop_publish.jar!/log4j.properties
/Application/adam/publish/conf/log4j.properties
而log4j只能加载第一个遇到的配置,所以我们mapreduce项目中的log4j.properties文件对于driver是不生效的。
很悲催,我们只能修改"/etc/hadoop-0.20/conf.empty/log4j.properties"也就是我们的hadoop程序conf下的log4j.properties文件来实现相关功能,所以大家使用的时候要小心点。我的一个配置样例如下:
log4j.logger.net.sf.json.JSONObject=ERROR
log4j.logger.org.apache.commons.httpclient.HttpMethodBase=ERROR
log4j.logger.org.apache.commons.httpclient.HttpMethodBase=ERROR
当然也可以通过程序代码SetLevel方法设置打印层级。
另外,附上寻找log4j.properties文件的方法:
Enumeration<URL> resources = this.getClass().getClassLoader().getResources("log4j.properties");
while (resources.hasMoreElements()) {
URL url = (URL) resources.nextElement();
System.out.println(url.getPath());
}
--heipark