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
- Download log4j2 from http://logging.apache.org/log4j/2.x/
- Extract the file
- Copy the log4j-api-2.0.2.jar and log4j-core-2.0.2.jar to your Eclipse project
- 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)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?xml version="1.0" encoding="UTF-8"?> <configuration status="error"> <appenders> <console name="Console" target="SYSTEM_OUT"> <thresholdfilter level="ERROR" 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> <file append="false" filename="logs/test.log" name="log"> <patternlayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"> </patternlayout></file> </appenders> <loggers> <root level="trace"> <appenderref ref="log"> </appenderref></root> </loggers> </configuration> |
Sample Usage
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | /* * Copyright (C) 2014 www.turreta.com * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.turreta.apache.log4j2; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class LogProgramFlowSample { private Logger logger = LogManager.getLogger(LogProgramFlowSample.class.getName()); public void doItA() { logger.entry(); this.doItB(); logger.exit(); } private void doItB() { logger.entry(); this.doItC(); logger.exit(); } private void doItC() { logger.entry(); System.out.println("DONE"); logger.exit(); } public static void main(String[] args) { LogProgramFlowSample sample = new LogProgramFlowSample(); sample.doItA(); } } |
Sample Output
1 2 3 4 5 6 | 01:18:23.175 TRACE com.turreta.apache.log4j2.LogProgramFlowSample 27 doItA - entry 01:18:23.176 TRACE com.turreta.apache.log4j2.LogProgramFlowSample 33 doItB - entry 01:18:23.176 TRACE com.turreta.apache.log4j2.LogProgramFlowSample 39 doItC - entry 01:18:23.176 TRACE com.turreta.apache.log4j2.LogProgramFlowSample 41 doItC - exit 01:18:23.176 TRACE com.turreta.apache.log4j2.LogProgramFlowSample 35 doItB - exit 01:18:23.176 TRACE com.turreta.apache.log4j2.LogProgramFlowSample 29 doItA - exit |
Get the Codes
[wp_ad_camp_4]