You have a batch application that reads, parses interface files (i.e., data files 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
- log4j-api-2.0.2.jar
- log4j-core-2.0.2.jar
- For this article, reference only the following jar files
Your log4j2.xml Configuration File
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <?xml version="1.0" encoding="UTF-8"?> <configuration status="error"> <appenders> <console name="Console" target="SYSTEM_OUT"> <thresholdfilter level="DEBUG" onmatch="ACCEPT" onmismatch="DENY"> <!-- Flow tracing is most useful with a pattern that shows location. Below pattern outputs class, line number and method name. --> <patternlayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"> </patternlayout></thresholdfilter></console> <!-- START: For com.turreta.apache.log4j2.thread.FileProcessorTest --> <routing name="RoutingAppender"> <routes pattern="${ctx:countryCode}-${ctx:interfaceFile}"> <route> <rollingfile filename="logs/${ctx:countryCode}/${ctx:interfaceFile}" filepattern="logs/${ctx:countryCode}/${ctx:interfaceFile}.%i.log.gz" name="Rolling-${ctx:countryCode}-${ctx:interfaceFile}"> <patternlayout pattern="%d{ABSOLUTE} %level{length=5} [%thread] %logger{1} - %msg%n"> <sizebasedtriggeringpolicy size="1024"> </sizebasedtriggeringpolicy></patternlayout></rollingfile> </route> </routes> </routing> <!-- END: For com.turreta.apache.log4j2.thread.FileProcessorTest --> </appenders> <loggers> <logger additivity="false" level="debug" name="com.turreta.apache.log4j2.thread"> <appenderref ref="RoutingAppender"> </appenderref></logger> </loggers> </configuration> |
The Codes
1 2 3 4 5 6 7 8 9 10 | public void process() { ThreadContext.put("countryCode", this.countryCode); ThreadContext.put("interfaceFile", this.interfaceFile); log.debug("Parsing file " + this.interfaceFile + " from " + this.countryCode); log.debug("Uploading data from " + this.interfaceFile + " from " + this.countryCode); log.debug("Generating invoices from " + this.interfaceFile + " from " + this.countryCode); log.debug("Processing completed **** " + this.interfaceFile + " from " + this.countryCode); ThreadContext.remove("countryCode"); ThreadContext.remove("interfaceFile"); } |
Get the Files
- https://github.com/Turreta/File-I-O-in-Java/blob/master/src/log4j2.xml
- https://github.com/Turreta/File-I-O-in-Java/blob/master/src/com/turreta/apache/log4j2/thread/FileProcessorTest.java