1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-12-23 16:28:51 -05:00

Refactor console logging

This commit is contained in:
Reinhard Pointner 2016-03-02 16:14:16 +00:00
parent 09ef06496d
commit e7da897a1c

View File

@ -16,7 +16,7 @@ import org.codehaus.groovy.runtime.StackTraceUtils;
public final class Logging {
public static final Logger log = getConsoleLogger(CmdlineInterface.class, Level.ALL, new ConsoleFormatter());
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());
public static Logger getConsoleLogger(Class<?> cls, Level level, Formatter formatter) {
@ -31,6 +31,14 @@ 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) {
@ -70,24 +78,20 @@ public final class Logging {
public static class ConsoleFormatter extends Formatter {
private final Pattern anonymize;
public ConsoleFormatter(Pattern anonymize) {
this.anonymize = anonymize;
}
@Override
public String format(LogRecord record) {
if (ANONYMIZE_PATTERN != null) {
return ANONYMIZE_PATTERN.matcher(record.getMessage()).replaceAll("") + System.lineSeparator();
String message = record.getMessage();
if (anonymize != null) {
message = anonymize.matcher(record.getMessage()).replaceAll("");
}
return record.getMessage() + System.lineSeparator();
return message + System.lineSeparator();
}
}
private static final Pattern ANONYMIZE_PATTERN = getAnonymizePattern();
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;
}
}