Refactor console logging

This commit is contained in:
Reinhard Pointner 2016-03-02 15:55:06 +00:00
parent e9ddee19d6
commit 09ef06496d
3 changed files with 47 additions and 41 deletions

View File

@ -2,10 +2,12 @@ package net.filebot;
import java.io.PrintStream;
import java.util.function.Supplier;
import java.util.logging.Formatter;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import java.util.regex.Pattern;
import net.filebot.cli.CmdlineInterface;
@ -14,14 +16,14 @@ import org.codehaus.groovy.runtime.StackTraceUtils;
public final class Logging {
public static final Logger log = getConsoleLogger(CmdlineInterface.class, Level.ALL);
public static final Logger debug = getConsoleLogger(Logging.class, Level.CONFIG);
public static final Logger log = getConsoleLogger(CmdlineInterface.class, Level.ALL, new ConsoleFormatter());
public static final Logger debug = getConsoleLogger(Logging.class, Level.ALL, new SimpleFormatter());
public static Logger getConsoleLogger(Class<?> cls, Level level) {
public static Logger getConsoleLogger(Class<?> cls, Level level, Formatter formatter) {
Logger log = Logger.getLogger(cls.getPackage().getName());
log.setLevel(level);
log.setUseParentHandlers(false);
log.addHandler(new ConsoleHandler());
log.addHandler(new ConsoleHandler(level, formatter));
return log;
}
@ -29,42 +31,25 @@ public final class Logging {
return () -> String.format(format, args);
}
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;
}
private static void printMessage(String message, PrintStream out) {
if (message != null && message.length() > 0) {
if (ANONYMIZE_PATTERN == null) {
out.println(ANONYMIZE_PATTERN.matcher(message).replaceAll(""));
} else {
out.println(message);
}
}
}
private static void printStackTrace(Throwable throwable, PrintStream out) {
if (throwable != null) {
StackTraceUtils.deepSanitize(throwable).printStackTrace(out);
}
}
public static class ConsoleHandler extends Handler {
public ConsoleHandler(Level level, Formatter formatter) {
setLevel(level);
setFormatter(formatter);
}
@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
printMessage(record.getMessage(), out);
printStackTrace(record.getThrown(), out);
out.print(getFormatter().format(record));
Throwable t = record.getThrown();
if (t != null) {
StackTraceUtils.deepSanitize(t).printStackTrace(out);
}
// flush every message immediately
out.flush();
@ -83,4 +68,26 @@ public final class Logging {
}
public static class ConsoleFormatter extends Formatter {
@Override
public String format(LogRecord record) {
if (ANONYMIZE_PATTERN != null) {
return ANONYMIZE_PATTERN.matcher(record.getMessage()).replaceAll("") + System.lineSeparator();
}
return record.getMessage() + 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;
}
}

View File

@ -3,6 +3,7 @@ package net.filebot;
import static java.awt.GraphicsEnvironment.*;
import static java.util.regex.Pattern.*;
import static javax.swing.JOptionPane.*;
import static net.filebot.Logging.*;
import static net.filebot.Settings.*;
import static net.filebot.util.FileUtilities.*;
import static net.filebot.util.ui.SwingUI.*;
@ -87,17 +88,17 @@ public class Main {
if (args.clearCache() || args.clearUserData()) {
if (args.clearUserData()) {
System.out.println("Reset preferences");
log.info("Reset preferences");
Settings.forPackage(Main.class).clear();
}
if (args.clearCache()) {
// clear preferences and cache
System.out.println("Clear cache and temporary files");
log.info("Clear cache and temporary files");
for (File folder : getChildren(getApplicationFolder().getAbsoluteFile(), FOLDERS)) {
if (matches("cache|temp|grape|reports|logs", folder.getName())) {
if (delete(folder)) {
System.out.println("* Delete " + folder);
log.config("* Delete " + folder);
}
}
}
@ -124,7 +125,7 @@ public class Main {
FileChannel logChannel = FileChannel.open(logFile.toPath(), StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.APPEND);
if (args.logLock) {
if (args.getLogLevel() == Level.ALL) {
System.out.println("Locking " + logFile);
log.config("Locking " + logFile);
}
logChannel.lock();
}
@ -485,7 +486,7 @@ public class Main {
}
if (cacheRevision != applicationRevision && applicationRevision > 0 && !isNewCache) {
Logger.getLogger(Main.class.getName()).log(Level.WARNING, String.format("App version (r%d) does not match cache version (r%d): reset cache", applicationRevision, cacheRevision));
debug.log(Level.WARNING, format("App version (r%d) does not match cache version (r%d): reset cache", applicationRevision, cacheRevision));
// tag cache with new revision number
isNewCache = true;

View File

@ -155,11 +155,9 @@ public class MediaInfo implements Closeable {
}
public synchronized void dispose() {
if (handle == null)
if (handle == null) {
return;
// DEBUG
// System.out.println("Dispose " + this);
}
// delete handle
MediaInfoLibrary.INSTANCE.Delete(handle);