Software Development

How to Log to Separate Files in Log4j2



You have a batch application that reads, parses interface files (i.e., data file from other sources usually legacy systems), and inserts data into your batch application’s database. All logs are written to a single file. Now, the business starts a change request (CR) that allows for logs to be written to multiple files based on country codes and interface file names. This article demonstrates how to configure and use Log4j2 to perform as described in the hypothetical CR.

Please note that the samples herein are not Thread-safe.

Software Environment

  • Windows 7 Professional SP1
  • Eclipse – Kepler Release
  • Java 1.7 (1.7.0_67 – Windows x86)
  • Apache Log4j2
    • For this article, reference only the following jar files
      1. log4j-api-2.0.2.jar
      2. log4j-core-2.0.2.jar

Your log4j2.xml Configuration File


The Codes

Get the Files


You Might Also Like