From 414152df1310067623f10f90cfb89293926dd681 Mon Sep 17 00:00:00 2001 From: Florian Reimair Date: Tue, 10 Mar 2015 14:35:12 +0100 Subject: save some ram with logger --- .../java/at/gv/egovernment/moa/logging/Logger.java | 52 ++++++++++++++++------ 1 file changed, 39 insertions(+), 13 deletions(-) (limited to 'common/src/main/java/at/gv/egovernment/moa/logging/Logger.java') diff --git a/common/src/main/java/at/gv/egovernment/moa/logging/Logger.java b/common/src/main/java/at/gv/egovernment/moa/logging/Logger.java index 2e76325a5..184be20cb 100644 --- a/common/src/main/java/at/gv/egovernment/moa/logging/Logger.java +++ b/common/src/main/java/at/gv/egovernment/moa/logging/Logger.java @@ -24,6 +24,9 @@ package at.gv.egovernment.moa.logging; +import java.util.HashMap; +import java.util.Map; + /** * A utility class acting as a facade to the logging subsystem. * @@ -45,6 +48,29 @@ public class Logger { // there is no need for that anymore } + + /** The Constant instances. */ + private static final Map instances = new HashMap(); + + /** + * Gets the logger. + * + * @return the logger + */ + private static synchronized org.apache.log4j.Logger getLogger() { + String className = Thread.currentThread().getStackTrace()[3].getClassName(); + + org.apache.log4j.Logger logger = instances.get(className); + if (logger != null) { + return logger; + } + + logger = org.apache.log4j.Logger.getLogger(className); + instances.put(className, logger); + + return logger; + } + /** * Test, if the trace log level is enabled. * @@ -52,7 +78,7 @@ public class Logger { * false otherwise. */ public static boolean isTraceEnabled() { - org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(Thread.currentThread().getStackTrace()[2].getClassName()); + org.apache.log4j.Logger logger = getLogger(); return logger.isTraceEnabled(); } @@ -64,7 +90,7 @@ public class Logger { * false otherwise. */ public static boolean isTraceEnabled(String hierarchy) { - org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(Thread.currentThread().getStackTrace()[2].getClassName()); + org.apache.log4j.Logger logger = getLogger(); return logger.isTraceEnabled(); } @@ -74,7 +100,7 @@ public class Logger { * @param message The message to trace. */ public static void trace(Object message) { - org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(Thread.currentThread().getStackTrace()[2].getClassName()); + org.apache.log4j.Logger logger = getLogger(); logger.trace(message); } @@ -85,7 +111,7 @@ public class Logger { * false otherwise. */ public static boolean isDebugEnabled() { - org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(Thread.currentThread().getStackTrace()[2].getClassName()); + org.apache.log4j.Logger logger = getLogger(); return logger.isDebugEnabled(); } @@ -97,7 +123,7 @@ public class Logger { * false otherwise. */ public static boolean isDebugEnabled(String hierarchy) { - org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(Thread.currentThread().getStackTrace()[2].getClassName()); + org.apache.log4j.Logger logger = getLogger(); return logger.isDebugEnabled(); } @@ -107,7 +133,7 @@ public class Logger { * @param message The message to log. */ public static void debug(Object message) { - org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(Thread.currentThread().getStackTrace()[2].getClassName()); + org.apache.log4j.Logger logger = getLogger(); logger.debug(message); } @@ -117,7 +143,7 @@ public class Logger { * @param message The message to log. */ public static void info(Object message) { - org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(Thread.currentThread().getStackTrace()[2].getClassName()); + org.apache.log4j.Logger logger = getLogger(); logger.info(message); } @@ -127,7 +153,7 @@ public class Logger { * @param message The message to log. */ public static void warn(Object message) { - org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(Thread.currentThread().getStackTrace()[2].getClassName()); + org.apache.log4j.Logger logger = getLogger(); logger.warn(message); } @@ -138,7 +164,7 @@ public class Logger { * @param t An exception that may be the cause of the warning. */ public static void warn(Object message, Throwable t) { - org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(Thread.currentThread().getStackTrace()[2].getClassName()); + org.apache.log4j.Logger logger = getLogger(); logger.warn(message, t); } @@ -148,7 +174,7 @@ public class Logger { * @param message The message to log. */ public static void error(Object message) { - org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(Thread.currentThread().getStackTrace()[2].getClassName()); + org.apache.log4j.Logger logger = getLogger(); logger.error(message); } @@ -159,7 +185,7 @@ public class Logger { * @param t An exception that may be the cause of the error. */ public static void error(Object message, Throwable t) { - org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(Thread.currentThread().getStackTrace()[2].getClassName()); + org.apache.log4j.Logger logger = getLogger(); logger.error(message, t); } @@ -169,7 +195,7 @@ public class Logger { * @param message The message to log. */ public static void fatal(Object message) { - org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(Thread.currentThread().getStackTrace()[2].getClassName()); + org.apache.log4j.Logger logger = getLogger(); logger.fatal(message); } @@ -180,7 +206,7 @@ public class Logger { * @param t An exception that may be the cause of the error. */ public static void fatal(Object message, Throwable t) { - org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(Thread.currentThread().getStackTrace()[2].getClassName()); + org.apache.log4j.Logger logger = getLogger(); logger.fatal(message, t); } } -- cgit v1.2.3