Skip to content

Commit

Permalink
Added -log_dir option.
Browse files Browse the repository at this point in the history
Added some logic to append to an existing log4j configuration (used by the Sparkling Water case).
  • Loading branch information
tomkraljevic committed Jul 28, 2015
1 parent 4f04322 commit ce67710
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 20 deletions.
@@ -0,0 +1,17 @@
package org.apache.log4j;

import org.apache.log4j.spi.LoggerRepository;

import java.util.Properties;

public class H2OPropertyConfigurator extends PropertyConfigurator {
@Override
public
void doConfigure(Properties properties, LoggerRepository hierarchy) {
parseCatsAndRenderers(properties, hierarchy);

// We don't want to hold references to appenders preventing their
// garbage collection.
registry.clear();
}
}
11 changes: 11 additions & 0 deletions h2o-core/src/main/java/water/H2O.java
Expand Up @@ -99,6 +99,10 @@ public static void printHelp() {
" -ice_root <fileSystemPath>\n" +
" The directory where H2O spills temporary data to disk.\n" +
"\n" +
" -log_dir <fileSystemPath>\n" +
" The directory where H2O writes logs to disk.\n" +
" (This usually has a good default that you need not change.)\n" +
"\n" +
" -flow_dir <server side directory or hdfs directory>\n" +
" The directory where H2O stores saved flows.\n" +
defaultFlowDirMessage +
Expand Down Expand Up @@ -191,6 +195,9 @@ public static class OptArgs {
/** -nthreads=nthreads; Max number of F/J threads in the low-priority batch queue */
public int nthreads=Runtime.getRuntime().availableProcessors();

/** -log_dir=/path/to/dir; directory to save logs in */
public String log_dir;

/** -flow_dir=/path/to/dir; directory to save flows in */
public String flow_dir;

Expand Down Expand Up @@ -372,6 +379,10 @@ else if (s.matches("ice_root")) {
i = s.incrementAndCheck(i, args);
ARGS.ice_root = args[i];
}
else if (s.matches("log_dir")) {
i = s.incrementAndCheck(i, args);
ARGS.log_dir = args[i];
}
else if (s.matches("flow_dir")) {
i = s.incrementAndCheck(i, args);
ARGS.flow_dir = args[i];
Expand Down
56 changes: 36 additions & 20 deletions h2o-core/src/main/java/water/util/Log.java
Expand Up @@ -2,6 +2,8 @@

import java.io.File;
import java.util.ArrayList;

import org.apache.log4j.H2OPropertyConfigurator;
import org.apache.log4j.LogManager;
import org.apache.log4j.PropertyConfigurator;
import water.H2O;
Expand Down Expand Up @@ -202,8 +204,8 @@ public static String getLogFileName(String level) throws Exception {
return getLogFileNameStem() + f;
}

private static void setLog4jProperties(String logDirParent, java.util.Properties p) throws Exception {
LOG_DIR = logDirParent + File.separator + "h2ologs";
private static void setLog4jProperties(String logDir, java.util.Properties p) throws Exception {
LOG_DIR = logDir;
String logPathFileName = getLogPathFileNameStem();

// H2O-wide logging
Expand Down Expand Up @@ -288,33 +290,35 @@ private static void setLog4jProperties(String logDirParent, java.util.Properties
private static synchronized org.apache.log4j.Logger createLog4j() {
if( _logger != null ) return _logger; // Test again under lock

// Create some default properties on the fly if we aren't using a provided configuration.
boolean launchedWithHadoopJar = H2O.ARGS.hdfs_skip;
String log4jConfiguration = System.getProperty ("log4j.configuration");
String log4jConfiguration = System.getProperty ("h2o.log4j.configuration");
boolean log4jConfigurationProvided = log4jConfiguration != null;

// Note: for the hadoop case, force H2O to specify the logging setup since we don't care
// about any hadoop log setup, anyway.
if (!launchedWithHadoopJar && H2O.haveInheritedLog4jConfiguration()) {
// Do nothing.
}
else if (!launchedWithHadoopJar && log4jConfigurationProvided) {
if (log4jConfigurationProvided) {
PropertyConfigurator.configure(log4jConfiguration);
}
else {
// Create some default properties on the fly if we aren't using a provided configuration.
// H2O creates the log setup itself on the fly in code.
java.util.Properties p = new java.util.Properties();
try {
File dir;
boolean windowsPath = H2O.ICE_ROOT.toString().matches("^[a-zA-Z]:.*");

// Use ice folder if local, or default
if (windowsPath)
dir = new File(H2O.ICE_ROOT.toString());
else if( H2O.ICE_ROOT.getScheme() == null || PersistManager.Schemes.FILE.equals(H2O.ICE_ROOT.getScheme()) )
dir = new File(H2O.ICE_ROOT.getPath());
else
dir = new File(H2O.DEFAULT_ICE_ROOT());
if (H2O.ARGS.log_dir != null) {
dir = new File(H2O.ARGS.log_dir);
}
else {
boolean windowsPath = H2O.ICE_ROOT.toString().matches("^[a-zA-Z]:.*");

// Use ice folder if local, or default
if (windowsPath)
dir = new File(H2O.ICE_ROOT.toString());
else if (H2O.ICE_ROOT.getScheme() == null || PersistManager.Schemes.FILE.equals(H2O.ICE_ROOT.getScheme()))
dir = new File(H2O.ICE_ROOT.getPath());
else
dir = new File(H2O.DEFAULT_ICE_ROOT());

dir = new File(dir, "h2ologs");
}

setLog4jProperties(dir.toString(), p);
}
Expand All @@ -323,7 +327,19 @@ else if( H2O.ICE_ROOT.getScheme() == null || PersistManager.Schemes.FILE.equals(
e.printStackTrace();
H2O.exit(1);
}
PropertyConfigurator.configure(p);

// For the Hadoop case, force H2O to specify the logging setup since we don't care
// about any hadoop log setup, anyway.
//
// For the Sparkling Water case, we will have inherited the log4j configuration,
// so append to it rather than whack it.
if (!launchedWithHadoopJar && H2O.haveInheritedLog4jConfiguration()) {
// Use a modified log4j property configurator to append rather than create a new log4j configuration.
H2OPropertyConfigurator.configure(p);
}
else {
PropertyConfigurator.configure(p);
}
}

return (_logger = LogManager.getLogger("water.default"));
Expand Down

0 comments on commit ce67710

Please sign in to comment.