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 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 LogManager.getLogManager().reset();
55 LogManager.getLogManager().readConfiguration(in);
56
57 if (verboseLogFile != null && !verboseLogFile.isEmpty()) {
58 verboseLoggingEnabled = true;
59 final Logger logger = Logger.getLogger("");
60 final FileHandler fileHandler = new FileHandler(verboseLogFile, true);
61 fileHandler.setFormatter(new SimpleFormatter());
62 fileHandler.setLevel(Level.FINE);
63 fileHandler.setFilter(new LogFilter());
64
65 logger.addHandler(fileHandler);
66 logger.setLevel(Level.FINE);
67 }
68 } catch (IOException ex) {
69 LOGGER.log(Level.WARNING, "IO Error preparing the logger", ex);
70 } catch (SecurityException ex) {
71 LOGGER.log(Level.WARNING, "Error preparing the logger", ex);
72 } catch (Throwable ex) {
73 LOGGER.log(Level.WARNING, "Error preparing the logger", ex);
74 } finally {
75 if (in != null) {
76 try {
77 in.close();
78 } catch (Throwable ex) {
79 LOGGER.log(Level.FINEST, "Error closing resource stream", ex);
80 }
81 }
82 }
83 }
84 /**
85 * Whether or not verbose logging is enabled.
86 */
87 private static boolean verboseLoggingEnabled = false;
88
89 /**
90 * Get the value of verboseLoggingEnabled.
91 *
92 * @return the value of verboseLoggingEnabled
93 */
94 public static boolean isVerboseLoggingEnabled() {
95 return verboseLoggingEnabled;
96 }
97 }