simple-logger v1.0
This commit is contained in:
@@ -0,0 +1,172 @@
|
||||
package fr.klemek.logger;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Locale;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.LogManager;
|
||||
|
||||
/**
|
||||
* Simple logger for this application.
|
||||
*
|
||||
* @author Clement Gouin
|
||||
*/
|
||||
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 final int DEFAULT_DEPTH = 4;
|
||||
|
||||
private static boolean initialized = false;
|
||||
|
||||
private Logger() {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the current logger level;
|
||||
*/
|
||||
public static Level getLevel() {
|
||||
return appLogger.getLevel();
|
||||
}
|
||||
|
||||
/**
|
||||
* Change the log level of this logger.
|
||||
*
|
||||
* @param newLevel the level of log to show
|
||||
*/
|
||||
public static void setLevel(Level newLevel) {
|
||||
appLogger.setLevel(newLevel);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a config file for the logger and set locale to english.
|
||||
*
|
||||
* @param relativePath the path in resources of the config file
|
||||
*/
|
||||
public static void init(String relativePath) {
|
||||
init(relativePath, Level.INFO);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a config file for the logger and set locale to english.
|
||||
*
|
||||
* @param relativePath the path in resources of the config file
|
||||
* @param level the level to set the logger to
|
||||
*/
|
||||
public static void init(String relativePath, Level level) {
|
||||
Locale.setDefault(Locale.ENGLISH);
|
||||
loadConfigFromFile(relativePath);
|
||||
Logger.setLevel(level);
|
||||
Logger.initialized = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a config file for the logger.
|
||||
*
|
||||
* @param relativePath the path in resources of the config file
|
||||
*/
|
||||
private static void loadConfigFromFile(String relativePath) {
|
||||
try {
|
||||
InputStream is = Logger.class.getClassLoader().getResourceAsStream(relativePath);
|
||||
if (is == null) {
|
||||
Logger.log(Level.SEVERE, "Logger config file not found at path {0}", relativePath);
|
||||
return;
|
||||
}
|
||||
LogManager.getLogManager().readConfiguration(is);
|
||||
appLogger = java.util.logging.Logger.getLogger(APP_NAME);
|
||||
} catch (IOException e) {
|
||||
Logger.log(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Log an exception.
|
||||
*
|
||||
* @param e the exception to log
|
||||
*/
|
||||
public static void log(Exception e) {
|
||||
Logger.log(Logger.DEFAULT_DEPTH, Level.SEVERE, e.toString(), e);
|
||||
}
|
||||
|
||||
/**
|
||||
* Log an exception.
|
||||
*
|
||||
* @param lvl the level of logging
|
||||
* @param e the exception to log
|
||||
*/
|
||||
public static void log(Level lvl, Exception e) {
|
||||
Logger.log(Logger.DEFAULT_DEPTH, lvl, e.toString(), e);
|
||||
}
|
||||
|
||||
/**
|
||||
* Log an exception.
|
||||
*
|
||||
* @param e the exception to log
|
||||
* @param msg the exception msg
|
||||
*/
|
||||
public static void log(Exception e, String msg) {
|
||||
Logger.log(Logger.DEFAULT_DEPTH, Level.SEVERE, msg + " : {0}", e);
|
||||
}
|
||||
|
||||
/**
|
||||
* Log an exception.
|
||||
*
|
||||
* @param lvl the level of logging
|
||||
* @param e the exception to log
|
||||
* @param msg the exception msg
|
||||
*/
|
||||
public static void log(Level lvl, Exception e, String msg) {
|
||||
Logger.log(Logger.DEFAULT_DEPTH, lvl, msg + " : {0}", e);
|
||||
}
|
||||
|
||||
/**
|
||||
* Log an INFO message.
|
||||
*
|
||||
* @param message the message
|
||||
* @param objects the object for the message formatting
|
||||
*/
|
||||
public static void log(String message, Object... objects) {
|
||||
Logger.log(Logger.DEFAULT_DEPTH, Level.INFO, message, objects);
|
||||
}
|
||||
|
||||
/**
|
||||
* Log a message.
|
||||
*
|
||||
* @param lvl the level of logging
|
||||
* @param message the message
|
||||
* @param objects the object for the message formatting
|
||||
*/
|
||||
public static void log(Level lvl, String message, Object... objects) {
|
||||
Logger.log(Logger.DEFAULT_DEPTH, lvl, message, objects);
|
||||
}
|
||||
|
||||
/**
|
||||
* Log a message.
|
||||
*
|
||||
* @param depth the source depth in stack
|
||||
* @param lvl the level of logging
|
||||
* @param message the message
|
||||
* @param objects the object for the message formatting
|
||||
*/
|
||||
private static void log(int depth, Level lvl, String message, Object... objects) {
|
||||
if(!initialized) {
|
||||
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);
|
||||
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))
|
||||
inPackage = true;
|
||||
else if (inPackage && !ste.getClassName().startsWith(Logger.BASE_PACKAGE))
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package fr.klemek.logger;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.MissingResourceException;
|
||||
import java.util.ResourceBundle;
|
||||
import java.util.logging.Level;
|
||||
|
||||
/**
|
||||
* Utility class that store useful misc functions.
|
||||
*
|
||||
* @author Clement Gouin
|
||||
*/
|
||||
final class Utils {
|
||||
|
||||
private static final ResourceBundle CONFIGURATION_BUNDLE = ResourceBundle.getBundle("logging");
|
||||
|
||||
private Utils() {
|
||||
}
|
||||
|
||||
/*
|
||||
* Configuration utils
|
||||
*/
|
||||
|
||||
/**
|
||||
* Get a configuration string by its key.
|
||||
*
|
||||
* @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) {
|
||||
try {
|
||||
return CONFIGURATION_BUNDLE.getString(key);
|
||||
} catch (MissingResourceException e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the class name from the calling class in th stack trace.
|
||||
*
|
||||
* @param stackLevel the level in the stack trace
|
||||
* @return the classname of th calling class
|
||||
*/
|
||||
static String getCallingClassName(int stackLevel) {
|
||||
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
|
||||
if (stackLevel >= stackTrace.length)
|
||||
return null;
|
||||
String[] source = stackTrace[stackLevel].getClassName().split("\\.");
|
||||
return source[source.length - 1];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
#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
|
||||
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
|
||||
Reference in New Issue
Block a user