diff options
Diffstat (limited to 'common/src/main/java')
-rw-r--r-- | common/src/main/java/at/gv/egovernment/moa/logging/Logger.java | 92 |
1 files changed, 68 insertions, 24 deletions
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..7cb2e7daf 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,49 @@ public class Logger { // there is no need for that anymore } + + /** The Constant instances. */ + private static final Map<String, org.slf4j.Logger> instances = new HashMap<String, org.slf4j.Logger>(); + + /** + * Gets the logger. + * + * @return the logger + */ + private static synchronized org.slf4j.Logger getLogger() { + StackTraceElement[] stacktrace = Thread.currentThread().getStackTrace(); + + int i; + for(i = 2; i < stacktrace.length; i++) + if(!stacktrace[i].getClassName().equals(Logger.class.getName())) + break; + + String className = stacktrace[i].getClassName(); + + org.slf4j.Logger logger = instances.get(className); + if (logger != null) { + return logger; + } + + logger = org.slf4j.LoggerFactory.getLogger(className); + instances.put(className, logger); + + return logger; + } + + /** + * Prepare the message for printing it as string. + * <p>Mainly introduce because the <code>message</code> might be <code>null</code>. + * + * @param message the message + * @return the string + */ + private static String prepareMessage(Object message) { + if(null == message) + return "no message given"; + return message.toString(); + } + /** * Test, if the trace log level is enabled. * @@ -52,7 +98,7 @@ public class Logger { * <code>false</code> otherwise. */ public static boolean isTraceEnabled() { - org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(Thread.currentThread().getStackTrace()[2].getClassName()); + org.slf4j.Logger logger = getLogger(); return logger.isTraceEnabled(); } @@ -64,7 +110,7 @@ public class Logger { * <code>false</code> otherwise. */ public static boolean isTraceEnabled(String hierarchy) { - org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(Thread.currentThread().getStackTrace()[2].getClassName()); + org.slf4j.Logger logger = getLogger(); return logger.isTraceEnabled(); } @@ -74,8 +120,8 @@ 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()); - logger.trace(message); + org.slf4j.Logger logger = getLogger(); + logger.trace(prepareMessage(message)); } /** @@ -85,7 +131,7 @@ public class Logger { * <code>false</code> otherwise. */ public static boolean isDebugEnabled() { - org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(Thread.currentThread().getStackTrace()[2].getClassName()); + org.slf4j.Logger logger = getLogger(); return logger.isDebugEnabled(); } @@ -97,7 +143,7 @@ public class Logger { * <code>false</code> otherwise. */ public static boolean isDebugEnabled(String hierarchy) { - org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(Thread.currentThread().getStackTrace()[2].getClassName()); + org.slf4j.Logger logger = getLogger(); return logger.isDebugEnabled(); } @@ -107,18 +153,18 @@ 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()); - logger.debug(message); + org.slf4j.Logger logger = getLogger(); + logger.debug(prepareMessage(message)); } - - /** + +/** * Log an info message. * * @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()); - logger.info(message); + org.slf4j.Logger logger = getLogger(); + logger.info(prepareMessage(message)); } /** @@ -127,8 +173,8 @@ 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()); - logger.warn(message); + org.slf4j.Logger logger = getLogger(); + logger.warn(prepareMessage(message)); } /** @@ -138,8 +184,8 @@ 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()); - logger.warn(message, t); + org.slf4j.Logger logger = getLogger(); + logger.warn(prepareMessage(message), t); } /** @@ -148,8 +194,8 @@ 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()); - logger.error(message); + org.slf4j.Logger logger = getLogger(); + logger.error(prepareMessage(message)); } /** @@ -159,8 +205,8 @@ 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()); - logger.error(message, t); + org.slf4j.Logger logger = getLogger(); + logger.error(prepareMessage(message), t); } /** @@ -169,8 +215,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()); - logger.fatal(message); + error(message); } /** @@ -180,7 +225,6 @@ 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()); - logger.fatal(message, t); + error(message, t); } } |