1 | package spiffy.log4j; |
2 | |
3 | import org.apache.log4j.Logger; |
4 | |
5 | /** |
6 | * Helper class for common Log4J interactions |
7 | * |
8 | * @author Kasper B. Graversen, (c) 2007-2008 |
9 | */ |
10 | public class LoggerHelper { |
11 | /** |
12 | * Returns a Log4J logger configured as the calling class. This ensures copy-paste safe code to get a logger instance, |
13 | * an ensures the logger is always fetched in a consistent manner. <br> |
14 | * <b>usage:</b><br> |
15 | * |
16 | * <pre> |
17 | * private final static Logger log = LoggerHelper.getLogger(); |
18 | * </pre> |
19 | * |
20 | * Since the logger is found by accessing the call stack it is important, that references are static. |
21 | * <p> |
22 | * The code is JDK1.4 compatible |
23 | * |
24 | * @return log4j logger instance for the calling class |
25 | * @author Kasper B. Graversen |
26 | */ |
27 | public static Logger getLogger() { |
28 | final Throwable t = new Throwable(); |
29 | t.fillInStackTrace(); |
30 | final String fullClassName = t.getStackTrace()[1].getClassName(); // get info on calling entity |
31 | return Logger.getLogger(fullClassName); |
32 | } |
33 | |
34 | } |