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') 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 From 399597f84d5a3112cc8ad7262a40cfcae88cf83c Mon Sep 17 00:00:00 2001 From: Florian Reimair Date: Thu, 12 Mar 2015 16:23:02 +0100 Subject: getting rid of log4j in logger class --- .../java/at/gv/egovernment/moa/logging/Logger.java | 50 +++++++++++----------- 1 file changed, 24 insertions(+), 26 deletions(-) (limited to 'common/src/main/java/at/gv/egovernment') 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 184be20cb..6999656ab 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 @@ -50,22 +50,22 @@ public class Logger { /** The Constant instances. */ - private static final Map instances = new HashMap(); + private static final Map instances = new HashMap(); /** * Gets the logger. * * @return the logger */ - private static synchronized org.apache.log4j.Logger getLogger() { + private static synchronized org.slf4j.Logger getLogger() { String className = Thread.currentThread().getStackTrace()[3].getClassName(); - org.apache.log4j.Logger logger = instances.get(className); + org.slf4j.Logger logger = instances.get(className); if (logger != null) { return logger; } - logger = org.apache.log4j.Logger.getLogger(className); + logger = org.slf4j.LoggerFactory.getLogger(className); instances.put(className, logger); return logger; @@ -78,7 +78,7 @@ public class Logger { * false otherwise. */ public static boolean isTraceEnabled() { - org.apache.log4j.Logger logger = getLogger(); + org.slf4j.Logger logger = getLogger(); return logger.isTraceEnabled(); } @@ -90,7 +90,7 @@ public class Logger { * false otherwise. */ public static boolean isTraceEnabled(String hierarchy) { - org.apache.log4j.Logger logger = getLogger(); + org.slf4j.Logger logger = getLogger(); return logger.isTraceEnabled(); } @@ -100,8 +100,8 @@ public class Logger { * @param message The message to trace. */ public static void trace(Object message) { - org.apache.log4j.Logger logger = getLogger(); - logger.trace(message); + org.slf4j.Logger logger = getLogger(); + logger.trace(message.toString()); } /** @@ -111,7 +111,7 @@ public class Logger { * false otherwise. */ public static boolean isDebugEnabled() { - org.apache.log4j.Logger logger = getLogger(); + org.slf4j.Logger logger = getLogger(); return logger.isDebugEnabled(); } @@ -123,7 +123,7 @@ public class Logger { * false otherwise. */ public static boolean isDebugEnabled(String hierarchy) { - org.apache.log4j.Logger logger = getLogger(); + org.slf4j.Logger logger = getLogger(); return logger.isDebugEnabled(); } @@ -133,8 +133,8 @@ public class Logger { * @param message The message to log. */ public static void debug(Object message) { - org.apache.log4j.Logger logger = getLogger(); - logger.debug(message); + org.slf4j.Logger logger = getLogger(); + logger.debug(message.toString()); } /** @@ -143,8 +143,8 @@ public class Logger { * @param message The message to log. */ public static void info(Object message) { - org.apache.log4j.Logger logger = getLogger(); - logger.info(message); + org.slf4j.Logger logger = getLogger(); + logger.info(message.toString()); } /** @@ -153,8 +153,8 @@ public class Logger { * @param message The message to log. */ public static void warn(Object message) { - org.apache.log4j.Logger logger = getLogger(); - logger.warn(message); + org.slf4j.Logger logger = getLogger(); + logger.warn(message.toString()); } /** @@ -164,8 +164,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 = getLogger(); - logger.warn(message, t); + org.slf4j.Logger logger = getLogger(); + logger.warn(message.toString(), t); } /** @@ -174,8 +174,8 @@ public class Logger { * @param message The message to log. */ public static void error(Object message) { - org.apache.log4j.Logger logger = getLogger(); - logger.error(message); + org.slf4j.Logger logger = getLogger(); + logger.error(message.toString()); } /** @@ -185,8 +185,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 = getLogger(); - logger.error(message, t); + org.slf4j.Logger logger = getLogger(); + logger.error(message.toString(), t); } /** @@ -195,8 +195,7 @@ public class Logger { * @param message The message to log. */ public static void fatal(Object message) { - org.apache.log4j.Logger logger = getLogger(); - logger.fatal(message); + error(message); } /** @@ -206,7 +205,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 = getLogger(); - logger.fatal(message, t); + error(message, t); } } -- cgit v1.2.3 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/main/java/at/gv/egovernment') 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/main/java/at/gv/egovernment') 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