v1.1 Logging into file
This commit is contained in:
@@ -1,11 +1,11 @@
|
|||||||
# Simple Logger
|
# Simple Logger
|
||||||
A simple but useful Java logger to use everywhere.
|
A simple but useful Java logger to use everywhere.
|
||||||
|
|
||||||
Current version v1.0
|
Current version v1.1
|
||||||
|
|
||||||
## Download
|
## 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
|
## How to use
|
||||||
|
|
||||||
@@ -19,18 +19,20 @@ Logger.setLevel(Level.WARNING);
|
|||||||
|
|
||||||
Configuration file as follow (example) :
|
Configuration file as follow (example) :
|
||||||
```
|
```
|
||||||
#Parameters for good behavior
|
# Parameters for good behavior
|
||||||
handlers=java.util.logging.ConsoleHandler
|
handlers=java.util.logging.ConsoleHandler
|
||||||
.level=WARNING
|
.level=WARNING
|
||||||
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
|
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
|
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
|
app_name=YourApp
|
||||||
# (Optional, specify your app package)
|
# (Optional, specify your app package)
|
||||||
default_package=com.your.app
|
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.
|
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>
|
<dependency>
|
||||||
<groupId>fr.klemek</groupId>
|
<groupId>fr.klemek</groupId>
|
||||||
<artifactId>simple-logger</artifactId>
|
<artifactId>simple-logger</artifactId>
|
||||||
<version>1.0</version>
|
<version>1.1</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
```
|
```
|
||||||
|
|||||||
Binary file not shown.
@@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
<groupId>fr.klemek</groupId>
|
<groupId>fr.klemek</groupId>
|
||||||
<artifactId>simple-logger</artifactId>
|
<artifactId>simple-logger</artifactId>
|
||||||
<version>1.0</version>
|
<version>1.1</version>
|
||||||
|
|
||||||
<name>SimpleLogger</name>
|
<name>SimpleLogger</name>
|
||||||
|
|
||||||
@@ -14,6 +14,16 @@
|
|||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<!-- Test Dependencies -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.12</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
<outputDirectory>target/${project.artifactId}/WEB-INF/classes</outputDirectory>
|
<outputDirectory>target/${project.artifactId}/WEB-INF/classes</outputDirectory>
|
||||||
<sourceDirectory>src/main/java</sourceDirectory>
|
<sourceDirectory>src/main/java</sourceDirectory>
|
||||||
|
|||||||
@@ -1,10 +1,14 @@
|
|||||||
package fr.klemek.logger;
|
package fr.klemek.logger;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
import java.util.logging.FileHandler;
|
||||||
|
import java.util.logging.Handler;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.LogManager;
|
import java.util.logging.LogManager;
|
||||||
|
import java.util.logging.SimpleFormatter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Simple logger for this application.
|
* Simple logger for this application.
|
||||||
@@ -13,12 +17,13 @@ import java.util.logging.LogManager;
|
|||||||
*/
|
*/
|
||||||
public final class Logger {
|
public final class Logger {
|
||||||
|
|
||||||
private static java.util.logging.Logger appLogger = java.util.logging.Logger.getLogger(Utils.getString("app_name","Unkown"));
|
private static java.util.logging.Logger appLogger = java.util.logging.Logger.getLogger("Unknown");
|
||||||
private static final String APP_NAME = Utils.getString("app_name","Unkown");
|
private static String appName = "Unknown";
|
||||||
private static final String BASE_PACKAGE = Utils.getString("default_package",null);
|
private static String basePackage = null;
|
||||||
|
private static boolean initialized = false;
|
||||||
private static final int DEFAULT_DEPTH = 4;
|
private static final int DEFAULT_DEPTH = 4;
|
||||||
|
|
||||||
private static boolean initialized = false;
|
|
||||||
|
|
||||||
private Logger() {
|
private Logger() {
|
||||||
}
|
}
|
||||||
@@ -74,7 +79,19 @@ public final class Logger {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
LogManager.getLogManager().readConfiguration(is);
|
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) {
|
} catch (IOException e) {
|
||||||
Logger.log(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.");
|
Logger.log(Level.WARNING, "Logger was not initialized please do so before using.");
|
||||||
initialized = true;
|
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);
|
appLogger.log(lvl, message, objects);
|
||||||
if (lvl == Level.SEVERE && objects.length > 0 && objects[0] instanceof Exception) {
|
if (lvl == Level.SEVERE && objects.length > 0 && objects[0] instanceof Exception) {
|
||||||
boolean inPackage = false;
|
boolean inPackage = false;
|
||||||
for (StackTraceElement ste : ((Exception) objects[0]).getStackTrace()) {
|
for (StackTraceElement ste : ((Exception) objects[0]).getStackTrace()) {
|
||||||
Logger.log(depth + 1, Level.SEVERE, "\t {0}", ste);
|
Logger.log(depth + 1, Level.SEVERE, "\t {0}", ste);
|
||||||
if(Logger.BASE_PACKAGE != null){
|
if(Logger.basePackage != null){
|
||||||
if (!inPackage && ste.getClassName().startsWith(Logger.BASE_PACKAGE))
|
if (!inPackage && ste.getClassName().startsWith(Logger.basePackage))
|
||||||
inPackage = true;
|
inPackage = true;
|
||||||
else if (inPackage && !ste.getClassName().startsWith(Logger.BASE_PACKAGE))
|
else if (inPackage && !ste.getClassName().startsWith(Logger.basePackage))
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,8 +13,6 @@ import java.util.logging.Level;
|
|||||||
*/
|
*/
|
||||||
final class Utils {
|
final class Utils {
|
||||||
|
|
||||||
private static final ResourceBundle CONFIGURATION_BUNDLE = ResourceBundle.getBundle("logging");
|
|
||||||
|
|
||||||
private Utils() {
|
private Utils() {
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -25,13 +23,15 @@ final class Utils {
|
|||||||
/**
|
/**
|
||||||
* Get a configuration string by its key.
|
* Get a configuration string by its key.
|
||||||
*
|
*
|
||||||
|
* @param bundlePath the path to the configuration file
|
||||||
* @param key the key in the config file
|
* @param key the key in the config file
|
||||||
* @param defaultValue the default value to use if not found
|
* @param defaultValue the default value to use if not found
|
||||||
* @return the string or default value 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 {
|
try {
|
||||||
return CONFIGURATION_BUNDLE.getString(key);
|
return ResourceBundle.getBundle(bundlePath.substring(0,pos)).getString(key);
|
||||||
} catch (MissingResourceException e) {
|
} catch (MissingResourceException e) {
|
||||||
return defaultValue;
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -9,4 +9,6 @@ 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=Test
|
app_name=Test
|
||||||
#(Optional, specify your app package)
|
#(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
|
||||||
Reference in New Issue
Block a user