Added SystemProperty accessor class
This commit is contained in:
parent
e7da897a1c
commit
227f313461
|
@ -11,13 +11,17 @@ import java.util.logging.SimpleFormatter;
|
|||
import java.util.regex.Pattern;
|
||||
|
||||
import net.filebot.cli.CmdlineInterface;
|
||||
import net.filebot.util.SystemProperty;
|
||||
|
||||
import org.codehaus.groovy.runtime.StackTraceUtils;
|
||||
|
||||
public final class Logging {
|
||||
|
||||
public static final Logger log = getConsoleLogger(CmdlineInterface.class, Level.ALL, new ConsoleFormatter(getAnonymizePattern()));
|
||||
public static final Logger debug = getConsoleLogger(Logging.class, Level.ALL, new SimpleFormatter());
|
||||
private static final SystemProperty<Level> debugLevel = SystemProperty.of("net.filebot.logging.debug", Level::parse, Level.CONFIG);
|
||||
private static final SystemProperty<Pattern> anonymizePattern = SystemProperty.of("net.filebot.logging.anonymize", s -> Pattern.compile(s, Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CHARACTER_CLASS | Pattern.MULTILINE));
|
||||
|
||||
public static final Logger log = getConsoleLogger(CmdlineInterface.class, Level.ALL, new ConsoleFormatter(anonymizePattern.get()));
|
||||
public static final Logger debug = getConsoleLogger(Logging.class, debugLevel.get(), new SimpleFormatter());
|
||||
|
||||
public static Logger getConsoleLogger(Class<?> cls, Level level, Formatter formatter) {
|
||||
Logger log = Logger.getLogger(cls.getPackage().getName());
|
||||
|
@ -31,14 +35,6 @@ public final class Logging {
|
|||
return () -> String.format(format, args);
|
||||
}
|
||||
|
||||
private static Pattern getAnonymizePattern() {
|
||||
String pattern = System.getProperty("net.filebot.logging.anonymize");
|
||||
if (pattern != null && pattern.length() > 0) {
|
||||
return Pattern.compile(pattern, Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CHARACTER_CLASS | Pattern.MULTILINE);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static class ConsoleHandler extends Handler {
|
||||
|
||||
public ConsoleHandler(Level level, Formatter formatter) {
|
||||
|
@ -88,7 +84,7 @@ public final class Logging {
|
|||
public String format(LogRecord record) {
|
||||
String message = record.getMessage();
|
||||
if (anonymize != null) {
|
||||
message = anonymize.matcher(record.getMessage()).replaceAll("");
|
||||
message = anonymize.matcher(message).replaceAll("");
|
||||
}
|
||||
return message + System.lineSeparator();
|
||||
}
|
||||
|
|
|
@ -0,0 +1,45 @@
|
|||
package net.filebot.util;
|
||||
|
||||
import java.util.function.Function;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class SystemProperty<T> {
|
||||
|
||||
public static <T> SystemProperty<T> of(String key, Function<String, T> valueFunction, T defaultValue) {
|
||||
return new SystemProperty<T>(key, valueFunction, defaultValue);
|
||||
}
|
||||
|
||||
public static <T> SystemProperty<T> of(String key, Function<String, T> valueFunction) {
|
||||
return new SystemProperty<T>(key, valueFunction, null);
|
||||
}
|
||||
|
||||
private final String key;
|
||||
private final Function<String, T> valueFunction;
|
||||
private final T defaultValue;
|
||||
|
||||
public SystemProperty(String key, Function<String, T> valueFunction, T defaultValue) {
|
||||
this.key = key;
|
||||
this.valueFunction = valueFunction;
|
||||
this.defaultValue = defaultValue;
|
||||
}
|
||||
|
||||
public T get() {
|
||||
String prop = System.getProperty(key);
|
||||
|
||||
if (prop != null && prop.length() > 0) {
|
||||
try {
|
||||
return valueFunction.apply(prop);
|
||||
} catch (Exception e) {
|
||||
Logger.getLogger(SystemProperty.class.getName()).logp(Level.WARNING, SystemProperty.class.getName(), key, e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public void set(T value) {
|
||||
System.setProperty(key, value.toString());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue