| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| LogUtils |
|
| 3.0;3 |
| 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.IOException; | |
| 21 | import java.io.InputStream; | |
| 22 | import java.util.logging.FileHandler; | |
| 23 | import java.util.logging.Level; | |
| 24 | import java.util.logging.LogManager; | |
| 25 | import java.util.logging.Logger; | |
| 26 | import java.util.logging.SimpleFormatter; | |
| 27 | ||
| 28 | /** | |
| 29 | * A utility class to aide in the setup of the logging mechanism. | |
| 30 | * | |
| 31 | * @author Jeremy Long <jeremy.long@owasp.org> | |
| 32 | */ | |
| 33 | public final class LogUtils { | |
| 34 | ||
| 35 | /** | |
| 36 | * The logger. | |
| 37 | */ | |
| 38 | 1 | private static final Logger LOGGER = Logger.getLogger(LogUtils.class.getName()); |
| 39 | ||
| 40 | /** | |
| 41 | * Private constructor for a utility class. | |
| 42 | */ | |
| 43 | private LogUtils() { | |
| 44 | } | |
| 45 | ||
| 46 | /** | |
| 47 | * Configures the logger for use by the application. | |
| 48 | * | |
| 49 | * @param in the input stream to read the log settings from | |
| 50 | * @param verboseLogFile the file path for the verbose log | |
| 51 | */ | |
| 52 | public static void prepareLogger(InputStream in, String verboseLogFile) { | |
| 53 | try { | |
| 54 | 0 | LogManager.getLogManager().reset(); |
| 55 | 0 | LogManager.getLogManager().readConfiguration(in); |
| 56 | 0 | if (verboseLogFile != null && !verboseLogFile.isEmpty()) { |
| 57 | 0 | verboseLoggingEnabled = true; |
| 58 | 0 | final Logger logger = Logger.getLogger(""); |
| 59 | 0 | final FileHandler handler = new FileHandler(verboseLogFile, true); |
| 60 | 0 | handler.setFormatter(new SimpleFormatter()); |
| 61 | 0 | handler.setLevel(Level.FINE); |
| 62 | 0 | handler.setFilter(new LogFilter()); |
| 63 | 0 | logger.addHandler(handler); |
| 64 | 0 | logger.setLevel(Level.FINE); |
| 65 | } | |
| 66 | 0 | } catch (IOException ex) { |
| 67 | 0 | LOGGER.log(Level.FINE, "IO Error preparing the logger", ex); |
| 68 | 0 | } catch (SecurityException ex) { |
| 69 | 0 | LOGGER.log(Level.FINE, "Error preparing the logger", ex); |
| 70 | } finally { | |
| 71 | 0 | if (in != null) { |
| 72 | try { | |
| 73 | 0 | in.close(); |
| 74 | 0 | } catch (Throwable ex) { |
| 75 | 0 | LOGGER.log(Level.FINEST, "Error closing resource stream", ex); |
| 76 | 0 | } |
| 77 | } | |
| 78 | } | |
| 79 | 0 | } |
| 80 | /** | |
| 81 | * Whether or not verbose logging is enabled. | |
| 82 | */ | |
| 83 | 1 | private static boolean verboseLoggingEnabled = false; |
| 84 | ||
| 85 | /** | |
| 86 | * Get the value of verboseLoggingEnabled. | |
| 87 | * | |
| 88 | * @return the value of verboseLoggingEnabled | |
| 89 | */ | |
| 90 | public static boolean isVerboseLoggingEnabled() { | |
| 91 | 3 | return verboseLoggingEnabled; |
| 92 | } | |
| 93 | } |