diff --git a/dependency-check-core/src/main/java/org/owasp/dependencycheck/utils/LogFilter.java b/dependency-check-core/src/main/java/org/owasp/dependencycheck/utils/LogFilter.java new file mode 100644 index 000000000..6dfa0dc77 --- /dev/null +++ b/dependency-check-core/src/main/java/org/owasp/dependencycheck/utils/LogFilter.java @@ -0,0 +1,44 @@ +/* + * This file is part of dependency-check-core. + * + * Dependency-check-core is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) any + * later version. + * + * Dependency-check-core is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * dependency-check-core. If not, see http://www.gnu.org/licenses/. + * + * Copyright (c) 2013 Jeremy Long. All Rights Reserved. + */ +package org.owasp.dependencycheck.utils; + +import java.util.logging.Filter; +import java.util.logging.LogRecord; + +/** + * A simple log filter to limit the entries written to the verbose log file. The + * verbose log file uses the root logger as I couldn't get anything else to + * work; as such, this filter limits the log entries to specific classes. + * + * @author Jeremy Long (jeremy.long@owasp.org) + */ +public class LogFilter implements Filter { + + /** + * Determines if the record should be logged. + * + * @param record a log record to examine + * @return true if the record should be logged, otherwise false + */ + @Override + public boolean isLoggable(LogRecord record) { + final String name = record.getSourceClassName(); + return name.startsWith("org.owasp.dependencycheck") && !name.contains("generated") && !name.contains("VelocityLoggerRedirect"); + } +} diff --git a/dependency-check-core/src/main/java/org/owasp/dependencycheck/utils/LogUtils.java b/dependency-check-core/src/main/java/org/owasp/dependencycheck/utils/LogUtils.java new file mode 100644 index 000000000..9bca01f33 --- /dev/null +++ b/dependency-check-core/src/main/java/org/owasp/dependencycheck/utils/LogUtils.java @@ -0,0 +1,75 @@ +/* + * This file is part of dependency-check-core. + * + * Dependency-check-core is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) any + * later version. + * + * Dependency-check-core is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * dependency-check-core. If not, see http://www.gnu.org/licenses/. + * + * Copyright (c) 2013 Jeremy Long. All Rights Reserved. + */ +package org.owasp.dependencycheck.utils; + +import java.io.IOException; +import java.io.InputStream; +import java.util.logging.FileHandler; +import java.util.logging.Level; +import java.util.logging.LogManager; +import java.util.logging.Logger; +import java.util.logging.SimpleFormatter; + +/** + * A utility class to aide in the setup of the logging mechanism. + * + * @author Jeremy Long (jeremy.long@owasp.org) + */ +public final class LogUtils { + + /** + * Private constructor for a utility class. + */ + private LogUtils() { + } + + /** + * Configures the logger for use by the application. + * + * @param in the input stream to read the log settings from + * @param verboseLogFile the file path for the verbose log + */ + public static void prepareLogger(InputStream in, String verboseLogFile) { + try { + LogManager.getLogManager().reset(); + LogManager.getLogManager().readConfiguration(in); + if (verboseLogFile != null && !verboseLogFile.isEmpty()) { + final Logger logger = Logger.getLogger(""); + final FileHandler handler = new FileHandler(verboseLogFile, true); + handler.setFormatter(new SimpleFormatter()); + handler.setLevel(Level.FINE); + handler.setFilter(new LogFilter()); + logger.addHandler(handler); + logger.setLevel(Level.FINE); + } + } catch (IOException ex) { + Logger.getLogger(LogUtils.class.getName()).log(Level.FINE, "IO Error preparing the logger", ex); + } catch (SecurityException ex) { + Logger.getLogger(LogUtils.class.getName()).log(Level.FINE, "Error preparing the logger", ex); + } finally { + if (in != null) { + try { + in.close(); + } catch (Exception ex) { + Logger.getLogger(LogUtils.class.getName()).log(Level.FINEST, "Error closing resource stream", ex); + } + } + } + } +}