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
+8 -6
View File
@@ -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 :
<dependency>
<groupId>fr.klemek</groupId>
<artifactId>simple-logger</artifactId>
<version>1.0</version>
<version>1.1</version>
</dependency>
</dependencies>
```
Binary file not shown.
+11 -1
View File
@@ -6,7 +6,7 @@
<groupId>fr.klemek</groupId>
<artifactId>simple-logger</artifactId>
<version>1.0</version>
<version>1.1</version>
<name>SimpleLogger</name>
@@ -14,6 +14,16 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- Test Dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<outputDirectory>target/${project.artifactId}/WEB-INF/classes</outputDirectory>
<sourceDirectory>src/main/java</sourceDirectory>
+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