From c8f05bad092599e304219073b418b48c5ad3e5ed Mon Sep 17 00:00:00 2001 From: Klemek Date: Thu, 30 Aug 2018 12:42:28 +0100 Subject: [PATCH] v1.1 Logging into file --- README.md | 14 ++++--- download/simple-logger-1.1.jar | Bin 0 -> 5684 bytes pom.xml | 12 +++++- src/main/java/fr/klemek/logger/Logger.java | 35 +++++++++++++----- src/main/java/fr/klemek/logger/Utils.java | 8 ++-- .../java/fr/klemek/logger/LoggerTest.java | 25 +++++++++++++ src/test/resources/logging.properties | 4 +- 7 files changed, 77 insertions(+), 21 deletions(-) create mode 100644 download/simple-logger-1.1.jar create mode 100644 src/test/java/fr/klemek/logger/LoggerTest.java 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 0000000000000000000000000000000000000000..639681639d1403018a88b1c11822b2786d5f9f6b GIT binary patch literal 5684 zcmb7|2|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fiOZRs*MZug!wLS10xieB@mV-W;na*H1YjAxn0jUQg83dR9ic?YiaRM95fipy_~!d z9D9l?CF;>5#usK^{jSMcMeV$Ewroj_dYt>931m7|Ri4UAa2h9AA~Q6(_+2$L$j~!U zYIS7bV#AnhHH1rdDnwv~H|TWwnV#Bhk+BYMy?mYK9~C>2B&?r;YMS1%CwHl8d-P9T z_XQS1H}j2yWaO#?DL*&*VVk&SvuK0&{CAs*EVAzyu{LFG?{v{s>nSrv?~WUl&*8Mp zu;%smEgEDjU@xwSXFaggbXsT_Xy{+S(@%eDz^Z)4NrjVczXMUnZY}ub zA6V)r(x3bM-QfsK{TxU8VDQUhOmlLh-+`)QzcU0U{eID)<8A*Am5#vGvAZPR{a1qi zgR%bI`3PhkJKO)$`6Sl*cbOxwbu5$h-(>!dxsF7CKKb8)uK&p9hhl!0my_7*)v;0y)jzU*oUp&1^v|p7r1tMh^;nyh_J7hoE?hX2 V7~izwTUY!?6Mx5k#tQ)e{|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