v1.3 : Throwable instead of Exception ang log cause
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>fr.klemek</groupId>
|
||||
<artifactId>simple-logger</artifactId>
|
||||
<version>1.2.1</version>
|
||||
<version>1.3</version>
|
||||
|
||||
<name>SimpleLogger</name>
|
||||
|
||||
|
||||
@@ -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() {
|
||||
@@ -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,7 +120,7 @@ public final class Logger {
|
||||
* @param e the exception to log
|
||||
* @param msg the exception msg
|
||||
*/
|
||||
public static void log(Exception e, String msg) {
|
||||
public static void log(Throwable e, String msg) {
|
||||
Logger.log(Logger.DEFAULT_DEPTH, Level.SEVERE, msg + ": {0}", e);
|
||||
}
|
||||
|
||||
@@ -132,7 +131,7 @@ 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) {
|
||||
public static void log(Level lvl, Throwable e, String msg) {
|
||||
Logger.log(Logger.DEFAULT_DEPTH, lvl, msg + ": {0}", e);
|
||||
}
|
||||
|
||||
@@ -172,9 +171,11 @@ public final class Logger {
|
||||
}
|
||||
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) {
|
||||
if (objects.length > 0 && objects[0] instanceof Throwable) {
|
||||
Throwable throwable = (Throwable) objects[0];
|
||||
if (lvl == Level.SEVERE) {
|
||||
boolean inPackage = false;
|
||||
for (StackTraceElement ste : ((Exception) objects[0]).getStackTrace()) {
|
||||
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))
|
||||
@@ -184,5 +185,9 @@ public final class Logger {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (throwable.getCause() != null)
|
||||
Logger.log(depth + 1, lvl, "Caused by: {0}", throwable.getCause());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,6 +97,28 @@ public class LoggerTest {
|
||||
});
|
||||
}
|
||||
|
||||
@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",
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetLevel() {
|
||||
assertEquals(Level.INFO, Logger.getLevel());
|
||||
|
||||
Reference in New Issue
Block a user