From 227f3134611a06d564bb95a06101b4819022b8ed Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Wed, 2 Mar 2016 17:10:18 +0000 Subject: [PATCH] Added SystemProperty accessor class --- source/net/filebot/Logging.java | 18 ++++----- source/net/filebot/util/SystemProperty.java | 45 +++++++++++++++++++++ 2 files changed, 52 insertions(+), 11 deletions(-) create mode 100644 source/net/filebot/util/SystemProperty.java diff --git a/source/net/filebot/Logging.java b/source/net/filebot/Logging.java index 4ba60dc4..76e0fd1e 100644 --- a/source/net/filebot/Logging.java +++ b/source/net/filebot/Logging.java @@ -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 debugLevel = SystemProperty.of("net.filebot.logging.debug", Level::parse, Level.CONFIG); + private static final SystemProperty 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(); } diff --git a/source/net/filebot/util/SystemProperty.java b/source/net/filebot/util/SystemProperty.java new file mode 100644 index 00000000..c49be43c --- /dev/null +++ b/source/net/filebot/util/SystemProperty.java @@ -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 { + + public static SystemProperty of(String key, Function valueFunction, T defaultValue) { + return new SystemProperty(key, valueFunction, defaultValue); + } + + public static SystemProperty of(String key, Function valueFunction) { + return new SystemProperty(key, valueFunction, null); + } + + private final String key; + private final Function valueFunction; + private final T defaultValue; + + public SystemProperty(String key, Function 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()); + } + +}