Skip to content

Commit

Permalink
#9 ability to configure logging directory
Browse files Browse the repository at this point in the history
* dedicated log4j file appender that is aware of application running directory
* new -l or -logDir option
* updated lo4j sample configuration
  • Loading branch information
tomasz-kucharski committed Jan 29, 2012
1 parent e76adfb commit 1dbb8ef
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,23 @@ public static void main(String args[]) throws Exception {

private void parseArguments(String[] args) {
Option configOption = new Option("c", "config", true, "Location of configuration directory, eg. " + ConfigFileLocation.getConfigurationDirectory());
Option logOption = new Option("l", "logDir", true, "Location of log directory, eg. " + ConfigFileLocation.getLogDirectory());

Options options = new Options();
options.addOption(configOption);
options.addOption(logOption);

try {
CommandLine parse = new GnuParser().parse(options, args);
if (parse.hasOption(configOption.getOpt())) {
ConfigFileLocation.setConfigurationDirectory("file:/"+parse.getOptionValue(configOption.getOpt()));
ConfigFileLocation.setConfigurationDirectory("file:/"+parse.getOptionValue(configOption.getOpt())+"/");
}
if (parse.hasOption(logOption.getOpt())) {
ConfigFileLocation.setLogDirectory(parse.getOptionValue(logOption.getOpt())+"/");
}
} catch (ParseException e) {
HelpFormatter usageFormatter = new HelpFormatter();
usageFormatter.setWidth(120);
usageFormatter.printHelp("usage", options);
System.exit(0);
}
Expand All @@ -50,6 +56,7 @@ private void startServer() throws MalformedURLException {
DOMConfigurator.configure(new URL(ConfigFileLocation.getLoggingConfigurationFile()));
log.info("Starting JHouse server...");
log.info("\tInstallation directory : " + ConfigFileLocation.getInstallationDirectory());
log.info("\tLogging directory : " + ConfigFileLocation.getLogDirectory());
log.info("\tApplication file : " + ConfigFileLocation.getApplicationFileLocation());
log.info("\tLogging configuration file : " + ConfigFileLocation.getLoggingConfigurationFile());
log.info("\tConfiguration properties file : " + ConfigFileLocation.getConfigurationPropertiesFile());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package eu.jhouse.server.runner;

import org.apache.log4j.RollingFileAppender;

/**
* @author tkucharski tomasz.kucharski@javart.eu
* @since 29.01.12, 21:36
*/
public class ApplicationContextLog4jFileRollingAppender extends RollingFileAppender {

@Override
public void setFile(String file) {
super.setFile(ConfigFileLocation.getLogDirectory()+file);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class ConfigFileLocation {
private static String installationDirectory = getApplicationFileLocation().substring(0, getApplicationFileLocation().lastIndexOf("/"));

private static String configurationDirectory = getInstallationDirectory() + "/conf/";

private static String logDirectory = getInstallationDirectory().substring(5) + "/logs/";

public static String getApplicationFileLocation() {
if (applicationFileLocation == null) {
Expand Down Expand Up @@ -43,4 +45,12 @@ public static String getConfigurationPropertiesFile() {
public static String getLoggingConfigurationFile() {
return getConfigurationDirectory() + "log4j.xml";
}

public static String getLogDirectory() {
return logDirectory;
}

public static void setLogDirectory(String logDirectory) {
ConfigFileLocation.logDirectory = logDirectory;
}
}
2 changes: 1 addition & 1 deletion app/jhouse-server-dist/src/main/resources/conf/log4j.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<param name="ConversionPattern" value="%d %-5p [%t] [%C{1}] %m%n"/>
</layout>
</appender>
<appender name="FILE" class="org.apache.log4j.RollingFileAppender">
<appender name="FILE" class="eu.jhouse.server.runner.ApplicationContextLog4jFileRollingAppender">
<param name="File" value="log.log"/>
<param name="MaxFileSize" value="10MB"/>
<param name="Threshold" value="DEBUG"/>
Expand Down

0 comments on commit 1dbb8ef

Please sign in to comment.