Software Development

How to Trace Program Flow using Log4j2

Background

[wp_ad_camp_1]

Do you not hate it to log method with “entry” and “exit” texts? Should you use “start” or “entry”? This article demonstrates how to use Log4j2 API to perform flow tracing without the need to provide texts.

Software Environment

  • Windows 7 Professional SP1
  • Eclipse – Kepler Release
  • Java 1.7 (1.7.0_67 – Windows x86)
  • Apache Log4j 2.0.2

Download Log4j2 and Reference it

  1. Download log4j2 from http://logging.apache.org/log4j/2.x/
  2. Extract the file
  3. Copy the log4j-api-2.0.2.jar and log4j-core-2.0.2.jar to your Eclipse project
  4. Reference the two jar files

Define your log4j2.xml

[wp_ad_camp_5]

Define log4j2.xml and place it right under the src folder (visible also via Eclipse workspace)

Sample Usage

Sample Output

Get the Codes

[wp_ad_camp_4]

https://github.com/Turreta/File-I-O-in-Java/blob/master/src/com/turreta/apache/log4j2/LogProgramFlowSample.java

Loading

Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.


You Might Also Like