filebot/source/net/filebot/Logging.java

119 lines
3.3 KiB
Java
Raw Normal View History

2016-03-02 15:02:44 +00:00
package net.filebot;
2016-03-09 16:18:20 +00:00
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
2016-03-02 15:02:44 +00:00
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-09 16:18:20 +00:00
import java.util.logging.SimpleFormatter;
import java.util.logging.StreamHandler;
2016-03-02 15:02:44 +00:00
import java.util.regex.Pattern;
import org.codehaus.groovy.runtime.StackTraceUtils;
2016-03-10 14:38:34 +00:00
import net.filebot.util.SystemProperty;
2016-03-02 15:02:44 +00:00
public final class Logging {
private static final SystemProperty<Pattern> anonymizePattern = SystemProperty.of("net.filebot.logging.anonymize", Pattern::compile);
2016-03-15 14:57:21 +00:00
private static final SystemProperty<Level> debugLevel = SystemProperty.of("net.filebot.logging.debug", Level::parse, Level.WARNING);
2016-03-02 17:10:18 +00:00
2016-03-10 14:38:34 +00:00
public static final Logger log = createConsoleLogger("net.filebot.console", Level.ALL);
public static final Logger debug = createConsoleLogger("net.filebot.debug", debugLevel.get());
2016-03-02 15:02:44 +00:00
2016-03-10 14:38:34 +00:00
public static Logger createConsoleLogger(String name, Level level) {
2016-03-02 17:25:14 +00:00
Logger log = Logger.getLogger(name);
2016-03-02 15:02:44 +00:00
log.setUseParentHandlers(false);
2016-03-10 14:38:34 +00:00
log.setLevel(level);
log.addHandler(createConsoleHandler(level));
2016-03-02 15:02:44 +00:00
return log;
}
2016-03-09 16:18:20 +00:00
public static StreamHandler createSimpleFileHandler(File file, Level level) throws IOException {
StreamHandler handler = new StreamHandler(new FileOutputStream(file, true), new SimpleFormatter());
handler.setLevel(level);
return handler;
}
2016-03-10 14:38:34 +00:00
public static ConsoleHandler createConsoleHandler(Level level) {
ConsoleHandler handler = new ConsoleHandler();
handler.setLevel(level);
handler.setFormatter(new ConsoleFormatter(anonymizePattern.get()));
return handler;
}
2016-03-02 15:02:44 +00:00
public static Supplier<String> format(String format, Object... args) {
return () -> String.format(format, args);
}
2016-04-12 06:19:45 +00:00
public static Supplier<String> trace(Throwable t) {
return () -> {
StringBuilder s = new StringBuilder();
s.append(t.getClass().getSimpleName()).append(": ");
s.append(t.getMessage());
StackTraceElement[] trace = StackTraceUtils.sanitizeRootCause(t).getStackTrace();
if (trace != null && trace.length > 0) {
s.append(" at ").append(trace[0]);
}
return s.toString();
};
}
2016-03-02 15:55:06 +00:00
public static class ConsoleHandler extends Handler {
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));
2016-03-09 16:02:36 +00:00
Throwable thrown = record.getThrown();
if (thrown != null) {
StackTraceUtils.deepSanitize(thrown).printStackTrace(out);
2016-03-02 15:55:06 +00:00
}
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();
2016-03-11 08:17:02 +00:00
if (anonymize != null && message != 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
}