Refactor logging

This commit is contained in:
Reinhard Pointner 2016-03-10 14:05:56 +00:00
parent 558ccee1f3
commit ffa98e1989
4 changed files with 58 additions and 54 deletions

View File

@ -79,15 +79,15 @@ public class Main {
if (args.clearCache() || args.clearUserData()) { if (args.clearCache() || args.clearUserData()) {
if (args.clearUserData()) { if (args.clearUserData()) {
log.info("Reset preferences"); System.out.println("Reset preferences");
Settings.forPackage(Main.class).clear(); Settings.forPackage(Main.class).clear();
} }
// clear preferences and cache // clear preferences and cache
if (args.clearCache()) { if (args.clearCache()) {
log.info("Clear cache and temporary files"); System.out.println("Clear cache and temporary files");
for (File folder : getChildren(getApplicationFolder().getCanonicalFile(), FOLDERS)) { for (File folder : getChildren(getApplicationFolder().getCanonicalFile(), FOLDERS)) {
log.info("* Delete " + folder); System.out.println("* Delete " + folder);
delete(folder); delete(folder);
} }
CacheManager.getInstance().clearAll(); CacheManager.getInstance().clearAll();
@ -99,36 +99,7 @@ public class Main {
// update system properties // update system properties
initializeSystemProperties(args); initializeSystemProperties(args);
initializeLogging(args);
// update logging level
log.setLevel(args.getLogLevel());
if (debug.getLevel().intValue() < log.getLevel().intValue()) {
debug.setLevel(log.getLevel());
}
// tee stdout and stderr to log file if set
if (args.logFile != null) {
File logFile = new File(args.logFile);
if (!logFile.isAbsolute()) {
logFile = new File(new File(getApplicationFolder(), "logs"), logFile.getPath()).getAbsoluteFile(); // by default resolve relative paths against {applicationFolder}/logs/{logFile}
}
if (!logFile.exists() && !logFile.getParentFile().mkdirs() && !logFile.createNewFile()) {
throw new IOException("Failed to create log file: " + logFile);
}
// open file channel and lock
FileChannel logChannel = FileChannel.open(logFile.toPath(), StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.APPEND);
if (args.logLock) {
if (args.getLogLevel() == Level.ALL) {
log.config("Locking " + logFile);
}
logChannel.lock();
}
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));
}
// make sure java.io.tmpdir exists // make sure java.io.tmpdir exists
createFolders(getApplicationTempFolder()); createFolders(getApplicationTempFolder());
@ -184,16 +155,8 @@ public class Main {
} }
private static void startUserInterface(ArgumentBean args) { private static void startUserInterface(ArgumentBean args) {
try {
// GUI logging settings
log.addHandler(new NotificationHandler(getApplicationName()));
// log errors to file
Handler error = createSimpleFileHandler(new File(getApplicationFolder(), "error.log"), Level.WARNING);
log.addHandler(error);
debug.addHandler(error);
// use native LaF an all platforms // use native LaF an all platforms
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) { } catch (Exception e) {
debug.log(Level.SEVERE, e.getMessage(), e); debug.log(Level.SEVERE, e.getMessage(), e);
@ -452,4 +415,53 @@ public class Main {
System.setProperty("application.rename.history", Boolean.toString(!args.action.equalsIgnoreCase("test"))); // do not keep history of --action test rename operations System.setProperty("application.rename.history", Boolean.toString(!args.action.equalsIgnoreCase("test"))); // do not keep history of --action test rename operations
} }
public static void initializeLogging(ArgumentBean args) throws IOException {
// tee stdout and stderr to log file if set
if (args.logFile != null) {
File logFile = new File(args.logFile);
if (!logFile.isAbsolute()) {
logFile = new File(new File(getApplicationFolder(), "logs"), logFile.getPath()).getAbsoluteFile(); // by default resolve relative paths against {applicationFolder}/logs/{logFile}
}
if (!logFile.exists() && !logFile.getParentFile().mkdirs() && !logFile.createNewFile()) {
throw new IOException("Failed to create log file: " + logFile);
}
// open file channel and lock
FileChannel logChannel = FileChannel.open(logFile.toPath(), StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.APPEND);
if (args.logLock) {
if (args.getLogLevel() == Level.ALL) {
debug.config("Locking " + logFile);
}
logChannel.lock();
}
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));
}
if (args.runCLI()) {
// CLI logging settings
log.setLevel(args.getLogLevel());
// set debug log level standard log level if lower
if (debug.getLevel().intValue() < log.getLevel().intValue()) {
debug.setLevel(log.getLevel());
}
} else {
// GUI logging settings
log.setLevel(Level.INFO);
log.addHandler(new NotificationHandler(getApplicationName()));
// log errors to file
try {
Handler error = createSimpleFileHandler(new File(getApplicationFolder(), "error.log"), Level.WARNING);
log.addHandler(error);
debug.addHandler(error);
} catch (Exception e) {
debug.log(Level.WARNING, "Failed to initialize error log", e);
}
}
}
} }

View File

@ -240,9 +240,9 @@ public class DropToUnlock extends JList<File> {
try { try {
String owner = Files.getOwner(f.toPath()).getName(); String owner = Files.getOwner(f.toPath()).getName();
String permissions = PosixFilePermissions.toString(Files.getPosixFilePermissions(f.toPath())); String permissions = PosixFilePermissions.toString(Files.getPosixFilePermissions(f.toPath()));
log.severe(String.format("Permission denied: %s (%s %s)", f, permissions, owner)); log.severe(format("Permission denied: %s (%s %s)", f, permissions, owner));
} catch (Exception e) { } catch (Exception e) {
log.severe(String.format("Permission denied: %s", f)); log.severe(format("Permission denied: %s", f));
} }
}); });
} }

