View Javadoc
1   /*
2    * This file is part of dependency-check-core.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   * Copyright (c) 2013 Jeremy Long. All Rights Reserved.
17   */
18  package org.owasp.dependencycheck.utils;
19  
20  import java.io.File;
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
33   */
34  public final class LogUtils {
35  
36      /**
37       * The logger.
38       */
39      private static final Logger LOGGER = Logger.getLogger(LogUtils.class.getName());
40  
41      /**
42       * Private constructor for a utility class.
43       */
44      private LogUtils() {
45      }
46  
47      /**
48       * Configures the logger for use by the application.
49       *
50       * @param in the input stream to read the log settings from
51       * @param verboseLogFile the file path for the verbose log
52       */
53      public static void prepareLogger(InputStream in, String verboseLogFile) {
54          try {
55              LogManager.getLogManager().reset();
56              LogManager.getLogManager().readConfiguration(in);
57  
58              if (verboseLogFile != null && !verboseLogFile.isEmpty()) {
59                  verboseLoggingEnabled = true;
60                  final Logger logger = Logger.getLogger("");
61                  final File logFile = new File(verboseLogFile);
62                  final File logDir = logFile.getParentFile();
63                  if (logDir != null && !logDir.isDirectory() && !logDir.mkdirs()) {
64                      final String msg = String.format("Unable to create directory '%s', verbose logging will be disabled.",
65                              logDir.getAbsolutePath());
66                      throw new IOException(msg);
67                  }
68                  final FileHandler fileHandler = new FileHandler(verboseLogFile, true);
69                  fileHandler.setFormatter(new SimpleFormatter());
70                  fileHandler.setLevel(Level.FINE);
71                  fileHandler.setFilter(new LogFilter());
72  
73                  logger.addHandler(fileHandler);
74                  logger.setLevel(Level.FINE);
75              }
76          } catch (IOException ex) {
77              LOGGER.log(Level.WARNING, "IO Error preparing the logger", ex);
78          } catch (SecurityException ex) {
79              LOGGER.log(Level.WARNING, "Error preparing the logger", ex);
80          } catch (Throwable ex) {
81              LOGGER.log(Level.WARNING, "Error preparing the logger", ex);
82          } finally {
83              if (in != null) {
84                  try {
85                      in.close();
86                  } catch (IOException ex) {
87                      LOGGER.log(Level.FINEST, "Error closing resource stream", ex);
88                  }
89              }
90          }
91      }
92      /**
93       * Whether or not verbose logging is enabled.
94       */
95      private static boolean verboseLoggingEnabled = false;
96  
97      /**
98       * Get the value of verboseLoggingEnabled.
99       *
100      * @return the value of verboseLoggingEnabled
101      */
102     public static boolean isVerboseLoggingEnabled() {
103         return verboseLoggingEnabled;
104     }
105 }