diff options
Diffstat (limited to 'common')
-rw-r--r-- | common/pom.xml | 32 | ||||
-rw-r--r-- | common/src/main/java/at/gv/egovernment/moa/logging/Logger.java | 92 | ||||
-rw-r--r-- | common/src/test/java/test/at/gv/egovernment/moa/util/DateTimeUtilsTest.java | 3 |
3 files changed, 76 insertions, 51 deletions
diff --git a/common/pom.xml b/common/pom.xml index 3d790d29b..8f18a1b92 100644 --- a/common/pom.xml +++ b/common/pom.xml @@ -67,47 +67,27 @@ <dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> - <version>2.4</version> + <version>${jodatime.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> - <version>1.7.7</version> </dependency> -<!-- <dependency> - <groupId>org.slf4j</groupId> - <artifactId>slf4j-simple</artifactId> - <version>1.7.6</version> - </dependency> --> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> - <version>1.7.7</version> </dependency> <dependency> <groupId>org.slf4j</groupId> - <artifactId>slf4j-api</artifactId> - <version>1.7.7</version> - </dependency> -<!-- <dependency> - <groupId>org.slf4j</groupId> - <artifactId>log4j-over-slf4j</artifactId> - <version>1.7.6</version> - </dependency> --> - <dependency> - <groupId>log4j</groupId> - <artifactId>log4j</artifactId> - <version>1.2.17</version> + <artifactId>jul-to-slf4j</artifactId> </dependency> <dependency> - <groupId>org.slf4j</groupId> - <artifactId>slf4j-log4j12</artifactId> - <version>1.7.7</version> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-log4j12</artifactId> </dependency> <dependency> - <groupId>org.slf4j</groupId> - <artifactId>jul-to-slf4j</artifactId> - <version>1.7.7</version> + <groupId>log4j</groupId> + <artifactId>log4j</artifactId> </dependency> </dependencies> 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); } } diff --git a/common/src/test/java/test/at/gv/egovernment/moa/util/DateTimeUtilsTest.java b/common/src/test/java/test/at/gv/egovernment/moa/util/DateTimeUtilsTest.java index 5b7a92004..e3468b89f 100644 --- a/common/src/test/java/test/at/gv/egovernment/moa/util/DateTimeUtilsTest.java +++ b/common/src/test/java/test/at/gv/egovernment/moa/util/DateTimeUtilsTest.java @@ -51,7 +51,8 @@ public class DateTimeUtilsTest extends TestCase { public void testParseDateTimeValid() throws Exception { Date date; - DateFormat format = SimpleDateFormat.getDateTimeInstance(); + DateFormat format = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss"); + String dateStr; format.setTimeZone(TimeZone.getTimeZone("GMT")); |