View Javadoc

    1   /*
    2    * This file is part of dependency-check-core.
    3    *
    4    * Dependency-check-core is free software: you can redistribute it and/or modify it
    5    * under the terms of the GNU General Public License as published by the Free
    6    * Software Foundation, either version 3 of the License, or (at your option) any
    7    * later version.
    8    *
    9    * Dependency-check-core is distributed in the hope that it will be useful, but
    10   * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11   * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
    12   * details.
    13   *
    14   * You should have received a copy of the GNU General Public License along with
    15   * dependency-check-core. If not, see http://www.gnu.org/licenses/.
    16   *
    17   * Copyright (c) 2013 Jeremy Long. All Rights Reserved.
    18   */
    19  package org.owasp.dependencycheck.utils;
    20
    21  import java.io.IOException;
    22  import java.io.InputStream;
    23  import java.util.logging.FileHandler;
    24  import java.util.logging.Level;
    25  import java.util.logging.LogManager;
    26  import java.util.logging.Logger;
    27  import java.util.logging.SimpleFormatter;
    28
    29  /**
    30   * A utility class to aide in the setup of the logging mechanism.
    31   *
    32   * @author Jeremy Long (jeremy.long@owasp.org)
    33   */
    34  public final class LogUtils {
    35
    36      /**
    37       * Private constructor for a utility class.
    38       */
    39      private LogUtils() {
    40      }
    41
    42      /**
    43       * Configures the logger for use by the application.
    44       *
    45       * @param in the input stream to read the log settings from
    46       * @param verboseLogFile the file path for the verbose log
    47       */
    48      public static void prepareLogger(InputStream in, String verboseLogFile) {
    49          try {
    50              LogManager.getLogManager().reset();
    51              LogManager.getLogManager().readConfiguration(in);
    52              if (verboseLogFile != null && !verboseLogFile.isEmpty()) {
    53                  verboseLoggingEnabled = true;
    54                  final Logger logger = Logger.getLogger("");
    55                  final FileHandler handler = new FileHandler(verboseLogFile, true);
    56                  handler.setFormatter(new SimpleFormatter());
    57                  handler.setLevel(Level.FINE);
    58                  handler.setFilter(new LogFilter());
    59                  logger.addHandler(handler);
    60                  logger.setLevel(Level.FINE);
    61              }
    62          } catch (IOException ex) {
    63              Logger.getLogger(LogUtils.class.getName()).log(Level.FINE, "IO Error preparing the logger", ex);
    64          } catch (SecurityException ex) {
    65              Logger.getLogger(LogUtils.class.getName()).log(Level.FINE, "Error preparing the logger", ex);
    66          } finally {
    67              if (in != null) {
    68                  try {
    69                      in.close();
    70                  } catch (Exception ex) {
    71                      Logger.getLogger(LogUtils.class.getName()).log(Level.FINEST, "Error closing resource stream", ex);
    72                  }
    73              }
    74          }
    75      }
    76      /**
    77       * Whether or not verbose logging is enabled.
    78       */
    79      private static boolean verboseLoggingEnabled = false;
    80
    81      /**
    82       * Get the value of verboseLoggingEnabled.
    83       *
    84       * @return the value of verboseLoggingEnabled
    85       */
    86      public static boolean isVerboseLoggingEnabled() {
    87          return verboseLoggingEnabled;
    88      }
    89  }