diff options
author | Florian Reimair <florian.reimair@iaik.tugraz.at> | 2015-08-11 15:42:13 +0200 |
---|---|---|
committer | Florian Reimair <florian.reimair@iaik.tugraz.at> | 2015-08-11 15:42:13 +0200 |
commit | ebe02bc990d362e39a37906385d7e470235e2627 (patch) | |
tree | b5513c0ccf37f201810ecea4a7af5c21dec60739 /common/src/main/java/at | |
parent | 2ec0757b66d9e23c4c29c9ca59c94acd6a9b46c4 (diff) | |
parent | 04381eb4e2d67ced539b34747403bb06cdf36c53 (diff) | |
download | moa-id-spss-ebe02bc990d362e39a37906385d7e470235e2627.tar.gz moa-id-spss-ebe02bc990d362e39a37906385d7e470235e2627.tar.bz2 moa-id-spss-ebe02bc990d362e39a37906385d7e470235e2627.zip |
Merge branch 'samlengine_update' into moa-2.1-Snapshot
Diffstat (limited to 'common/src/main/java/at')
-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); } } |