##### Coverage Report - org.owasp.dependencycheck.utils.LogUtils

Classes in this File | Line Coverage | Branch Coverage | Complexity
LogUtils |
8% | 2/25
0% | 0/6
| 3.0;3

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 | 0 |
         private LogUtils() {
40 | 0 |
         }
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 | 0 |
                 LogManager.getLogManager().reset();
51 | 0 |
                 LogManager.getLogManager().readConfiguration(in);
52 | 0 |
                 if (verboseLogFile != null && !verboseLogFile.isEmpty()) {
53 | 0 |
                     verboseLoggingEnabled = true;
54 | 0 |
                     final Logger logger = Logger.getLogger("");
55 | 0 |
                     final FileHandler handler = new FileHandler(verboseLogFile, true);
56 | 0 |
                     handler.setFormatter(new SimpleFormatter());
57 | 0 |
                     handler.setLevel(Level.FINE);
58 | 0 |
                     handler.setFilter(new LogFilter());
59 | 0 |
                     logger.addHandler(handler);
60 | 0 |
                     logger.setLevel(Level.FINE);
61 |  |
                 }
62 | 0 |
             } catch (IOException ex) {
63 | 0 |
                 Logger.getLogger(LogUtils.class.getName()).log(Level.FINE, "IO Error preparing the logger", ex);
64 | 0 |
             } catch (SecurityException ex) {
65 | 0 |
                 Logger.getLogger(LogUtils.class.getName()).log(Level.FINE, "Error preparing the logger", ex);
66 |  |
             } finally {
67 | 0 |
                 if (in != null) {
68 |  |
                     try {
69 | 0 |
                         in.close();
70 | 0 |
                     } catch (Exception ex) {
71 | 0 |
                         Logger.getLogger(LogUtils.class.getName()).log(Level.FINEST, "Error closing resource stream", ex);
72 | 0 |
                     }
73 |  |
                 }
74 |  |
             }
75 | 0 |
         }
76 |  |
         /**
77 |  |
          * Whether or not verbose logging is enabled.
78 |  |
          */
79 | 1 |
         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 | 3 |
             return verboseLoggingEnabled;
88 |  |
         }
89 |  |
     }