Initial commit

This commit is contained in:
Isaac Parenteau
2018-09-13 21:02:36 -05:00
commit a65c54ca80
14 changed files with 1202 additions and 0 deletions

View File

@@ -0,0 +1,217 @@
package net.locusworks.logger;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;
/***
* Logger factory to create/retrieve loggers for the portal application
* @author Isaac Parenteau
* @version 1.0.0
* @date 02/15/2018
*/
public class ApplicationLoggerFactory {
private static Map<String, ApplicationLogger> loggers = new TreeMap<>();
private static LogLevel DEFAULT_LEVEL = null;
private static ApplicationLoggerInitializer initializer;
/**
* Need to get the log level from the properties file.
* Would normally use the configuration service but would create a circular dependency as
* the configuration service use the ApplicationLoggerFactory to create a logger
* @param initializer The initializer
*/
public static void init(ApplicationLoggerInitializer init) {
initializer = init;
DEFAULT_LEVEL = initializer == null ? LogLevel.INFO : initializer.initialize();
loadLoggers();
}
/**
* Load loggers from ApplicationLoggerInitializer interface
*/
public static void loadLoggers() {
loadLoggers(initializer);
}
/**
* Load loggers from ApplicationLoggerInitializer interface
* @param init interface to try to load loggers from
*/
public static void loadLoggers(ApplicationLoggerInitializer init) {
if (init != null) {
init.loadLoggers();
}
}
/**
* Retrieve a portal logger
* @param name Name of the logger to retrieve
* @return the logger representation of the name used
*/
public static ApplicationLogger getLogger(String name) {
return getLogger(name, getDefaultLevel());
}
/**
* Get the default logger level. if the default
* has not been set, call the <b>init</b> function
* @return
*/
private static LogLevel getDefaultLevel() {
return DEFAULT_LEVEL;
}
/**
* Retrieve a portal logger
* @param name Name of the logger to retrieve
* @param level Starting log level of the logger
* @return the logger represented by the given name
*/
public static ApplicationLogger getLogger(String name, LogLevel level) {
if (!loggers.containsKey(name)) {
loggers.put(name, new ApplicationLogger(name, level));
}
ApplicationLogger logger = loggers.get(name);
if (initializer != null) {
initializer.postGetLogger(logger.getName(), logger.getLogLevel().toName());
}
return logger;
}
/**
* Get a portal logger
* @param clazz Class to get the logger for
* @return patchRepoLogger
*/
public static ApplicationLogger getLogger(Class<?> clazz) {
return getLogger(clazz, true);
}
/**
* Get a portal logger
* @param clazz Class to get the logger for
* @param useSimpleName use the simplified class name
* @return patchRepoLogger
*/
public static ApplicationLogger getLogger(Class<?> clazz, boolean useSimpleName) {
LogLevel level = getDefaultLevel();
String name = useSimpleName ? clazz.getSimpleName() : clazz.getName();
if (clazz.isAnnotationPresent(ApplicationLoggerInfo.class)) {
ApplicationLoggerInfo prli = clazz.getAnnotation(ApplicationLoggerInfo.class);
String tmpName = prli.name();
level = prli.defaultLevel();
if (tmpName != null && !tmpName.trim().isEmpty()) {
name = tmpName;
}
}
return getLogger(name, level);
}
/**
* Set individual loggers to a specified log level
* @param logLevels List of loggers to configure
*/
public static void setLogLevels(List<LoggerInfo> logLevels) {
logLevels
.stream()
.filter(logInfo -> loggers.containsKey(logInfo.getLogger()))
.forEach(logInfo -> {
loggers.get(logInfo.getLogger()).setLogLevel(LogLevel.valueOf(logInfo.getLevel()));
});
}
/**
* Add a logger to to the collection to be monitored
* @param logger Logger to add
*/
public static void addLogger(ApplicationLogger logger) {
loggers.put(logger.getName(), logger);
}
/**
* Add loggers to the current logger set
* @param loggerList List of loggers to add
*/
public static void addLoggers(List<LoggerInfo> loggerList) {
loggerList.stream()
.map(item -> new ApplicationLogger(item.getLogger(), LogLevel.getEnum(item.getLevel())))
.forEach(item -> loggers.put(item.getName(), item));
}
/**
* Get all the currently available loggers
* @return loggers
*/
public static List<LoggerInfo> getLoggers() {
return loggers.entrySet()
.stream()
.filter(entry -> entry.getValue().getLogLevel() != null)
.map(entry -> new LoggerInfo(entry.getKey(), entry.getValue().getLogLevel().toString()) )
.collect(Collectors.toList());
}
/**
* Save the log levels through the interface
*/
public static void saveLogLevels() {
saveLogLevels(initializer);
}
/**
* Save the log levels through the interface
* @param init ApplicationLoggerInitializer interface
*/
public static void saveLogLevels(ApplicationLoggerInitializer init) {
if (init != null) {
init.saveLogLevels(loggers);
}
}
/**
* Helper class to display logger information for
* @author Isaac Parenteau
* @since 1.0.0-RELEASE
*/
public static class LoggerInfo {
private String logger;
private String level;
public LoggerInfo() {}
public LoggerInfo(String logger, String level) {
this.logger = logger;
this.level = level;
}
/**
* @return the logger
*/
public String getLogger() {
return logger;
}
/**
* @param logger the logger to set
*/
public void setLogger(String logger) {
this.logger = logger;
}
/**
* @return the current logger level
*/
public String getLevel() {
return level;
}
/**
* @param level the level to set
*/
public void setLevel(String level) {
this.level = level;
}
}
}