Hadoop MapReduce log4j - log messages to a custom file in userlogs/job_ dir?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Hadoop MapReduce is a framework for processing large volumes of data across clusters of computers using simple programming models. It utilizes the YARN framework to manage distributed computing resources and uses MapReduce to process data in a series of two-step operations: mapping and reducing. As part of its operational infrastructure, Hadoop uses Apache log4j, a flexible logging framework for Java, to handle logging messages which are crucial for diagnosing and debugging software issues.
By default, Hadoop directs log4j-generated log messages to STDERR and STDOUT files located under the userlogs/job_<jobid>/ directory. Modification of these logging behaviors often becomes necessary to organize logs better, separate them by log levels, or distinguish them by other meaningful partitions. While MapReduce programs inherently offer tremendous computational power, understanding and managing the vast logging they produce is vital for effective application management.
Customizing Log4j Properties in MapReduce
To direct log messages to a custom file in the userlogs/job_<jobid>/ directory, you must configure the log4j properties specific to your MapReduce job. This can be achieved either by setting properties programmatically within your job's code or by using external log4j configuration files.
1. Log4j Configuration through Code
You can configure log4j programmatically by manipulating the Logger object in your MapReduce job code. Here is an illustrative example:
This code snippet will create and use a custom_log.log in the working directory. However, to ensure the log file is placed specifically under the userlogs/job_<jobid>/ directory, you might need additional configuration or command-line options when launching jobs.
2. External Log4j Configuration
A more maintainable and flexible approach is to use an external log4j configuration file. This allows changes without modifying the job's source code. You can specify the log4j configuration file using the -files option of the hadoop jar command.
Your log4j.properties should redirect the logging to the desired file:
Important Considerations
- Environmental Impact: Note that the file paths may require dynamic behavior to adapt to different YARN containers' paths.
- Performance: Excessive logging, especially to disk, can significantly impact the performance of your MapReduce jobs.
Summary Table
| Feature | Description | Consideration |
| Default Logging | Logs to STDERR and STDOUT | May need filtering and separation |
| Programmable Logging | Customize logging within the job's code | Increases code complexity |
| External Configuration | Use external configuration files | Flexible and maintainable |
Conclusion
Effective logging is critical for monitoring and troubleshooting in Hadoop MapReduce environments. Whether through programmatic configurations within the job itself or by external configuration files, managing logging location and level can greatly enhance the operational management of MapReduce jobs. Always consider the environmental impact and performance implications when customizing the logging infrastructure.
Related reading
- Hadoop (NameNode, DataNode and SecondaryNameNode) Not Starting
- Hadoop NoClassDefFoundError when adding external Jar
- Hadoop on cassandra database
- Hadoop Processing logic close to data, rather than data close to processing logic explanation
- Hadoop rack topology
- Hadoop Unable to load native-hadoop library for your platform warning
- Hadoop When does the setup method gets invoked in reducer?
- Hadoop/Hive Loading data from .csv on a local machine

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.