]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - org.argeo.slc.agent/src/main/java/org/argeo/slc/cli/SlcMain.java
Improve logging
[gpl/argeo-slc.git] / org.argeo.slc.agent / src / main / java / org / argeo / slc / cli / SlcMain.java
index 6184c5a7630a27b0b6dc1cca3099d92a5915ae48..fd6a402093ff21cd411996b79a464a97089a2278 100644 (file)
@@ -11,7 +11,10 @@ import org.apache.commons.cli.Option;
 import org.apache.commons.cli.OptionBuilder;
 import org.apache.commons.cli.Options;
 import org.apache.commons.cli.ParseException;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.argeo.slc.core.SlcException;
+import org.argeo.slc.logging.Log4jUtils;
 import org.springframework.core.io.FileSystemResource;
 
 public class SlcMain {
@@ -19,8 +22,9 @@ public class SlcMain {
                single, agent
        }
 
-       public final static String MODE_SINGLE = "single";
-       public final static String MODE_AGENT = "agent";
+       private static Log log = null;
+
+       private final static String BOOTSTRAP_LOG4J_CONFIG = "org/argeo/slc/cli/bootstrapLog4j.properties";
 
        private final static Option modeOpt = OptionBuilder.withLongOpt("mode")
                        .withArgName("mode").hasArg().isRequired().withDescription(
@@ -53,6 +57,7 @@ public class SlcMain {
                File script = null;
 
                try {
+
                        CommandLineParser clParser = new GnuParser();
                        CommandLine cl = clParser.parse(options, args);
 
@@ -63,7 +68,6 @@ public class SlcMain {
                        } catch (IllegalArgumentException e) {
                                throw new SlcException("Unrecognized mode '" + modeStr + "'", e);
                        }
-                       System.out.println("Mode: " + mode);
 
                        // Script
                        if (mode.equals(Mode.single)) {
@@ -73,7 +77,6 @@ public class SlcMain {
                                                        + "'");
                                script = (File) cl.getOptionObject(scriptOpt.getOpt());
                        }
-                       System.out.println("Script: " + script.getAbsolutePath());
 
                        // Properties
                        if (cl.hasOption(propertyOpt.getOpt())) {
@@ -81,16 +84,30 @@ public class SlcMain {
                                        addProperty(properties, property);
                                }
                        }
-                       System.out.print("Properties: " + properties);
+
                } catch (ParseException e) {
                        System.err.println("Problem with command line arguments. "
                                        + e.getMessage());
-                       printUsage();
+                       badExit();
                } catch (SlcException e) {
                        System.err.println(e.getMessage());
-                       printUsage();
+                       badExit();
+               } catch (Exception e) {
+                       System.err.println("Unexpected exception when bootstrapping.");
+                       e.printStackTrace();
+                       badExit();
                }
 
+               // Initializes logging and log arguments
+               initLogging(properties);
+               if (log.isDebugEnabled()) {
+                       log.debug("Mode: " + mode);
+                       log.debug("User properties: " + properties);
+                       if (script != null)
+                               log.debug("Script: " + script.getAbsolutePath());
+               }
+
+               // Execution
                if (mode.equals(Mode.single)) {
                        DefaultSlcRuntime runtime = new DefaultSlcRuntime();
                        runtime.executeScript(new FileSystemResource(script), properties,
@@ -127,4 +144,24 @@ public class SlcMain {
                }
 
        }
+
+       private static void initLogging(Properties userProperties) {
+               System.setProperty("log4j.defaultInitOverride", "true");
+
+               // Add log4j user properties to System properties
+               for (String key : userProperties.stringPropertyNames()) {
+                       if (key.startsWith("log4j.")) {
+                               System.setProperty(key, userProperties.getProperty(key));
+                       }
+               }
+               Log4jUtils.initLog4j(System.getProperty("log4j.configuration",
+                               "classpath:" + BOOTSTRAP_LOG4J_CONFIG));
+               log = LogFactory.getLog(SlcMain.class);
+
+       }
+
+       private static void badExit() {
+               printUsage();
+               System.exit(1);
+       }
 }