v1.3 : Throwable instead of Exception ang log cause

This commit is contained in:
Klemek
2018-09-02 14:24:27 +01:00
parent 55aab91887
commit b3f14b545e
6 changed files with 54 additions and 27 deletions
+2 -2
View File
@@ -1,11 +1,11 @@
# Simple Logger # Simple Logger
A simple but useful Java logger to use everywhere. A simple but useful Java logger to use everywhere.
Current version v1.2.1 Current version v1.3
## Download ## 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 ## How to use
Binary file not shown.
Binary file not shown.
+1 -1
View File
@@ -6,7 +6,7 @@
<groupId>fr.klemek</groupId> <groupId>fr.klemek</groupId>
<artifactId>simple-logger</artifactId> <artifactId>simple-logger</artifactId>
<version>1.2.1</version> <version>1.3</version>
<name>SimpleLogger</name> <name>SimpleLogger</name>
+27 -22
View File
@@ -16,12 +16,11 @@ import java.util.logging.SimpleFormatter;
*/ */
public final class Logger { 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 java.util.logging.Logger appLogger = java.util.logging.Logger.getLogger("Unknown");
private static String appName = "Unknown"; private static String appName = "Unknown";
private static String basePackage = null; private static String basePackage = null;
private static boolean initialized = false; private static boolean initialized = false;
private static final int DEFAULT_DEPTH = 4;
private Logger() { private Logger() {
@@ -79,13 +78,13 @@ public final class Logger {
} }
LogManager.getLogManager().readConfiguration(is); LogManager.getLogManager().readConfiguration(is);
appName = Utils.getString(relativePath, "app_name","Unknown"); appName = Utils.getString(relativePath, "app_name", "Unknown");
basePackage = Utils.getString(relativePath, "default_package",null); basePackage = Utils.getString(relativePath, "default_package", null);
String outputFile = Utils.getString(relativePath, "output_file",null); String outputFile = Utils.getString(relativePath, "output_file", null);
appLogger = java.util.logging.Logger.getLogger(appName); appLogger = java.util.logging.Logger.getLogger(appName);
if(outputFile != null){ if (outputFile != null) {
Handler handler = new FileHandler(outputFile, true); Handler handler = new FileHandler(outputFile, true);
handler.setFormatter(new SimpleFormatter()); handler.setFormatter(new SimpleFormatter());
appLogger.addHandler(handler); appLogger.addHandler(handler);
@@ -101,7 +100,7 @@ public final class Logger {
* *
* @param e the exception to log * @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); 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 lvl the level of logging
* @param e the exception to log * @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); Logger.log(Logger.DEFAULT_DEPTH, lvl, e.toString(), e);
} }
@@ -121,8 +120,8 @@ public final class Logger {
* @param e the exception to log * @param e the exception to log
* @param msg the exception msg * @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); 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 e the exception to log
* @param msg the exception msg * @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); 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 * @param objects the object for the message formatting
*/ */
private static void log(int depth, Level lvl, String message, Object... objects) { private static void log(int depth, Level lvl, String message, Object... objects) {
if(!initialized) { if (!initialized) {
initialized = true; initialized = true;
Logger.log(Level.WARNING, "Logger was not initialized please do so before using."); Logger.log(Level.WARNING, "Logger was not initialized please do so before using.");
} }
message = String.format("[%s-%s] %s", appName, Utils.getCallingClassName(depth), message); message = String.format("[%s-%s] %s", appName, Utils.getCallingClassName(depth), message);
appLogger.log(lvl, message, objects); appLogger.log(lvl, message, objects);
if (lvl == Level.SEVERE && objects.length > 0 && objects[0] instanceof Exception) { if (objects.length > 0 && objects[0] instanceof Throwable) {
boolean inPackage = false; Throwable throwable = (Throwable) objects[0];
for (StackTraceElement ste : ((Exception) objects[0]).getStackTrace()) { if (lvl == Level.SEVERE) {
Logger.log(depth + 1, Level.SEVERE, "\t {0}", ste); boolean inPackage = false;
if(Logger.basePackage != null){ for (StackTraceElement ste : throwable.getStackTrace()) {
if (!inPackage && ste.getClassName().startsWith(Logger.basePackage)) Logger.log(depth + 1, Level.SEVERE, "\t {0}", ste);
inPackage = true; if (Logger.basePackage != null) {
else if (inPackage && !ste.getClassName().startsWith(Logger.basePackage)) if (!inPackage && ste.getClassName().startsWith(Logger.basePackage))
break; 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());
} }
} }
} }
+24 -2
View File
@@ -75,7 +75,7 @@ public class LoggerTest {
public void testLogExceptionMessage() { public void testLogExceptionMessage() {
Logger.log(new Exception("custom exception"), "custom message"); Logger.log(new Exception("custom exception"), "custom message");
verifyOutput(new String[]{ 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 fr.klemek.logger.LoggerTest.testLogExceptionMessage(LoggerTest.java:76)",
"[SEVERE][Test-LoggerTest] \t sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)" "[SEVERE][Test-LoggerTest] \t sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)"
}); });
@@ -93,7 +93,29 @@ public class LoggerTest {
public void testLogExceptionCustomLevelCustomMessage() { public void testLogExceptionCustomLevelCustomMessage() {
Logger.log(Level.WARNING, new Exception("custom exception"), "custom message"); Logger.log(Level.WARNING, new Exception("custom exception"), "custom message");
verifyOutput(new String[]{ 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",
}); });
} }