mirror of
https://github.com/mitb-archive/filebot
synced 2024-11-16 06:15:02 -05:00
Refactor console logging
This commit is contained in:
parent
e9ddee19d6
commit
09ef06496d
@ -2,10 +2,12 @@ package net.filebot;
|
|||||||
|
|
||||||
import java.io.PrintStream;
|
import java.io.PrintStream;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
import java.util.logging.Formatter;
|
||||||
import java.util.logging.Handler;
|
import java.util.logging.Handler;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.LogRecord;
|
import java.util.logging.LogRecord;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
import java.util.logging.SimpleFormatter;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import net.filebot.cli.CmdlineInterface;
|
import net.filebot.cli.CmdlineInterface;
|
||||||
@ -14,14 +16,14 @@ import org.codehaus.groovy.runtime.StackTraceUtils;
|
|||||||
|
|
||||||
public final class Logging {
|
public final class Logging {
|
||||||
|
|
||||||
public static final Logger log = getConsoleLogger(CmdlineInterface.class, Level.ALL);
|
public static final Logger log = getConsoleLogger(CmdlineInterface.class, Level.ALL, new ConsoleFormatter());
|
||||||
public static final Logger debug = getConsoleLogger(Logging.class, Level.CONFIG);
|
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());
|
Logger log = Logger.getLogger(cls.getPackage().getName());
|
||||||
log.setLevel(level);
|
log.setLevel(level);
|
||||||
log.setUseParentHandlers(false);
|
log.setUseParentHandlers(false);
|
||||||
log.addHandler(new ConsoleHandler());
|
log.addHandler(new ConsoleHandler(level, formatter));
|
||||||
return log;
|
return log;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -29,42 +31,25 @@ public final class Logging {
|
|||||||
return () -> String.format(format, args);
|
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 static class ConsoleHandler extends Handler {
|
||||||
|
|
||||||
|
public ConsoleHandler(Level level, Formatter formatter) {
|
||||||
|
setLevel(level);
|
||||||
|
setFormatter(formatter);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void publish(LogRecord record) {
|
public void publish(LogRecord record) {
|
||||||
// use either System.out or System.err depending on the severity of the error
|
// 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;
|
PrintStream out = record.getLevel().intValue() < Level.WARNING.intValue() ? System.out : System.err;
|
||||||
|
|
||||||
// print messages
|
// print messages
|
||||||
printMessage(record.getMessage(), out);
|
out.print(getFormatter().format(record));
|
||||||
printStackTrace(record.getThrown(), out);
|
|
||||||
|
Throwable t = record.getThrown();
|
||||||
|
if (t != null) {
|
||||||
|
StackTraceUtils.deepSanitize(t).printStackTrace(out);
|
||||||
|
}
|
||||||
|
|
||||||
// flush every message immediately
|
// flush every message immediately
|
||||||
out.flush();
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package net.filebot;
|
|||||||
import static java.awt.GraphicsEnvironment.*;
|
import static java.awt.GraphicsEnvironment.*;
|
||||||
import static java.util.regex.Pattern.*;
|
import static java.util.regex.Pattern.*;
|
||||||
import static javax.swing.JOptionPane.*;
|
import static javax.swing.JOptionPane.*;
|
||||||
|
import static net.filebot.Logging.*;
|
||||||
import static net.filebot.Settings.*;
|
import static net.filebot.Settings.*;
|
||||||
import static net.filebot.util.FileUtilities.*;
|
import static net.filebot.util.FileUtilities.*;
|
||||||
import static net.filebot.util.ui.SwingUI.*;
|
import static net.filebot.util.ui.SwingUI.*;
|
||||||
@ -87,17 +88,17 @@ public class Main {
|
|||||||
|
|
||||||
if (args.clearCache() || args.clearUserData()) {
|
if (args.clearCache() || args.clearUserData()) {
|
||||||
if (args.clearUserData()) {
|
if (args.clearUserData()) {
|
||||||
System.out.println("Reset preferences");
|
log.info("Reset preferences");
|
||||||
Settings.forPackage(Main.class).clear();
|
Settings.forPackage(Main.class).clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (args.clearCache()) {
|
if (args.clearCache()) {
|
||||||
// clear preferences and cache
|
// 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)) {
|
for (File folder : getChildren(getApplicationFolder().getAbsoluteFile(), FOLDERS)) {
|
||||||
if (matches("cache|temp|grape|reports|logs", folder.getName())) {
|
if (matches("cache|temp|grape|reports|logs", folder.getName())) {
|
||||||
if (delete(folder)) {
|
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);
|
FileChannel logChannel = FileChannel.open(logFile.toPath(), StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.APPEND);
|
||||||
if (args.logLock) {
|
if (args.logLock) {
|
||||||
if (args.getLogLevel() == Level.ALL) {
|
if (args.getLogLevel() == Level.ALL) {
|
||||||
System.out.println("Locking " + logFile);
|
log.config("Locking " + logFile);
|
||||||
}
|
}
|
||||||
logChannel.lock();
|
logChannel.lock();
|
||||||
}
|
}
|
||||||
@ -485,7 +486,7 @@ public class Main {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (cacheRevision != applicationRevision && applicationRevision > 0 && !isNewCache) {
|
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
|
// tag cache with new revision number
|
||||||
isNewCache = true;
|
isNewCache = true;
|
||||||
|
@ -155,11 +155,9 @@ public class MediaInfo implements Closeable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void dispose() {
|
public synchronized void dispose() {
|
||||||
if (handle == null)
|
if (handle == null) {
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
// DEBUG
|
|
||||||
// System.out.println("Dispose " + this);
|
|
||||||
|
|
||||||
// delete handle
|
// delete handle
|
||||||
MediaInfoLibrary.INSTANCE.Delete(handle);
|
MediaInfoLibrary.INSTANCE.Delete(handle);
|
||||||
|
Loading…
Reference in New Issue
Block a user