diff --git a/README.md b/README.md
index a6ab634..259fd0d 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.0
+Current version v1.1
## Download
-* [simple-logger-1.0.jar](../../raw/master/download/simple-logger-1.0.jar)
+* [simple-logger-1.1.jar](../../raw/master/download/simple-logger-1.1.jar)
## How to use
@@ -19,18 +19,20 @@ Logger.setLevel(Level.WARNING);
Configuration file as follow (example) :
```
-#Parameters for good behavior
+# Parameters for good behavior
handlers=java.util.logging.ConsoleHandler
.level=WARNING
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
-#Customize date/time shown with this line
+# Customize date/time shown with this line
java.util.logging.SimpleFormatter.format=[%1$tF %1$tT][%4$s]%5$s %n
-#Specify your app name here
+# Specify your app name here
app_name=YourApp
# (Optional, specify your app package)
default_package=com.your.app
+# (Optional, specify an output file pattern)
+output_file=output.log
```
The default package works as follow : When logging stack trace of an error, it will stop when the package doesnt't match your app anymore. It helps having small and relevant stack trace in logs.
@@ -83,7 +85,7 @@ You can use this project as a maven dependency with this :
fr.klemek
simple-logger
- 1.0
+ 1.1
```
diff --git a/download/simple-logger-1.1.jar b/download/simple-logger-1.1.jar
new file mode 100644
index 0000000..6396816
Binary files /dev/null and b/download/simple-logger-1.1.jar differ
diff --git a/pom.xml b/pom.xml
index ce6cb2e..68c86ab 100644
--- a/pom.xml
+++ b/pom.xml
@@ -6,7 +6,7 @@
fr.klemek
simple-logger
- 1.0
+ 1.1
SimpleLogger
@@ -14,6 +14,16 @@
UTF-8
+
+
+
+ junit
+ junit
+ 4.12
+ test
+
+
+
target/${project.artifactId}/WEB-INF/classes
src/main/java
diff --git a/src/main/java/fr/klemek/logger/Logger.java b/src/main/java/fr/klemek/logger/Logger.java
index cde186d..b28c90f 100644
--- a/src/main/java/fr/klemek/logger/Logger.java
+++ b/src/main/java/fr/klemek/logger/Logger.java
@@ -1,10 +1,14 @@
package fr.klemek.logger;
+import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;
+import java.util.logging.FileHandler;
+import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogManager;
+import java.util.logging.SimpleFormatter;
/**
* Simple logger for this application.
@@ -13,12 +17,13 @@ import java.util.logging.LogManager;
*/
public final class Logger {
- private static java.util.logging.Logger appLogger = java.util.logging.Logger.getLogger(Utils.getString("app_name","Unkown"));
- private static final String APP_NAME = Utils.getString("app_name","Unkown");
- private static final String BASE_PACKAGE = Utils.getString("default_package",null);
+ 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 static boolean initialized = false;
+
private Logger() {
}
@@ -74,7 +79,19 @@ public final class Logger {
return;
}
LogManager.getLogManager().readConfiguration(is);
- appLogger = java.util.logging.Logger.getLogger(APP_NAME);
+
+ 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){
+ Handler handler = new FileHandler(outputFile, true);
+ handler.setFormatter(new SimpleFormatter());
+ appLogger.addHandler(new FileHandler(outputFile, true));
+ }
+
} catch (IOException e) {
Logger.log(e);
}
@@ -154,16 +171,16 @@ public final class Logger {
Logger.log(Level.WARNING, "Logger was not initialized please do so before using.");
initialized = true;
}
- message = String.format("[%s-%s] %s", APP_NAME, Utils.getCallingClassName(depth), message);
+ 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.BASE_PACKAGE != null){
- if (!inPackage && ste.getClassName().startsWith(Logger.BASE_PACKAGE))
+ if(Logger.basePackage != null){
+ if (!inPackage && ste.getClassName().startsWith(Logger.basePackage))
inPackage = true;
- else if (inPackage && !ste.getClassName().startsWith(Logger.BASE_PACKAGE))
+ else if (inPackage && !ste.getClassName().startsWith(Logger.basePackage))
break;
}
}
diff --git a/src/main/java/fr/klemek/logger/Utils.java b/src/main/java/fr/klemek/logger/Utils.java
index 2465722..4a824d0 100644
--- a/src/main/java/fr/klemek/logger/Utils.java
+++ b/src/main/java/fr/klemek/logger/Utils.java
@@ -13,8 +13,6 @@ import java.util.logging.Level;
*/
final class Utils {
- private static final ResourceBundle CONFIGURATION_BUNDLE = ResourceBundle.getBundle("logging");
-
private Utils() {
}
@@ -25,13 +23,15 @@ final class Utils {
/**
* Get a configuration string by its key.
*
+ * @param bundlePath the path to the configuration file
* @param key the key in the config file
* @param defaultValue the default value to use if not found
* @return the string or default value if not found
*/
- static String getString(String key, String defaultValue) {
+ static String getString(String bundlePath, String key, String defaultValue) {
+ int pos = bundlePath.indexOf(".properties");
try {
- return CONFIGURATION_BUNDLE.getString(key);
+ return ResourceBundle.getBundle(bundlePath.substring(0,pos)).getString(key);
} catch (MissingResourceException e) {
return defaultValue;
}
diff --git a/src/test/java/fr/klemek/logger/LoggerTest.java b/src/test/java/fr/klemek/logger/LoggerTest.java
new file mode 100644
index 0000000..19a5126
--- /dev/null
+++ b/src/test/java/fr/klemek/logger/LoggerTest.java
@@ -0,0 +1,25 @@
+package fr.klemek.logger;
+
+import java.io.File;
+
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class LoggerTest {
+
+ private static File output = new File("output.log");
+
+ @Test
+ public void test() {
+ Logger.log("message");
+ }
+
+ @Before
+ public void setUp() {
+ Logger.init("logging.properties");
+ if(output.exists())
+ output.delete();
+ }
+
+}
\ No newline at end of file
diff --git a/src/test/resources/logging.properties b/src/test/resources/logging.properties
index 8ea7c3f..638f9a0 100644
--- a/src/test/resources/logging.properties
+++ b/src/test/resources/logging.properties
@@ -9,4 +9,6 @@ java.util.logging.SimpleFormatter.format=[%1$tF %1$tT][%4$s]%5$s %n
#Specify your app name here
app_name=Test
#(Optional, specify your app package)
-default_package=fr.klemek.logger
\ No newline at end of file
+default_package=fr.klemek.logger
+#(Optional, specify an output file pattern)
+output_file=output.log
\ No newline at end of file