filebot/source/net/filebot/Logging.java

94 lines
2.7 KiB
Java
Raw Normal View History

2016-03-02 15:02:44 +00:00
package net.filebot;
import java.io.PrintStream;
import java.util.function.Supplier;
2016-03-02 15:55:06 +00:00
import java.util.logging.Formatter;
2016-03-02 15:02:44 +00:00
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
2016-03-02 15:55:06 +00:00
import java.util.logging.SimpleFormatter;
2016-03-02 15:02:44 +00:00
import java.util.regex.Pattern;
import net.filebot.cli.CmdlineInterface;
2016-03-02 17:10:18 +00:00
import net.filebot.util.SystemProperty;
2016-03-02 15:02:44 +00:00
import org.codehaus.groovy.runtime.StackTraceUtils;
public final class Logging {
2016-03-02 17:10:18 +00:00
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());
2016-03-02 15:02:44 +00:00
2016-03-02 15:55:06 +00:00
public static Logger getConsoleLogger(Class<?> cls, Level level, Formatter formatter) {
2016-03-02 15:02:44 +00:00
Logger log = Logger.getLogger(cls.getPackage().getName());
log.setLevel(level);
log.setUseParentHandlers(false);
2016-03-02 15:55:06 +00:00
log.addHandler(new ConsoleHandler(level, formatter));
2016-03-02 15:02:44 +00:00
return log;
}
public static Supplier<String> format(String format, Object... args) {
return () -> String.format(format, args);
}
2016-03-02 15:55:06 +00:00
public static class ConsoleHandler extends Handler {
2016-03-02 15:02:44 +00:00
2016-03-02 15:55:06 +00:00
public ConsoleHandler(Level level, Formatter formatter) {
setLevel(level);
setFormatter(formatter);
2016-03-02 15:02:44 +00:00
}
@Override
public void publish(LogRecord record) {
// use either System.out or System.err depending on the severity of the error
PrintStream out = record.getLevel().intValue() < Level.WARNING.intValue() ? System.out : System.err;
// print messages
2016-03-02 15:55:06 +00:00
out.print(getFormatter().format(record));
Throwable t = record.getThrown();
if (t != null) {
StackTraceUtils.deepSanitize(t).printStackTrace(out);
}
2016-03-02 15:02:44 +00:00
// flush every message immediately
out.flush();
}
@Override
public void flush() {
System.out.flush();
System.err.flush();
}
@Override
public void close() throws SecurityException {
}
}
2016-03-02 15:55:06 +00:00
public static class ConsoleFormatter extends Formatter {
2016-03-02 16:14:16 +00:00
private final Pattern anonymize;
public ConsoleFormatter(Pattern anonymize) {
this.anonymize = anonymize;
}
2016-03-02 15:55:06 +00:00
@Override
public String format(LogRecord record) {
2016-03-02 16:14:16 +00:00
String message = record.getMessage();
if (anonymize != null) {
2016-03-02 17:10:18 +00:00
message = anonymize.matcher(message).replaceAll("");
2016-03-02 15:55:06 +00:00
}
2016-03-02 16:14:16 +00:00
return message + System.lineSeparator();
2016-03-02 15:55:06 +00:00
}
}
2016-03-02 15:02:44 +00:00
}