From 1bb5a7e0db70fb95343436155571f2037372c352 Mon Sep 17 00:00:00 2001 From: Florian Reimair Date: Mon, 23 Mar 2015 13:41:33 +0100 Subject: made logging system more robust --- common/src/main/java/at/gv/egovernment/moa/logging/Logger.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'common/src') 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 6999656ab..265ad026d 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 @@ -58,7 +58,14 @@ public class Logger { * @return the logger */ private static synchronized org.slf4j.Logger getLogger() { - String className = Thread.currentThread().getStackTrace()[3].getClassName(); + 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) { -- cgit v1.2.3 From 2db59d0b99e6ca3bc9139b72fedfd9356be7e898 Mon Sep 17 00:00:00 2001 From: Florian Reimair Date: Tue, 5 May 2015 12:55:56 +0200 Subject: fixed NullPointerException in Logger class triggered by empty message --- .../java/at/gv/egovernment/moa/logging/Logger.java | 31 +++++++++++++++------- 1 file changed, 22 insertions(+), 9 deletions(-) (limited to 'common/src') 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 265ad026d..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 @@ -78,6 +78,19 @@ public class Logger { return logger; } + /** + * Prepare the message for printing it as string. + *

Mainly introduce because the message might be null. + * + * @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. * @@ -108,7 +121,7 @@ public class Logger { */ public static void trace(Object message) { org.slf4j.Logger logger = getLogger(); - logger.trace(message.toString()); + logger.trace(prepareMessage(message)); } /** @@ -141,17 +154,17 @@ public class Logger { */ public static void debug(Object message) { org.slf4j.Logger logger = getLogger(); - logger.debug(message.toString()); + logger.debug(prepareMessage(message)); } - - /** + +/** * Log an info message. * * @param message The message to log. */ public static void info(Object message) { org.slf4j.Logger logger = getLogger(); - logger.info(message.toString()); + logger.info(prepareMessage(message)); } /** @@ -161,7 +174,7 @@ public class Logger { */ public static void warn(Object message) { org.slf4j.Logger logger = getLogger(); - logger.warn(message.toString()); + logger.warn(prepareMessage(message)); } /** @@ -172,7 +185,7 @@ public class Logger { */ public static void warn(Object message, Throwable t) { org.slf4j.Logger logger = getLogger(); - logger.warn(message.toString(), t); + logger.warn(prepareMessage(message), t); } /** @@ -182,7 +195,7 @@ public class Logger { */ public static void error(Object message) { org.slf4j.Logger logger = getLogger(); - logger.error(message.toString()); + logger.error(prepareMessage(message)); } /** @@ -193,7 +206,7 @@ public class Logger { */ public static void error(Object message, Throwable t) { org.slf4j.Logger logger = getLogger(); - logger.error(message.toString(), t); + logger.error(prepareMessage(message), t); } /** -- cgit v1.2.3