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() {
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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",
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user