|
@@ -18,32 +18,45 @@ import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
|
|
|
import org.apache.logging.log4j.core.config.plugins.PluginElement;
|
|
|
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
|
|
|
import org.apache.logging.log4j.core.layout.PatternLayout;
|
|
|
+import org.apache.logging.log4j.util.Strings;
|
|
|
|
|
|
-@Plugin(name = "HomeDirectoryAwareFile", category = "Core", elementType = "appender", printObject = true)
|
|
|
-public class HomeDirectoryAwareFileAppender extends AbstractOutputStreamAppender<FileManager> {
|
|
|
+/**
|
|
|
+ * A preconfigured FileAppender only relying on a configurable system property, e.g. <code>-DlogPath=/var/log/cryptomator.log</code>.<br/>
|
|
|
+ * Other than the normal {@link org.apache.logging.log4j.core.appender.FileAppender} paths can be resolved relative to the users home directory.
|
|
|
+ */
|
|
|
+@Plugin(name = "ConfigurableFile", category = "Core", elementType = "appender", printObject = true)
|
|
|
+public class ConfigurableFileAppender extends AbstractOutputStreamAppender<FileManager> {
|
|
|
|
|
|
private static final long serialVersionUID = -6548221568069606389L;
|
|
|
private static final int DEFAULT_BUFFER_SIZE = 8192;
|
|
|
+ private static final String DEFAULT_FILE_NAME = "cryptomator.log";
|
|
|
|
|
|
- protected HomeDirectoryAwareFileAppender(String name, Layout<? extends Serializable> layout, Filter filter, FileManager manager) {
|
|
|
+ protected ConfigurableFileAppender(String name, Layout<? extends Serializable> layout, Filter filter, FileManager manager) {
|
|
|
super(name, layout, filter, true, true, manager);
|
|
|
- LOGGER.warn("Logging to " + manager.getFileName());
|
|
|
+ LOGGER.info("Logging to " + manager.getFileName());
|
|
|
}
|
|
|
|
|
|
@PluginFactory
|
|
|
- public static HomeDirectoryAwareFileAppender createAppender(@PluginAttribute("name") final String name, @PluginAttribute("fileName") final String fileName,
|
|
|
+ public static ConfigurableFileAppender createAppender(@PluginAttribute("name") final String name, @PluginAttribute("pathPropertyName") final String pathPropertyName,
|
|
|
@PluginElement("Layout") Layout<? extends Serializable> layout) {
|
|
|
|
|
|
if (name == null) {
|
|
|
- LOGGER.error("No name provided for FileAppender");
|
|
|
+ LOGGER.error("No name provided for HomeDirectoryAwareFileAppender");
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- final Path filePath;
|
|
|
- if (fileName == null) {
|
|
|
- LOGGER.error("No filename provided for FileAppender with name " + name);
|
|
|
+ if (pathPropertyName == null) {
|
|
|
+ LOGGER.error("No pathPropertyName provided for HomeDirectoryAwareFileAppender with name " + name);
|
|
|
return null;
|
|
|
- } else if (fileName.startsWith("~/")) {
|
|
|
+ }
|
|
|
+
|
|
|
+ String fileName = System.getProperty(pathPropertyName);
|
|
|
+ if (Strings.isEmpty(fileName)) {
|
|
|
+ fileName = DEFAULT_FILE_NAME;
|
|
|
+ }
|
|
|
+
|
|
|
+ final Path filePath;
|
|
|
+ if (fileName.startsWith("~/")) {
|
|
|
// home-dir-relative Path:
|
|
|
final Path userHome = FileSystems.getDefault().getPath(SystemUtils.USER_HOME);
|
|
|
filePath = userHome.resolve(fileName.substring(2));
|
|
@@ -53,7 +66,7 @@ public class HomeDirectoryAwareFileAppender extends AbstractOutputStreamAppender
|
|
|
} else {
|
|
|
// relative Path:
|
|
|
try {
|
|
|
- final URI jarFileLocation = HomeDirectoryAwareFileAppender.class.getProtectionDomain().getCodeSource().getLocation().toURI();
|
|
|
+ final URI jarFileLocation = ConfigurableFileAppender.class.getProtectionDomain().getCodeSource().getLocation().toURI();
|
|
|
final Path workingDir = FileSystems.getDefault().getPath(jarFileLocation.getPath()).getParent();
|
|
|
filePath = workingDir.resolve(fileName);
|
|
|
} catch (URISyntaxException e) {
|
|
@@ -76,7 +89,7 @@ public class HomeDirectoryAwareFileAppender extends AbstractOutputStreamAppender
|
|
|
}
|
|
|
|
|
|
final FileManager manager = FileManager.getFileManager(filePath.toString(), false, false, true, null, layout, DEFAULT_BUFFER_SIZE);
|
|
|
- return new HomeDirectoryAwareFileAppender(name, layout, null, manager);
|
|
|
+ return new ConfigurableFileAppender(name, layout, null, manager);
|
|
|
}
|
|
|
|
|
|
}
|