|
@@ -19,6 +19,7 @@ import java.util.regex.Pattern;
|
|
|
import org.apache.commons.lang3.SystemUtils;
|
|
|
import org.apache.logging.log4j.core.Filter;
|
|
|
import org.apache.logging.log4j.core.Layout;
|
|
|
+import org.apache.logging.log4j.core.appender.AbstractAppender;
|
|
|
import org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender;
|
|
|
import org.apache.logging.log4j.core.appender.FileManager;
|
|
|
import org.apache.logging.log4j.core.config.plugins.Plugin;
|
|
@@ -26,10 +27,11 @@ 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.core.util.Booleans;
|
|
|
import org.apache.logging.log4j.util.Strings;
|
|
|
|
|
|
/**
|
|
|
- * A preconfigured FileAppender only relying on a configurable system property, e.g. <code>-DlogPath=/var/log/cryptomator.log</code>.<br/>
|
|
|
+ * A preconfigured FileAppender only relying on a configurable system property, e.g. <code>-Dcryptomator.logPath=/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)
|
|
@@ -37,7 +39,6 @@ public class ConfigurableFileAppender extends AbstractOutputStreamAppender<FileM
|
|
|
|
|
|
private static final long serialVersionUID = -6548221568069606389L;
|
|
|
private static final int DEFAULT_BUFFER_SIZE = 8192;
|
|
|
- private static final String DEFAULT_FILE_NAME = "cryptomator.log";
|
|
|
private static final Pattern DRIVE_LETTER_WITH_PRECEEDING_SLASH = Pattern.compile("^/[A-Z]:", Pattern.CASE_INSENSITIVE);
|
|
|
|
|
|
protected ConfigurableFileAppender(String name, Layout<? extends Serializable> layout, Filter filter, FileManager manager) {
|
|
@@ -46,9 +47,8 @@ public class ConfigurableFileAppender extends AbstractOutputStreamAppender<FileM
|
|
|
}
|
|
|
|
|
|
@PluginFactory
|
|
|
- public static ConfigurableFileAppender createAppender(@PluginAttribute("name") final String name, @PluginAttribute("pathPropertyName") final String pathPropertyName,
|
|
|
+ public static AbstractAppender createAppender(@PluginAttribute("name") final String name, @PluginAttribute("pathPropertyName") final String pathPropertyName, @PluginAttribute("append") final String append,
|
|
|
@PluginElement("Layout") Layout<? extends Serializable> layout) {
|
|
|
-
|
|
|
if (name == null) {
|
|
|
LOGGER.error("No name provided for HomeDirectoryAwareFileAppender");
|
|
|
return null;
|
|
@@ -59,23 +59,48 @@ public class ConfigurableFileAppender extends AbstractOutputStreamAppender<FileM
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- String fileName = System.getProperty(pathPropertyName);
|
|
|
+ final String fileName = System.getProperty(pathPropertyName);
|
|
|
if (Strings.isEmpty(fileName)) {
|
|
|
- fileName = DEFAULT_FILE_NAME;
|
|
|
+ LOGGER.warn("No log file location provided in system property \"" + pathPropertyName + "\"");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ final Path filePath = parsePath(fileName);
|
|
|
+ if (filePath == null) {
|
|
|
+ LOGGER.warn("Invalid path \"" + fileName + "\"");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!Files.exists(filePath.getParent())) {
|
|
|
+ try {
|
|
|
+ Files.createDirectories(filePath.getParent());
|
|
|
+ } catch (IOException e) {
|
|
|
+ LOGGER.error("Could not create parent directories for log file located at " + filePath.toString(), e);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ final boolean shouldAppend = Booleans.parseBoolean(append, true);
|
|
|
+ if (layout == null) {
|
|
|
+ layout = PatternLayout.createDefaultLayout();
|
|
|
}
|
|
|
|
|
|
- final Path filePath;
|
|
|
- if (fileName.startsWith("~/")) {
|
|
|
+ final FileManager manager = FileManager.getFileManager(filePath.toString(), shouldAppend, false, true, null, layout, DEFAULT_BUFFER_SIZE);
|
|
|
+ return new ConfigurableFileAppender(name, layout, null, manager);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Path parsePath(String path) {
|
|
|
+ if (path.startsWith("~/")) {
|
|
|
// home-dir-relative Path:
|
|
|
final Path userHome = FileSystems.getDefault().getPath(SystemUtils.USER_HOME);
|
|
|
- filePath = userHome.resolve(fileName.substring(2));
|
|
|
- } else if (fileName.startsWith("/")) {
|
|
|
+ return userHome.resolve(path.substring(2));
|
|
|
+ } else if (path.startsWith("/")) {
|
|
|
// absolute Path:
|
|
|
- filePath = FileSystems.getDefault().getPath(fileName);
|
|
|
- } else if (SystemUtils.IS_OS_WINDOWS && fileName.startsWith("%appdata%/")) {
|
|
|
+ return FileSystems.getDefault().getPath(path);
|
|
|
+ } else if (SystemUtils.IS_OS_WINDOWS && path.startsWith("%appdata%/")) {
|
|
|
final String appdata = System.getenv("APPDATA");
|
|
|
final Path appdataPath = appdata != null ? FileSystems.getDefault().getPath(appdata) : FileSystems.getDefault().getPath(SystemUtils.USER_HOME);
|
|
|
- filePath = appdataPath.resolve(fileName.substring(10));
|
|
|
+ return appdataPath.resolve(path.substring(10));
|
|
|
} else {
|
|
|
// relative Path:
|
|
|
try {
|
|
@@ -85,28 +110,12 @@ public class ConfigurableFileAppender extends AbstractOutputStreamAppender<FileM
|
|
|
jarFileLocation = jarFileLocation.substring(1);
|
|
|
}
|
|
|
final Path workingDir = FileSystems.getDefault().getPath(jarFileLocation).getParent();
|
|
|
- filePath = workingDir.resolve(fileName);
|
|
|
+ return workingDir.resolve(path);
|
|
|
} catch (URISyntaxException e) {
|
|
|
LOGGER.error("Unable to resolve working directory ", e);
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- if (layout == null) {
|
|
|
- layout = PatternLayout.createDefaultLayout();
|
|
|
- }
|
|
|
-
|
|
|
- if (!Files.exists(filePath.getParent())) {
|
|
|
- try {
|
|
|
- Files.createDirectories(filePath.getParent());
|
|
|
- } catch (IOException e) {
|
|
|
- LOGGER.error("Could not create parent directories for log file located at " + filePath.toString(), e);
|
|
|
- return null;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- final FileManager manager = FileManager.getFileManager(filePath.toString(), false, false, true, null, layout, DEFAULT_BUFFER_SIZE);
|
|
|
- return new ConfigurableFileAppender(name, layout, null, manager);
|
|
|
}
|
|
|
|
|
|
}
|