override log4j.properties in hadoop

Deadly 提交于 2019-12-03 10:33:59
Daniel

If you use the default Log4j.properties file the logging settings get overridden by environment variables from the startup script. If you want to use the default log4j and just simply want to change the logging level, use $HADOOP_CONF_DIR/hadoop-env.sh

For example, to change your logger to DEBUG log level and DRFA logger, use

export HADOOP_ROOT_LOGGER="DEBUG,DRFA"
  1. You could remove the log4j.properties from your hadoop jar
  2. OR make sure that your jar/log4j.properties is first in the classpath (log4j picks the first log4j.properties from the classpath that it finds)
  3. OR specify the system variable: -Dlog4j.configuration=PATH_TO_FILE

See the documentation to learn how log4j finds the configuration.

Modify the log4j file inside HADOOP_CONF_DIR. Note that hadoop job wont consider the log4j file of your application. It will consider the one inside HADOOP_CONF_DIR.

If you want to force hadoop to use some other log4j file, try one of these:

  1. You can try what @Patrice said. ie.

    -Dlog4j.configuration=file:/path/to/user_specific/log4j.xml

  2. Customize HADOOP_CONF_DIR/log4j.xml and set the logger level for "your" classes as per your wish. Other user(s) wont be affected due to this unless both are having classes with same package structure. This wont work for core hadoop classes as all users will get afftected.

  3. Create your customized log4j file. Replicate the directory HADOOP_CONF_DIR and put your log4j file inside it. export HADOOP_CONF_DIR to your conf directory. Other users will point to the default one.

Maven Packaging:

Once I realized I needed to add my custom debug-log.properties file to src/main/java/resources, Maven added it to the application.jar root directory and then it was just a matter of referring to it or not in -Dlog4j.configuration=debug-log.properties from the command line.

Oozie <java> Action:

In regard to Oozie, use <java-opts>-Dlog4j.configuration=${log4jConfig}</java-opts> in the workflow.xml actions and define the following in a job.properties file.

#one of the following log4j.config parameters must be defined 
#log4jConfig=log4j.properties
log4jConfig=debug-log.properties

Oozie <map-reduce> Action:

 <property>
      <name>mapred.child.java.opts</name>
      <value>-Dlog4j.configuration=${log4jConfig}</value>
 </property> 
Patrice

I was faced with the same problem (CDH3U3, Hadoop 0.20.2). I finally found a solution with (note file: prefix in the path):

-Dlog4j.configuration=file:/path/to/user_specific/log4j.xml

As mentioned by Sulpha, for hadoop 1.2.1, it is important to override the task-log4j.properties that is present inside hadoop-core.jar

For my pseudo distributed mode,

I was unable to print the debug messages of my pig UDFs and had to delete the task-log4j.properties from hadoop-core.jar and replace it with a copy of the $HADOOP_INSTALL/conf/log4j.properties.

Used the

zip -d hadoop-core-1.2.1.jar task-log4j.properties #to delete

and

zip -g hadoop-core-1.2.1.jar task-log4j.properties #to add back 

If there is already configured log4j properties file inside the jar file. you can override by simple putting -Dlog4j.configuration= before the -classpath

here is the sample:

java -Dlog4j.configuration=..\conf\log4j.properties -classpath %CLASSPATH%

Put log4j.configuration option in the child java options.

I.e.

hadoop jar ... -Dmapred.child.java.opts=-Dlog4j.configuration=file:/...../log4j_debug.properties

You must put log4j_debug.properties file on all slave servers in a same directory path like /home/yourname/log4j_debug.properties or /tmp/log4j_debug.properties

This setting overwrites mapred.child.java.opts settings. If you want to use with another options like -Xmx32m, which means 32MB heap size, then do like following:

hadoop jar ... -Dmapred.child.java.opts='-Xmx32m -Dlog4j.configuration=file:/...../log4j_debug.properties'

In the Hadoop 1.2.1 there are 2 config files: log4j.properties and task-log4j.properties So to make example above work the change have to be done in task-log4j.properties not in log4j.properties

you can add follwing line in your task-log4j.properties:

log4j.logger.org.xxx=WARN
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!