diff --git a/README.md b/README.md
index a826e0a..81e1f6b 100644
--- a/README.md
+++ b/README.md
@@ -1,11 +1,11 @@
# Simple Logger
A simple but useful Java logger to use everywhere.
-Current version v1.2.1
+Current version v1.3
## Download
-* [simple-logger-1.2.1.jar](../../raw/master/download/simple-logger-1.2.1.jar)
+* [simple-logger-1.3.jar](../../raw/master/download/simple-logger-1.3.jar)
## How to use
diff --git a/download/simple-logger-1.2.1.jar b/download/simple-logger-1.2.1.jar
deleted file mode 100644
index e1d58c2..0000000
Binary files a/download/simple-logger-1.2.1.jar and /dev/null differ
diff --git a/download/simple-logger-1.3.jar b/download/simple-logger-1.3.jar
new file mode 100644
index 0000000..f644993
Binary files /dev/null and b/download/simple-logger-1.3.jar differ
diff --git a/pom.xml b/pom.xml
index 50749ad..c08eb77 100644
--- a/pom.xml
+++ b/pom.xml
@@ -6,7 +6,7 @@
fr.klemek
simple-logger
- 1.2.1
+ 1.3
SimpleLogger
diff --git a/src/main/java/fr/klemek/logger/Logger.java b/src/main/java/fr/klemek/logger/Logger.java
index 53896f2..8497eb7 100644
--- a/src/main/java/fr/klemek/logger/Logger.java
+++ b/src/main/java/fr/klemek/logger/Logger.java
@@ -16,12 +16,11 @@ import java.util.logging.SimpleFormatter;
*/
public final class Logger {
+ private static final int DEFAULT_DEPTH = 4;
private static java.util.logging.Logger appLogger = java.util.logging.Logger.getLogger("Unknown");
private static String appName = "Unknown";
private static String basePackage = null;
private static boolean initialized = false;
- private static final int DEFAULT_DEPTH = 4;
-
private Logger() {
@@ -79,13 +78,13 @@ public final class Logger {
}
LogManager.getLogManager().readConfiguration(is);
- appName = Utils.getString(relativePath, "app_name","Unknown");
- basePackage = Utils.getString(relativePath, "default_package",null);
- String outputFile = Utils.getString(relativePath, "output_file",null);
+ appName = Utils.getString(relativePath, "app_name", "Unknown");
+ basePackage = Utils.getString(relativePath, "default_package", null);
+ String outputFile = Utils.getString(relativePath, "output_file", null);
appLogger = java.util.logging.Logger.getLogger(appName);
- if(outputFile != null){
+ if (outputFile != null) {
Handler handler = new FileHandler(outputFile, true);
handler.setFormatter(new SimpleFormatter());
appLogger.addHandler(handler);
@@ -101,7 +100,7 @@ public final class Logger {
*
* @param e the exception to log
*/
- public static void log(Exception e) {
+ public static void log(Throwable e) {
Logger.log(Logger.DEFAULT_DEPTH, Level.SEVERE, e.toString(), e);
}
@@ -111,7 +110,7 @@ public final class Logger {
* @param lvl the level of logging
* @param e the exception to log
*/
- public static void log(Level lvl, Exception e) {
+ public static void log(Level lvl, Throwable e) {
Logger.log(Logger.DEFAULT_DEPTH, lvl, e.toString(), e);
}
@@ -121,8 +120,8 @@ public final class Logger {
* @param e the exception to log
* @param msg the exception msg
*/
- public static void log(Exception e, String msg) {
- Logger.log(Logger.DEFAULT_DEPTH, Level.SEVERE, msg + " : {0}", e);
+ public static void log(Throwable e, String msg) {
+ Logger.log(Logger.DEFAULT_DEPTH, Level.SEVERE, msg + ": {0}", e);
}
/**
@@ -132,8 +131,8 @@ public final class Logger {
* @param e the exception to log
* @param msg the exception msg
*/
- public static void log(Level lvl, Exception e, String msg) {
- Logger.log(Logger.DEFAULT_DEPTH, lvl, msg + " : {0}", e);
+ public static void log(Level lvl, Throwable e, String msg) {
+ Logger.log(Logger.DEFAULT_DEPTH, lvl, msg + ": {0}", e);
}
/**
@@ -166,23 +165,29 @@ public final class Logger {
* @param objects the object for the message formatting
*/
private static void log(int depth, Level lvl, String message, Object... objects) {
- if(!initialized) {
+ if (!initialized) {
initialized = true;
Logger.log(Level.WARNING, "Logger was not initialized please do so before using.");
}
message = String.format("[%s-%s] %s", appName, Utils.getCallingClassName(depth), message);
appLogger.log(lvl, message, objects);
- if (lvl == Level.SEVERE && objects.length > 0 && objects[0] instanceof Exception) {
- boolean inPackage = false;
- for (StackTraceElement ste : ((Exception) objects[0]).getStackTrace()) {
- Logger.log(depth + 1, Level.SEVERE, "\t {0}", ste);
- if(Logger.basePackage != null){
- if (!inPackage && ste.getClassName().startsWith(Logger.basePackage))
- inPackage = true;
- else if (inPackage && !ste.getClassName().startsWith(Logger.basePackage))
- break;
+ if (objects.length > 0 && objects[0] instanceof Throwable) {
+ Throwable throwable = (Throwable) objects[0];
+ if (lvl == Level.SEVERE) {
+ boolean inPackage = false;
+ for (StackTraceElement ste : throwable.getStackTrace()) {
+ Logger.log(depth + 1, Level.SEVERE, "\t {0}", ste);
+ if (Logger.basePackage != null) {
+ if (!inPackage && ste.getClassName().startsWith(Logger.basePackage))
+ inPackage = true;
+ else if (inPackage && !ste.getClassName().startsWith(Logger.basePackage))
+ break;
+ }
}
}
+ if (throwable.getCause() != null)
+ Logger.log(depth + 1, lvl, "Caused by: {0}", throwable.getCause());
}
+
}
}
diff --git a/src/test/java/fr/klemek/logger/LoggerTest.java b/src/test/java/fr/klemek/logger/LoggerTest.java
index 612cf48..37942a4 100644
--- a/src/test/java/fr/klemek/logger/LoggerTest.java
+++ b/src/test/java/fr/klemek/logger/LoggerTest.java
@@ -75,7 +75,7 @@ public class LoggerTest {
public void testLogExceptionMessage() {
Logger.log(new Exception("custom exception"), "custom message");
verifyOutput(new String[]{
- "[SEVERE][Test-LoggerTest] custom message : java.lang.Exception: custom exception",
+ "[SEVERE][Test-LoggerTest] custom message: java.lang.Exception: custom exception",
"[SEVERE][Test-LoggerTest] \t fr.klemek.logger.LoggerTest.testLogExceptionMessage(LoggerTest.java:76)",
"[SEVERE][Test-LoggerTest] \t sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)"
});
@@ -93,7 +93,29 @@ public class LoggerTest {
public void testLogExceptionCustomLevelCustomMessage() {
Logger.log(Level.WARNING, new Exception("custom exception"), "custom message");
verifyOutput(new String[]{
- "[WARNING][Test-LoggerTest] custom message : java.lang.Exception: custom exception"
+ "[WARNING][Test-LoggerTest] custom message: java.lang.Exception: custom exception"
+ });
+ }
+
+ @Test
+ public void testLogExceptionCause() {
+ Logger.log(new Exception("custom exception", new Exception("custom cause")));
+ verifyOutput(new String[]{
+ "[SEVERE][Test-LoggerTest] java.lang.Exception: custom exception",
+ "[SEVERE][Test-LoggerTest] \t fr.klemek.logger.LoggerTest.testLogExceptionCause(LoggerTest.java:102)",
+ "[SEVERE][Test-LoggerTest] \t sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)",
+ "[SEVERE][Test-LoggerTest] Caused by: java.lang.Exception: custom cause",
+ "[SEVERE][Test-LoggerTest] \t fr.klemek.logger.LoggerTest.testLogExceptionCause(LoggerTest.java:102)",
+ "[SEVERE][Test-LoggerTest] \t sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)"
+ });
+ }
+
+ @Test
+ public void testLogExceptionCauseCustomLevel() {
+ Logger.log(Level.WARNING, new Exception("custom exception", new Exception("custom cause")));
+ verifyOutput(new String[]{
+ "[WARNING][Test-LoggerTest] java.lang.Exception: custom exception",
+ "[WARNING][Test-LoggerTest] Caused by: java.lang.Exception: custom cause",
});
}