Skip to content

Commit

Permalink
added a default logging location, if logPath property is not set.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Stenzel committed May 24, 2015
1 parent 3d3cb7b commit 587c45e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
Expand Up @@ -18,32 +18,45 @@
import org.apache.logging.log4j.core.config.plugins.PluginElement; import org.apache.logging.log4j.core.config.plugins.PluginElement;
import org.apache.logging.log4j.core.config.plugins.PluginFactory; import org.apache.logging.log4j.core.config.plugins.PluginFactory;
import org.apache.logging.log4j.core.layout.PatternLayout; 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 long serialVersionUID = -6548221568069606389L;
private static final int DEFAULT_BUFFER_SIZE = 8192; 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); super(name, layout, filter, true, true, manager);
LOGGER.warn("Logging to " + manager.getFileName()); LOGGER.info("Logging to " + manager.getFileName());
} }


@PluginFactory @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) { @PluginElement("Layout") Layout<? extends Serializable> layout) {


if (name == null) { if (name == null) {
LOGGER.error("No name provided for FileAppender"); LOGGER.error("No name provided for HomeDirectoryAwareFileAppender");
return null; return null;
} }


final Path filePath; if (pathPropertyName == null) {
if (fileName == null) { LOGGER.error("No pathPropertyName provided for HomeDirectoryAwareFileAppender with name " + name);
LOGGER.error("No filename provided for FileAppender with name " + name);
return null; 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: // home-dir-relative Path:
final Path userHome = FileSystems.getDefault().getPath(SystemUtils.USER_HOME); final Path userHome = FileSystems.getDefault().getPath(SystemUtils.USER_HOME);
filePath = userHome.resolve(fileName.substring(2)); filePath = userHome.resolve(fileName.substring(2));
Expand All @@ -53,7 +66,7 @@ public static HomeDirectoryAwareFileAppender createAppender(@PluginAttribute("na
} else { } else {
// relative Path: // relative Path:
try { 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(); final Path workingDir = FileSystems.getDefault().getPath(jarFileLocation.getPath()).getParent();
filePath = workingDir.resolve(fileName); filePath = workingDir.resolve(fileName);
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
Expand All @@ -76,7 +89,7 @@ public static HomeDirectoryAwareFileAppender createAppender(@PluginAttribute("na
} }


final FileManager manager = FileManager.getFileManager(filePath.toString(), false, false, true, null, layout, DEFAULT_BUFFER_SIZE); 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);
} }


} }
5 changes: 3 additions & 2 deletions main/ui/src/main/resources/log4j2.xml
Expand Up @@ -18,9 +18,10 @@
<PatternLayout pattern="%16d %-5p [%c{1}:%L] %m%n" /> <PatternLayout pattern="%16d %-5p [%c{1}:%L] %m%n" />
<ThresholdFilter level="WARN" onMatch="ACCEPT" onMismatch="DENY" /> <ThresholdFilter level="WARN" onMatch="ACCEPT" onMismatch="DENY" />
</Console> </Console>
<HomeDirectoryAwareFile name="File" fileName="${sys:logPath}"> <!-- <File fileName="${sys:logPath}" ...> not feasible for paths like ~/foo/bar -->
<ConfigurableFile name="File" pathPropertyName="logPath">
<PatternLayout pattern="%16d %-5p [%c{1}:%L] %m%n" /> <PatternLayout pattern="%16d %-5p [%c{1}:%L] %m%n" />
</HomeDirectoryAwareFile> </ConfigurableFile>
</Appenders> </Appenders>


<Loggers> <Loggers>
Expand Down

0 comments on commit 587c45e

Please sign in to comment.