v1.1 Logging into file

This commit is contained in:
Klemek
2018-08-30 12:42:28 +01:00
parent 33d6b44a20
commit c8f05bad09
7 changed files with 77 additions and 21 deletions
+26 -9
View File
@@ -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;
}
}
+4 -4
View File
@@ -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;
}
@@ -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();
}
}
+3 -1
View File
@@ -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
default_package=fr.klemek.logger
#(Optional, specify an output file pattern)
output_file=output.log