Use internal logging for --log-file instead of copying stdio/stderr to file (which doesn't work for native code or sub-process console output anyway)

This commit is contained in:
Reinhard Pointner 2017-01-28 01:22:46 +08:00
parent 892e0d70fb
commit ee96df22f5
2 changed files with 17 additions and 9 deletions

View File

@ -1,11 +1,14 @@
package net.filebot;
import static java.nio.channels.Channels.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.channels.FileChannel;
import java.util.function.Supplier;
import java.util.logging.Formatter;
import java.util.logging.Handler;
@ -40,6 +43,14 @@ public final class Logging {
public static StreamHandler createSimpleFileHandler(File file, Level level) throws IOException {
StreamHandler handler = new StreamHandler(new FileOutputStream(file, true), new SimpleFormatter());
handler.setEncoding("UTF-8");
handler.setLevel(level);
return handler;
}
public static StreamHandler createLogFileHandler(FileChannel channel, Level level) throws IOException {
StreamHandler handler = new StreamHandler(newOutputStream(channel), new ConsoleFormatter(anonymizePattern.get(), false));
handler.setEncoding("UTF-8");
handler.setLevel(level);
return handler;
}

View File

@ -13,9 +13,7 @@ import static net.filebot.util.ui.SwingUI.*;
import java.awt.Dialog.ModalityType;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.file.StandardOpenOption;
import java.security.CodeSource;
@ -52,7 +50,6 @@ import net.filebot.ui.SinglePanelFrame;
import net.filebot.ui.SupportDialog;
import net.filebot.ui.transfer.FileTransferable;
import net.filebot.util.PreferencesMap.PreferencesEntry;
import net.filebot.util.TeePrintStream;
import net.filebot.util.ui.SwingEventBus;
import net.filebot.win.WinAppUtilities;
import net.miginfocom.swing.MigLayout;
@ -387,9 +384,9 @@ public class Main {
// log errors to file
try {
Handler error = createSimpleFileHandler(ApplicationFolder.AppData.resolve("error.log"), Level.WARNING);
log.addHandler(error);
debug.addHandler(error);
Handler errorLogHandler = createSimpleFileHandler(ApplicationFolder.AppData.resolve("error.log"), Level.WARNING);
log.addHandler(errorLogHandler);
debug.addHandler(errorLogHandler);
} catch (Exception e) {
log.log(Level.WARNING, "Failed to initialize error log", e);
}
@ -416,9 +413,9 @@ public class Main {
}
}
OutputStream out = Channels.newOutputStream(logChannel);
System.setOut(new TeePrintStream(out, true, "UTF-8", System.out));
System.setErr(new TeePrintStream(out, true, "UTF-8", System.err));
Handler logFileHandler = createLogFileHandler(logChannel, Level.ALL);
log.addHandler(logFileHandler);
debug.addHandler(logFileHandler);
}
}