View File

@ -262,10 +262,10 @@ class BindingDialog extends JDialog {
// check episode and media file // check episode and media file
if (getInfoObject() == null) { if (getInfoObject() == null) {
// illegal episode string // illegal episode string
log.warning(String.format("Failed to parse episode: '%s'", infoTextField.getText())); log.warning(format("Failed to parse episode: '%s'", infoTextField.getText()));
} else if (getMediaFile() == null && !mediaFileTextField.getText().isEmpty()) { } else if (getMediaFile() == null && !mediaFileTextField.getText().isEmpty()) {
// illegal file path // illegal file path
log.warning(String.format("Invalid media file: '%s'", mediaFileTextField.getText())); log.warning(format("Invalid media file: '%s'", mediaFileTextField.getText()));
} else { } else {
// everything seems to be in order // everything seems to be in order
finish(true); finish(true);

View File

@ -1,52 +1,44 @@
package net.filebot.util; package net.filebot.util;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.PrintStream; import java.io.PrintStream;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
public class TeePrintStream extends PrintStream { public class TeePrintStream extends PrintStream {
private final PrintStream cc; private final PrintStream cc;
public TeePrintStream(OutputStream out, boolean autoFlush, String encoding, PrintStream cc) throws UnsupportedEncodingException { public TeePrintStream(OutputStream out, boolean autoFlush, String encoding, PrintStream cc) throws UnsupportedEncodingException {
super(out, autoFlush, encoding); super(out, autoFlush, encoding);
this.cc = cc; this.cc = cc;
} }
@Override @Override
public void close() { public void close() {
super.close(); super.close();
cc.close(); cc.close();
} }
@Override @Override
public void flush() { public void flush() {
super.flush(); super.flush();
cc.flush(); cc.flush();
} }
@Override @Override
public void write(byte[] buf, int off, int len) { public void write(byte[] buf, int off, int len) {
super.write(buf, off, len); super.write(buf, off, len);
cc.write(buf, off, len); cc.write(buf, off, len);
} }
@Override @Override
public void write(int b) { public void write(int b) {
super.write(b); super.write(b);
cc.write(b); cc.write(b);
} }
@Override @Override
public void write(byte[] b) throws IOException { public void write(byte[] b) throws IOException {
super.write(b); super.write(b);