diff --git a/pom.xml b/pom.xml index bc5bf8014..da8edf6aa 100644 --- a/pom.xml +++ b/pom.xml @@ -398,5 +398,11 @@ along with DependencyCheck. If not, see . + diff --git a/src/main/java/org/codesecure/dependencycheck/App.java b/src/main/java/org/codesecure/dependencycheck/App.java index 8022ee824..165be83be 100644 --- a/src/main/java/org/codesecure/dependencycheck/App.java +++ b/src/main/java/org/codesecure/dependencycheck/App.java @@ -18,11 +18,16 @@ package org.codesecure.dependencycheck; * Copyright (c) 2012 Jeremy Long. All Rights Reserved. */ +import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; +import java.io.InputStream; import java.util.List; +import java.util.Properties; import java.util.logging.Level; +import java.util.logging.LogManager; import java.util.logging.Logger; +import java.util.prefs.Preferences; import javax.xml.parsers.ParserConfigurationException; import org.apache.commons.cli.ParseException; import org.codesecure.dependencycheck.data.cpe.Index; @@ -57,29 +62,55 @@ import org.xml.sax.SAXException; */ public class App { + private static final String LOG_PROPERTIES_FILE = "configuration/log.properties"; /** * @param args the command line arguments */ public static void main(String[] args) { - //Preferences.systemRoot().put("java.util.logging.config.file", "log.properties"); + prepareLogger(); App app = new App(); app.run(args); } + private static void prepareLogger() { + //while java doc for JUL says to use preferences api - it throws an exception... + //Preferences.systemRoot().put("java.util.logging.config.file", "log.properties"); + //System.getProperties().put("java.util.logging.config.file", "configuration/log.properties"); + File dir = new File("logs"); + if (!dir.exists()) { + dir.mkdir(); + } + try { + InputStream in = App.class.getClassLoader().getResourceAsStream(LOG_PROPERTIES_FILE); + LogManager.getLogManager().reset(); + LogManager.getLogManager().readConfiguration(in); + } catch (IOException ex) { + System.err.println(ex.toString()); + Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex); + } catch (SecurityException ex) { + Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex); + } + } + /** * main CLI entry-point into the application. * * @param args the command line arguments */ public void run(String[] args) { + CliParser cli = new CliParser(); try { cli.parse(args); } catch (FileNotFoundException ex) { - Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex); + System.err.println(ex.getMessage()); + cli.printHelp(); + Logger.getLogger(App.class.getName()).log(Level.WARNING, null, ex); return; } catch (ParseException ex) { - Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex); + System.err.println(ex.getMessage()); + cli.printHelp(); + Logger.getLogger(App.class.getName()).log(Level.INFO, null, ex); return; } diff --git a/src/main/java/org/codesecure/dependencycheck/utils/Settings.java b/src/main/java/org/codesecure/dependencycheck/utils/Settings.java index 2c706fb06..6817dbc7d 100644 --- a/src/main/java/org/codesecure/dependencycheck/utils/Settings.java +++ b/src/main/java/org/codesecure/dependencycheck/utils/Settings.java @@ -67,7 +67,7 @@ public class Settings { */ public static final String CONNECTION_TIMEOUT = "connection.timeout"; } - private static final String PROPERTIES_FILE = "META-INF/dependencycheck.properties"; + private static final String PROPERTIES_FILE = "configuration/dependencycheck.properties"; private static final Settings INSTANCE = new Settings(); private Properties props = null; diff --git a/src/main/resources/META-INF/dependencycheck.properties b/src/main/resources/configuration/dependencycheck.properties similarity index 100% rename from src/main/resources/META-INF/dependencycheck.properties rename to src/main/resources/configuration/dependencycheck.properties diff --git a/src/main/resources/configuration/log.properties b/src/main/resources/configuration/log.properties new file mode 100644 index 000000000..158c15ef8 --- /dev/null +++ b/src/main/resources/configuration/log.properties @@ -0,0 +1,21 @@ +handlers=java.util.logging.ConsoleHandler, java.util.logging.FileHandler + +# logging levels +# FINEST, FINER, FINE, CONFIG, INFO, WARNING and SEVERE. + +# Configure the ConsoleHandler. +java.util.logging.ConsoleHandler.level=SEVERE + +# Configure the FileHandler. +java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter +java.util.logging.FileHandler.level=FINEST + +# The following special tokens can be used in the pattern property +# which specifies the location and name of the log file. +# / - standard path separator +# %t - system temporary directory +# %h - value of the user.home system property +# %g - generation number for rotating logs +# %u - unique number to avoid conflicts +# FileHandler writes to %h/demo0.log by default. +java.util.logging.FileHandler.pattern=./logs/DependencyCheck%g.log \ No newline at end of file diff --git a/src/test/resources/log.properties b/src/test/resources/log.properties new file mode 100644 index 000000000..158c15ef8 --- /dev/null +++ b/src/test/resources/log.properties @@ -0,0 +1,21 @@ +handlers=java.util.logging.ConsoleHandler, java.util.logging.FileHandler + +# logging levels +# FINEST, FINER, FINE, CONFIG, INFO, WARNING and SEVERE. + +# Configure the ConsoleHandler. +java.util.logging.ConsoleHandler.level=SEVERE + +# Configure the FileHandler. +java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter +java.util.logging.FileHandler.level=FINEST + +# The following special tokens can be used in the pattern property +# which specifies the location and name of the log file. +# / - standard path separator +# %t - system temporary directory +# %h - value of the user.home system property +# %g - generation number for rotating logs +# %u - unique number to avoid conflicts +# FileHandler writes to %h/demo0.log by default. +java.util.logging.FileHandler.pattern=./logs/DependencyCheck%g.log \ No newline at end of file